Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(3c) — NEWS-os 5.0.1

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopts(1)

intro(1)

getsubopt(3C)



getopt(3C)              LIBRARY FUNCTIONS              getopt(3C)



NAME
     getopt - get option letter from argument vector

SYNOPSIS
     #include <stdlib.h>

     int getopt (int argc, char * const *argv, const  char  *opt-
     string);

     extern char *optarg;

     extern int optind, opterr, optopt;

DESCRIPTION
     getopt returns the next option letter in argv that matches a
     letter  in optstring.  It supports all the rules of the com-
     mand syntax standard [see intro(1)].  Since all new commands
     are  intended to adhere to the command syntax standard, they
     should use  getopts(1),  getopt(3C),  or  getsubopts(3C)  to
     parse  positional  parameters and check for options that are
     legal for that command.

     optstring must contain the option letters the command  using
     getopt  will  recognize; if a letter is followed by a colon,
     the option is expected to have  an  argument,  or  group  of
     arguments, which may be separated from it by white space.

     optarg is set to point to the start of  the  option-argument
     on return from getopt.

     getopt places in optind the argv index of the next  argument
     to be processed.  optind is external and is initialized to 1
     before the first call to getopt.

     When all options have been processed (i.e., up to the  first
     non-option  argument),  getopt  returns  EOF.   The  special
     option ``--'' may be used to delimit the end of the options;
     when  it  is  encountered,  EOF will be returned, and ``--''
     will be skipped. This is  useful  in  delimiting  non-option
     arguments that begin with "-".

DIAGNOSTICS
     getopt prints an error message on standard error and returns
     a question mark (?)  when it encounters an option letter not
     included in optstring or no option-argument after an  option
     that  expects  one.   This  error message may be disabled by
     setting opterr to 0.  The value of the character that caused
     the error is in optopt.

EXAMPLE
     The following code fragment shows how one might process  the
     arguments for a command that can take the mutually exclusive



                                                                1





getopt(3C)              LIBRARY FUNCTIONS              getopt(3C)



     options a and  b,  and  the  option  o,  which  requires  an
     option-argument:

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

     main (int argc, char **argv)
     {
          int c;
          extern char *optarg;
          extern int optind;
          int aflg = 0;
          int bflg = 0;
          int errflg = 0;
          char *ofile = NULL;

          while ((c = getopt(argc, argv, "abo:")) != EOF)
               switch (c) {
               case 'a':
                    if (bflg)
                         errflg++;
                    else
                         aflg++;
                    break;
               case 'b':
                    if (aflg)
                         errflg++;
                    else
                         bflg++;
                    break;
               case 'o':
                    ofile = optarg;
                    (void)printf("ofile = %s\n", ofile);
                    break;
               case '?':
                    errflg++;
               }
          if (errflg) {
               (void)fprintf(stderr, "usage: cmd [-a|-b] [-o<file>] files...\n");
               exit (2);
          }
          for ( ; optind < argc; optind++)
               (void)printf("%s\n", argv[optind]);
          return 0;
     }

NOTES
     The library routine getopt does not fully check  for  manda-
     tory  arguments.  That  is, given an option string "a:b" and
     the input "-a -b", getopt assumes that "-b" is the mandatory
     argument  to  the  option  "-a" and not that -a is missing a
     mandatory argument.  Although the following  command  syntax



                                                                2





getopt(3C)              LIBRARY FUNCTIONS              getopt(3C)



     rule  [see  intro(1)]  relaxation  is  permitted  under  the
     current implementation, they should not be used because they
     may  not  be supported in future releases of the system.  As
     in the EXAMPLE section above, a and b are options, o  is  an
     option  which  requires  an  option-argument, and xxx is the
     option-argument to o.

     cmd -aboxxx file  (Violation:  options with
           option-arguments must not be grouped with other options)

SEE ALSO
     getopts(1), intro(1) in the User's Reference Manual.
     getsubopt(3C).










































                                                                3



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