getopt Subroutine

Purpose

Returns the next flag letter specified on the command line.

Library

Standard C Library (libc.a)

Syntax

#include <unistd.h>

int getopt (ArgumentC,  ArgumentV,  OptionString)
int ArgumentC;
char *const ArgumentV [ ];
const char *OptionString;

extern int  optind;
extern int  optopt;
extern int  opterr;
extern char * optarg;

Description

The optind parameter indexes the next element of the ArgumentV parameter to be processed. It is initialized to 1 and the getopt subroutine updates it after calling each element of the ArgumentV parameter.

The getopt subroutine returns the next flag letter in the ArgumentV parameter list that matches a letter in the OptionString parameter. If the flag takes an argument, the getopt subroutine sets the optarg parameter to point to the argument as follows:

Parameters

Item Description
ArgumentC Specifies the number of parameters passed to the routine.
ArgumentV Specifies the list of parameters passed to the routine.
OptionString Specifies 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 separated from it by white space.
optind Specifies the next element of the ArgumentV array to be processed.
optopt Specifies any erroneous character in the OptionString parameter.
opterr Indicates that an error has occurred when set to a value other than 0.
optarg Points to the next option flag argument.

Return Values

The getopt subroutine returns the next flag letter specified on the command line. A value of -1 is returned when all command line flags have been parsed. When the value of the ArgumentV [optind] parameter is null, *ArgumentV [optind] is not the (minus) character, or ArgumentV [optind] points to the "-" (minus) string, the getopt subroutine returns a value of -1 without changing the value. If ArgumentV [optind] points to the "- -" (double minus) string, the getopt subroutine returns a value of -1 after incrementing the value of the optind parameter.

Error Codes

If the getopt subroutine encounters an option character that is not specified by the OptionString parameter, a ? (question mark) character is returned. If it detects a missing option argument and the first character of OptionString is a : (colon), then a : (colon) character is returned. If this subroutine detects a missing option argument and the first character of OptionString is not a colon, it returns a ? (question mark). In either case, the getopt subroutine sets the optopt parameter to the option character that caused the error. If the application has not set the opterr parameter to 0 and the first character of OptionString is not a : (colon), the getopt subroutine also prints a diagnostic message to standard error.

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 parameters.

#include <unistd.h>    /*Needed for access subroutine 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 */