Guardian Native C Library Calls Reference Manual (G06.28+, H06.04+)
getopt(3) Guardian Native C Library Calls Reference Manual
NAME
getopt - Gets flag letters from the argument vector
LIBRARY
G-series native Guardian processes: $SYSTEM.SYSnn.ZCRTLSRL
G-series native OSS processes: /G/system/sysnn/zcrtlsrl
H-series native Guardian processes: $SYSTEM.ZDLLnnn.ZCRTLDLL
H-series OSS processes: /G/system/zdllnnn/zcrtldll
SYNOPSIS
#include <unistd.h>
int getopt(
int argc,
const char * const argv[],
const char *optstring);
extern char *optarg;
extern int optind;
extern int opterr;
extern int optopt;
PARAMETERS
argc Specifies the number of parameters passed to the routine.
argv Points to an array of argc pointers to argument strings.
optstring Specifies a string of recognized flag characters. If a character 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.
DESCRIPTION
The getopt() function parses argument lists. It returns the next flag character in the argv parame-
ter list that matches a character in the optstring parameter. If that flag takes an argument, the
getopt() function has the optarg variable point to the flag argument according to the following
rules:
• If the flag is the last character pointed at by an argv element, optarg will contain argv’s
next element, and optind is incremented by 2. The getopt() function returns an error if
the resulting optind is greater than or equal to argc.
• If the flag is not the last character, then the optarg variable points at the string after the
flag character in the associated element of argv. The optind variable is incremented by
1.
The optarg external variable is set to point to the start of the flag’s parameter on return from the
getopt() function.
The getopt() function places the argv index of the next argument to be processed in optind. The
optind variable is externally initialized to 1 before the first call to getopt() so that argv[0] is not
processed. Error messages can be suppressed by providing a value of 0 (zero) as the opterr
parameter.
EXAMPLES
The following example shows a suggested way to use the getopt() function.
#include <unistd.h>
main(argc, argv)
int argc;
char *argv[];
3−24 Hewlett-Packard Company 527192-005