[3ae31e9] | 1 |
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | GETOPT(3) UNIX 5.0 (21 January 1986) GETOPT(3)
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | NAME
|
---|
| 8 |
|
---|
| 9 | getopt, optarg, optind - get option letter from argv
|
---|
| 10 |
|
---|
| 11 | SYNOPSIS
|
---|
| 12 |
|
---|
| 13 | int getopt(argc, argv, opts)
|
---|
| 14 | int argc;
|
---|
| 15 | char **argv;
|
---|
| 16 | char *opts;
|
---|
| 17 |
|
---|
| 18 | int
|
---|
| 19 | getarg(nargc, nargv)
|
---|
| 20 | int nargc;
|
---|
| 21 | char *nargv[];
|
---|
| 22 |
|
---|
| 23 | extern int optind;
|
---|
| 24 | extern int opterr;
|
---|
| 25 | extern int optopt;
|
---|
| 26 | extern char *optarg;
|
---|
| 27 |
|
---|
| 28 | DESCRIPTION
|
---|
| 29 |
|
---|
| 30 | This is the public domain getopt from AT&T with enhancements.
|
---|
| 31 |
|
---|
| 32 | Getopt returns the next option letter in argv that matches a
|
---|
| 33 | letter in opts. Opts is a string of recognized option
|
---|
| 34 | letters; if a letter is followed by a colon, the option is
|
---|
| 35 | expected to have an argument that may or may not be
|
---|
| 36 | separated from it by white space. Optarg is set to point to
|
---|
| 37 | the start of the option argument on return from getopt.
|
---|
| 38 |
|
---|
| 39 | Getopt places in optind the argv index of the next argument
|
---|
| 40 | to be processed. Optind is external and it is normally
|
---|
| 41 | initialized to one automatically before the first call to
|
---|
| 42 | getopt. This is so that the program name will not be processed
|
---|
| 43 | as an argument if getopt is used to process the command line
|
---|
| 44 | passed to the main function of a program.
|
---|
| 45 |
|
---|
| 46 | When all options have been processed (i.e., up to the first
|
---|
| 47 | non-option argument), getopt returns EOF. The special option
|
---|
| 48 | -- may be used to delimit the end of the options; EOF will
|
---|
| 49 | be returned, and -- will be skipped.
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | Getarg returns the index of the next argument, or EOF if the
|
---|
| 53 | argument string is exhausted. Getarg is used to get the file
|
---|
| 54 | arguments after getopt() is finished. Getarg updates optarg
|
---|
| 55 | to point at the argument, and increments optind to point
|
---|
| 56 | at the next argument.
|
---|
| 57 | |
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | GETOPT(3) UNIX 5.0 (21 January 1986) GETOPT(3)
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | DIAGNOSTICS
|
---|
| 65 |
|
---|
| 66 | Getopt prints an error message on stderr (if opterr is non-
|
---|
| 67 | zero) and returns a question mark (?) when it encounters an
|
---|
| 68 | option letter not included in opts. The error message uses
|
---|
| 69 | argv[0] as the program name. If this is not desireable, set
|
---|
| 70 | opterr = 0 and issue your own error message when getopt
|
---|
| 71 | returns '?'.
|
---|
| 72 |
|
---|
| 73 | EXAMPLE
|
---|
| 74 |
|
---|
| 75 | The following code fragment shows how one might process the
|
---|
| 76 | arguments for a command that can take the mutually exclusive
|
---|
| 77 | options a and b, and the options f and o, both of which
|
---|
| 78 | require arguments:
|
---|
| 79 |
|
---|
| 80 | main(argc, argv)
|
---|
| 81 | int argc;
|
---|
| 82 | char **argv;
|
---|
| 83 | {
|
---|
| 84 | int c;
|
---|
| 85 | extern int optind;
|
---|
| 86 | extern char *optarg;
|
---|
| 87 |
|
---|
| 88 | ...
|
---|
| 89 | |
---|
| 90 |
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | GETOPT(3) UNIX 5.0 (21 January 1986) GETOPT(3)
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | while ((c = getopt(argc, argv, "abf:o:")) != EOF)
|
---|
| 97 | switch (c) {
|
---|
| 98 |
|
---|
| 99 | case 'a':
|
---|
| 100 | if (bflg)
|
---|
| 101 | errflg++;
|
---|
| 102 | else
|
---|
| 103 | aflg++;
|
---|
| 104 | break;
|
---|
| 105 |
|
---|
| 106 | case 'b':
|
---|
| 107 | if (aflg)
|
---|
| 108 | errflg++;
|
---|
| 109 | else
|
---|
| 110 | bproc();
|
---|
| 111 | break;
|
---|
| 112 |
|
---|
| 113 | case 'f':
|
---|
| 114 | infile = optarg;
|
---|
| 115 | break;
|
---|
| 116 |
|
---|
| 117 | case 'o':
|
---|
| 118 | ofile = optarg;
|
---|
| 119 | bufsiza = 512;
|
---|
| 120 | break;
|
---|
| 121 |
|
---|
| 122 | case '?':
|
---|
| 123 | errflg++;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | if (errflg) {
|
---|
| 127 | fprintf(stderr, "usage: . . . ");
|
---|
| 128 | exit(2);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | for (; optind < argc; optind++) {
|
---|
| 132 |
|
---|
| 133 | if (access(argv[optind], 4)) {
|
---|
| 134 |
|
---|
| 135 | ...
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | |
---|
| 139 |
|
---|