[ Previous | Next | Contents | Glossary | Home | Search ]
AIX Version 4.3 Base Operating System and Extensions Technical Reference, Volume 1

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

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 */

Implementation Specifics

This subroutine is part of Base Operating System (BOS) Runtime.

Related Information

The getopt command.

List of Executable Program Creation Subroutines, Subroutines Overview, and List of Multithread Subroutines in AIX Version 4.3 General Programming Concepts: Writing and Debugging Programs.


[ Previous | Next | Contents | Glossary | Home | Search ]