int getopt(argc, argv, optstring) int argc; char **argv; char *optstring;
extern char *optarg; extern int optind, opterr;
getopt() returns the next option letter in argv that matches a letter in optstring. optstring must contain the option letters the command using getopt() will recognize; if a letter is followed by a colon, the option is expected to have an argument, or group of arguments, which must be separated from it by white space.
optarg is set to point to the start of the option argument on return from getopt.
getopt() places in optind the argv index of the next argument to be processed. optind is external and is initialized to 1 before the first call to getopt.
When all options have been processed (that is, up to the first non-option argument), getopt() returns -1. The special option ``--'' may be used to delimit the end of the options; when it is encountered, -1 will be returned, and ``--'' will be skipped.
main(argc, argv) int argc; char **argv; { int c; extern char *optarg; extern int optind; . . . while ((c = getopt(argc, argv, "abo:")) != -1) switch (c) { case 'a': if (bflg) errflg++; else aflg++; break; case 'b': if (aflg) errflg++; else bproc (); break; case 'o': ofile = optarg; break; case '?': errflg++; } if (errflg) { (void)fprintf(stderr, "usage: . . . "); exit (2); } for (; optind < argc; optind++) { if (access(argv[optind], 4)) { . . . }
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97