GETOPT(3C) DOMAIN/IX SYS5 GETOPT(3C)
NAME
getopt - get option letter from argument vector
USAGE
int getopt(argc, argv, optstring)
int argc;
char **argv, *opstring;
extern char *optarg;
extern int optind, opterr;
DESCRIPTION
Getopt returns the next option letter in argv that matches a
letter in optstring. Optstring is a string of recognized
option letters. If a letter is followed by a colon, the
option should have an argument. Optarg is set to point to
the start of the option argument on return from getopt.
Getopt places the argv index of the next argument to be pro-
cessed in optind Because optind is external, it is automati-
cally set to zero before the first call to getopt.
When all the options have been processed (i.e., up to the
first non-option argument), getopt returns EOF. When spe-
cial option - (dash) is used to delimit the end of the
options, getopt returns EOF and ignores the dash.
EXAMPLE
The code fragment below shows how to process the arguments
for a command that can take the mutually exclusive options a
and b, and the options f and o, both of which require argu-
ments:
main (argc, argv)
int argc;
char **argv;
{
int c;
extern char **optarg;
extern int optind;
.
.
.
while ((c = getopt(argc, argv, "abf:o:")) != EOF)
switch (c) {
case 'a':
if (bflg)
errflg++;
else
aflg++;
Printed 12/4/86 GETOPT-1
GETOPT(3C) DOMAIN/IX SYS5 GETOPT(3C)
break;
case 'b':
if (aflg)
errflg++;
else
bproc( );
break;
case 'f':
ifile = optarg;
break;
case 'o':
ofile = optarg;
break;
case '?':
errflg++;
}
if (errflg) {
fprintf(stderr, "usage: . . . ");
exit (2);
}
for ( ; optind < argc; optind++) {
if (access(argv[optind], 4)) {
.
.
.
}
DIAGNOSTICS
Getopt prints an error message on stderr and returns a ques-
tion mark (?) when it encounters an option letter that is
not included in optstring. This error message may be dis-
abled by setting opterr to a non-zero value.
RELATED INFORMATION
getopt(1)
GETOPT-2 Printed 12/4/86