Find the answer to your Linux question:
Results 1 to 2 of 2
I've been studying the getopt() function of glibc, and I've noticed something I'm not sure how to address. The manual page says that a string of valid characters is to ...
  1. #1
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162

    getopt(): options requiring arguments

    I've been studying the getopt() function of glibc, and I've noticed something I'm not sure how to address. The manual page says that a string of valid characters is to be held in "optstring", and if an option character has a colon ':' behind it, then it requires an argument, and will cause getopt() to produce an ':' or '?' if getopt detects a missing argument.

    The problem is that it seems as if the last option in "optstring" with a colon is the only one that will cause ':' or '?' to be returned, and thus causing it to be the only one properly checked for a necessary argument. I've taken the following example code from the manual page and modified 'n' to require an argument...

    Code:
    #include <unistd.h>
           #include <stdlib.h>
           #include <stdio.h>
    
           int
           main(int argc, char *argv[])
           {
               int flags, opt;
               int nsecs, tfnd;
    
               nsecs = 0;
               tfnd = 0;
               flags = 0;
               while ((opt = getopt(argc, argv, "n:t:")) != -1) {
                   switch (opt) {
                   case 'n':
                       flags = 1;
                       break;
                   case 't':
                       nsecs = atoi(optarg);
                       tfnd = 1;
                       break;
                   default: /* '?' */
                       fprintf(stderr, "Usage: %s [-t nsecs] [-n] name\n",
                               argv[0]);
                       exit(EXIT_FAILURE);
                   }
               }
    
               printf("flags=%d; tfnd=%d; optind=%d\n", flags, tfnd, optind);
    
               if (optind >= argc) {
                   fprintf(stderr, "Expected argument after options\n");
                   exit(EXIT_FAILURE);
               }
    
               printf("name argument = %s\n", argv[optind]);
    
               /* Other code omitted */
    
               exit(EXIT_SUCCESS);
           }
    As you can see, I've specified that option 'n' requires an argument, and as you can see, it produces no error...

    Code:
    $./getopttest -n -t val
    flags=1; tfnd=0; optind=3
    name argument = val
    However, if I reverse the positions of -t and -n, and give -t no argument, this is my result...

    Code:
    $./getopttest -t -n
    flags=0; tfnd=1; optind=3
    Expected argument after options

    Anyway, these results are pretty unexpected, and for the time being I've just modified the switch statements to test for an argument to each option instead of relying on getopt(), but I'd like to know what is going on, what I've misunderstood, etc.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I think you need to check the return value of getopt() for other than -1 (no more arguments). It will return a '?' or ':' if there is a missing argument.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...