Guardian Native C Library Calls Reference Manual (G06.28+, H06.04+)

Guardian Native C Library Calls (g - i) getopt(3)
#define ARGS "r:w:f:s"
{
int c, errflg = 0;
int readers = 1, writers = 1;
int freeBufs = 1;
int doStats = FALSE;
optarg = NULL;
while (!errflg && (c = getopt(argc, argv, ARGS)) != -1)
switch (c) {
case ’r’ :
readers = atoi(optarg);
break;
case ’w’ :
writers = atoi(optarg);
break;
case ’f’ :
freeBufs = atoi(optarg);
break;
case ’s’ :
doStats = TRUE;
break;
default :
errflg++;
}
NOTES
The external int optopt variable is set to the real ag found in the argv parameter. This is true
whether the ag is in the optstring parameter or not.
RETURN VALUES
Upon successful completion, the getopt() function returns the ag character that was detected.
If it encounters a ag that is not included in the optstring parameter, or if the : (colon) character
is used incorrectly, the getopt() function prints an error message on stderr and returns a ? (ques-
tion mark). If there is a missing ag, the getopt() function returns a : (colon) if optstrings rst
character is a : (colon), and a ? (question mark) otherwise. In addition, the getopt() function sets
the optopt variable to the ag character that caused one of these errors.
The getopt() function also displays a diagnostic message if the application did not set the opterr
variable to 0 (zero), and optstrings rst character is not a : (colon).
When all ags have been processed (that is, up to the rst nonag argument), the getopt() func-
tion returns a value of -1. The special ag -- (dash dash) can be used to delimit the end of the
ags; -1 is returned, and the -- (dash dash) string is skipped.
The getopt() function does not change optind, and also returns a value of -1, if one of the fol-
lowing occurs:
The argv[optind] result is NULL.
The *argv[optind] result is not the special - (dash) ag.
527192-005 Hewlett-Packard Company 325