getopt Subroutine
Purpose
Returns the next flag letter specified on the command line.
Library
Standard C Library (libc.a)
Syntax
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:
- If the flag was the last letter in the string pointed to by an element of the ArgumentV parameter, the optarg parameter contains the next element of the ArgumentV parameter and the optind parameter is incremented by 2. If the resulting value of the optind parameter is not less than the ArgumentC parameter, this indicates a missing flag argument, and the getopt subroutine returns an error message.
- Otherwise, the optarg parameter points to the string following the flag letter in that element of the ArgumentV parameter and the optind parameter is incremented by 1.
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 */