Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(3) — AIX/RT 2.2.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopt

     getopt

Purpose

     Gets flag letters from the argument vector.

Library

     Standard C Library (libc.a)

Syntax

     int getopt (argc, argv, optstring)        extern char *optarg;
     int argc;
     char **argv;                              extern int optind;
     char *optstring;
     Description

     The getopt subroutine returns the next flag letter in the
     argv  parameter  list  that   matches  a  letter  in  the
     optstring parameter.  The getopt  subroutine is an aid to
     help programs interpret shell command-line flags that are
     passed to them.

     The optstring  parameter is  a string of  recognized flag
     letters.  If a letter is followed by a colon, the flag is
     expected to take a parameter that may or may not be sepa-
     rated from it by white  space.  The optarg external vari-
     able is set to point to the start of the flag's parameter
     on return from the getopt subroutine.

     The getopt subroutine  places the argv index  of the next
     argument to be processed in optind.  optind is externally
     initialized to 1 so that argv[0] is not processed.

     When all  flags have been  processed (that is, up  to the
     first  nonflag argument),  the getopt  subroutine returns
     EOF.  The  special flag "--"  (dash dash) can be  used to
     delimit the end  of the flags; EOF is  returned, and "--"
     is skipped.

     The getopt  subroutine prints an error  message on stderr
     and   returns  "(int) '?'"   (question   mark)  when   it
     encounters  a flag  letter that  is not  included in  the
     optstring parameter.

     Examples

     The  following code  fragment processes  the flags  for a
     command that can take the  mutually exclusive flags a and
     b, and the  flags f and o, both of  which require parame-
     ters.

       #include <unistd.h>      /* Needed for access system call constants */

       main (argc, argv)
       int argc;
       char **argv;
       {
          int c;
          extern int optind;
          extern char *optarg;
          .
          while ((c = getopt(argc, argv, "abf:o:")) != EOF)
          {
             switch (c)
             {
                case 'a':
                   if (bflg)
                      errflg++;
                   else
                      aflg++;
                   break;
                case 'b':
                   if (aflg)
                      errflg++;
                   else
                      bflg++;
                   break;
                case 'f':
                   ifile = optarg;
                   break;
                case 'o':
                   ofile = optarg;
                   break;
                case '?':
                   errflg++;
             } /* case */

             if (errflg)
             {
                fprintf(stderr, "usage: . . . ");
                exit(2);
             }
          } /* while */

          for ( ; optind < argc; optind++)
          {
             if (access(argv[optind], R_OK))
             {
             .
             }
          } /* for  */
       }    /* main */

     Related Information

     The getopt command in  AIX Operating System Commands Ref-
     erence.

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026