tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

MacOS and tools and getopt

I've found a problem where getopt() on the MacOS tools version of
mdsetimage doesn't work, and I'm not sure what the fix should be.

Using the attached program below:

        mini:/tmp 74% cc -O2 -o foo foo.c
        mini:/tmp 75% ./foo -v 2 3 
        argc = 4, optind = 1
          got opt v
        argc = 2, optind = 2
   (yay! correct)
        mini:/tmp 76% cc -O2 -o foo foo.c 
        mini:/tmp 77% ./foo -v 2 3
        argc = 4, optind = 1
          got opt v
        argc = 3, optind = 1
   (boo hiss evil)

What appears to be happening is that as soon as we include either of:


we pull in this line:

        int      getopt(int, char * const [], const char *) 

which gives us a symbol renamed getopt().  Then the MacOS libc getopt()
and the libiberty getopt() seem to get all confused and optind comes out
a mess.

One "fix" is to not use libiberty's getopt on Macos.  I'm not yet sure
how to do that.

Another somewhat hacky (but easy!) fix is to just not include the -v
in the mdsetimage commandline.  If getopt does no work, nothing needs
adjusting and then the mdsetimage just works.

Any suggestions?


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

main(int argc, char **argv)
        int ch;

        printf("argc = %d, optind = %d\n", argc, optind);

        while ((ch = getopt(argc, argv, "v")) != -1) {
                printf("  got opt %c\n", ch);

        argc -= optind;
        argv += optind;

        printf("argc = %d, optind = %d\n", argc, optind);


Home | Main Index | Thread Index | Old Index