Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(3C) — Reliant UNIX 5.44c4

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

getopts(1)

getsubopt(3C)

unistd(4)

getopt(3C)                                                       getopt(3C)

NAME
     getopt, optarg, optind, opterr, optopt - get option letter from argu-
     ment vector

SYNOPSIS
     #include <unistd.h>

     int getopt(int argc, char * const argv[], const char *optstring);

     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 command syntax stan-
     dard. Since all new commands are intended to adhere to the command
     syntax standard, they should use getopt(1), getopt(3C), or
     getsubopt(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 sep-
     arated 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(). Arguments which include spaces must be quoted.

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

EXAMPLES
     The following code fragment shows how one might process the arguments
     for a command that can take the mutually exclusive options a and b,
     and the option o, which requires an argument:

        #include <unistd.h>

        int
        main (int argc, char *argv[])
        {








Page 1                       Reliant UNIX 5.44                Printed 11/98

getopt(3C)                                                       getopt(3C)

           int c;
           extern char *optarg;
           extern int optind, optopt;
           int aflg, bflg, errflg;
           char *ofile;
           char *ifile;

           while ((c = getopt(argc, argv, ":abf:o:")) != -1) {
                   switch (c) {
                   case 'a':
                           if (bflg)
                                   errflg++;
                           else
                                   aflg++;
                           break;
                   case 'b':
                           if (aflg)
                                   errflg++;
                           else {
                                   bflg++;
                                   bproc();
                           }
                           break;
                   case 'f':
                           ifile = optarg;
                           break;
                   case 'o':
                           ofile = optarg;
                           break;
                   case ':':           /* -f or -o without operand */
                           fprintf(stderr,"Option -%c
                                requires an operand\n", optopt);
                           errflg++;
                           break;
                   case '?':
                           fprintf(stderr,
                                "Unrecognized option: -%c\n", optiot);
                           errflg++;
                   }
           }
           if (errflg) {
                   fprintf(stderr,
                           "usage: ...");
                   exit (2);
           }
           for ( ; optind < argc; optind++) {
                   if (access (argv[optind], ROK)) {
           ...
        }





Page 2                       Reliant UNIX 5.44                Printed 11/98

getopt(3C)                                                       getopt(3C)

     The following are accepted as equivalent:

     cmd -ao arg path path
     cmd -a -o arg path path
     cmd -o arg -a path path
     cmd -a -o arg -- path path
     cmd -a -oarg path path
     cmd -aoarg path path

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

NOTES
     The getopt() function does not fully check for mandatory 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.

     If a "?" (question mark) is required as an option, opterr should be
     set to 0, and the character in optopt must be checked. If optopt holds
     the "?" character, a question mark was specified as an option. Other-
     wise an unrecognized option was given or no argument was given for an
     option that required it. The error message handling must be done by
     the program. Although getopt() allows such usage, the "?" character
     should not be used as a valid option.

     It is a violation of the command syntax standard for options with
     arguments to be grouped with other options, as in cmd -aboxxx file,
     where a and b are options, o is an option that requires an argument,
     and xxx is the argument to o. Although this syntax is permitted in the
     current implementation, it should not be used because it may not be
     supported in future releases. The correct syntax is cmd -ab oxxx file.

SEE ALSO
     getopts(1), getsubopt(3C), unistd(4).













Page 3                       Reliant UNIX 5.44                Printed 11/98

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