This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

getopt_long takes an option as an argument of another option which requires argument


Hi!
I need help in explaining one of the behavior of getopt_long, where an
option requires an argument, but unfortunately it is followed by
another option, thus that option becomes the argument, and getopt_long
does not raise error.

I want to understand if this is by standard or a bug in getopt_long
(glibc). I wrote a sample program on Redhat (Red Hat Enterprise Linux
Server release 5.8 (Tikanga)), and glic 2.0.

For the sample program,

1. To compile
   gcc -g -o getopttest getoptlong.c
2. To run
   ./getopttest -c -d -d

Then you will see the first '-d' becomes the value of "-c" in the
console output:

$ ./getopttest -c -d -d
option c with value '-d'
option d

If I NM the symbol,
$ nm geto | grep getopttest
$ U getopt_long@@GLIBC_2.0

It shows glibc 2.0.

Please help,

Thanks,

Eng



===================== <getoptlong.c>====================
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

int main(int argc, char **argv)
{
    int c = 0;
        int option_index = 0;
        static struct option long_options[] =
        {
            {"all",     1, 0, 'a'},
            {"update",  0, 0, 'u'},
            {"delete",  0, 0, 'd'},
            {"verbose", 0, 0, 'V'},
            {"cond",    1, 0, 'c'},
            {"name",    1, 0, 'n'},
            {0,         0, 0,  0 }
        };

        while ((c = getopt_long(argc, argv, "a:udVc:n:", long_options,
&option_index)) != -1)
        {
                switch (c) {
                case 'a':
                        printf("option a with value '%s'\n", optarg);
                        break;

                case 'u':
                        printf("option u\n");
                        break;

                case 'd':
                        printf("option d\n");
                        break;

                case 'V':
                        printf("option V\n");
                        break;

                case 'c':
                        printf("option c with value '%s'\n", optarg);
                        break;

                case 'n':
            printf("option n with value '%s'\n", optarg);
            break;

                case '?':
            printf("Found invalid option or missing required argument\n");
            break;

                case ':':
            printf("Found missing required argument\n");
            break;

                default:
            printf("?? getopt returned character code 0%o ??\n", c);
                }
    }

   if (optind < argc) {
        printf("non-option ARGV-elements: ");
        while (optind < argc)
            printf("%s ", argv[optind++]);
        printf("\n");
    }

   exit(0);
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]