Crossfire wrote:


So would I.  If I was trying to implement the original arrangement
properly, I'd probably do it something like this...

---BEGIN---
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <libintl.h>

#define _(x) gettext(x)
#define VERSION "0.1"

void
somefunction(char *strout, int n)
{
       strncpy(strout, _("some words"), n);
       strout[n-1]='\0';
}

void
usage(int argc, char *argv[])
{
       fprintf(stderr,_("%s: do stuff\n\n"), argv[0]);
       fprintf(stderr,_("usage:\n  %s [<flags>]\n\n"), argv[0]);
       fprintf(stderr,_("valid flags:\n"));
        fprintf(stderr,_("  -h, --help      display this message\n"));
       fprintf(stderr,_("  -v, --version   display the version number\n"));
       exit(1);
}

void
version(int argc, char *argv[])
{
       fprintf(stderr, _("%s: version %s\n"), argv[0], VERSION);
       exit(1);
}

static struct option opts[] = {
        {"help", no_argument, NULL, 'h'},
        {"version", no_argument, NULL, 'v'},
        {NULL, 0, NULL, 0}
};

#ifndef BUF
#define BUF     256
#endif

int
main (int argc, char *argv[])
{
       char    string[BUF];
       int     c;

       while (-1 != (c = getopt_long(argc, argv, "hv", opts, NULL))) {
               switch (c) {
               case 'v':
                       version (argc, argv);
                       break;
               case 'h':
                       usage (argc, argv);
                       break;
               default:
                       break;
               }
       }

       somefunction(string, BUF);

       printf (_("\n\nString is: %s\n\n"), string);

       return 0;
}
---END---

Thus keeping the i18n and GNU people happy too!


I'd cut the crap off to demonstrate the idea and get,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void
somefunction(char *strout)
{
       char *string2 = "some words";
       strcpy(strout, string2);
}

int main (void)
{
     char *string1;
     string1 = malloc(11);
     somefunction(string1);
     printf ("\n\nString is: %s\n\n",string1);
     return 0;
}

And compare to,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *
somefunction()
{
       char *string2 = "some words";
       return string2;
}

int main (void)
{
       printf ("\n\nString is: %s\n\n", somefunction());
       return 0;
}



--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to