Guardian Native C Library Calls Reference Manual (G06.28+, H06.05+)
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 flag found in the argv parameter. This is true
whether the flag is in the optstring parameter or not.
RETURN VALUES
Upon successful completion, the getopt() function returns the flag character that was detected.
If it encounters a flag 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 flag, the getopt() function returns a : (colon) if optstring’s first
character is a : (colon), and a ? (question mark) otherwise. In addition, the getopt() function sets
the optopt variable to the flag 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 optstring’s first character is not a : (colon).
When all flags have been processed (that is, up to the first nonflag argument), the getopt() func-
tion returns a value of -1. The special flag -- (dash dash) can be used to delimit the end of the
flags; -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) flag.
527192-007 Hewlett-Packard Company 3−25