diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
| -rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 254 |
1 files changed, 254 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 new file mode 100644 index 0000000000..4acbe69606 --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
| @@ -0,0 +1,254 @@ | |||
| 1 | .\" Copyright (c) 1988, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 13 | .\" must display the following acknowledgement: | ||
| 14 | .\" This product includes software developed by the University of | ||
| 15 | .\" California, Berkeley and its contributors. | ||
| 16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 17 | .\" may be used to endorse or promote products derived from this software | ||
| 18 | .\" without specific prior written permission. | ||
| 19 | .\" | ||
| 20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 30 | .\" SUCH DAMAGE. | ||
| 31 | .\" | ||
| 32 | .\" $OpenBSD: getopt.3,v 1.17 2002/08/19 22:29:52 miod Exp $ | ||
| 33 | .\" | ||
| 34 | .Dd April 19, 1994 | ||
| 35 | .Dt GETOPT 3 | ||
| 36 | .Os | ||
| 37 | .Sh NAME | ||
| 38 | .Nm getopt | ||
| 39 | .Nd get option character from command line argument list | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .Fd #include <unistd.h> | ||
| 42 | .Vt extern char *optarg; | ||
| 43 | .Vt extern int optind; | ||
| 44 | .Vt extern int optopt; | ||
| 45 | .Vt extern int opterr; | ||
| 46 | .Vt extern int optreset; | ||
| 47 | .Ft int | ||
| 48 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn getopt | ||
| 52 | function incrementally parses a command line argument list | ||
| 53 | .Fa argv | ||
| 54 | and returns the next known option character. | ||
| 55 | An option character is | ||
| 56 | .Dq known | ||
| 57 | if it has been specified in the string of accepted option characters, | ||
| 58 | .Fa optstring . | ||
| 59 | .Pp | ||
| 60 | The option string | ||
| 61 | .Fa optstring | ||
| 62 | may contain the following elements: individual characters and | ||
| 63 | characters followed by a colon to indicate an option argument | ||
| 64 | is to follow. | ||
| 65 | For example, an option string | ||
| 66 | .Qq x | ||
| 67 | recognizes an option | ||
| 68 | .Fl x , | ||
| 69 | and an option string | ||
| 70 | .Qq Li x: | ||
| 71 | recognizes an option and argument | ||
| 72 | .Fl x Ar argument . | ||
| 73 | It does not matter to | ||
| 74 | .Fn getopt | ||
| 75 | if a following argument has leading whitespace. | ||
| 76 | .Pp | ||
| 77 | On return from | ||
| 78 | .Fn getopt , | ||
| 79 | .Va optarg | ||
| 80 | points to an option argument, if it is anticipated, | ||
| 81 | and the variable | ||
| 82 | .Va optind | ||
| 83 | contains the index to the next | ||
| 84 | .Fa argv | ||
| 85 | argument for a subsequent call | ||
| 86 | to | ||
| 87 | .Fn getopt . | ||
| 88 | The variable | ||
| 89 | .Va optopt | ||
| 90 | saves the last known option character returned by | ||
| 91 | .Fn getopt . | ||
| 92 | .Pp | ||
| 93 | The variables | ||
| 94 | .Va opterr | ||
| 95 | and | ||
| 96 | .Va optind | ||
| 97 | are both initialized to 1. | ||
| 98 | The | ||
| 99 | .Va optind | ||
| 100 | variable may be set to another value before a set of calls to | ||
| 101 | .Fn getopt | ||
| 102 | in order to skip over more or less argv entries. | ||
| 103 | .Pp | ||
| 104 | In order to use | ||
| 105 | .Fn getopt | ||
| 106 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
| 107 | arguments multiple times, | ||
| 108 | the variable | ||
| 109 | .Va optreset | ||
| 110 | must be set to 1 before the second and each additional set of calls to | ||
| 111 | .Fn getopt , | ||
| 112 | and the variable | ||
| 113 | .Va optind | ||
| 114 | must be reinitialized. | ||
| 115 | .Pp | ||
| 116 | The | ||
| 117 | .Fn getopt | ||
| 118 | function returns \-1 when the argument list is exhausted. | ||
| 119 | The interpretation of options in the argument list may be cancelled | ||
| 120 | by the option | ||
| 121 | .Ql -- | ||
| 122 | (double dash) which causes | ||
| 123 | .Fn getopt | ||
| 124 | to signal the end of argument processing and returns \-1. | ||
| 125 | When all options have been processed (i.e., up to the first non-option | ||
| 126 | argument), | ||
| 127 | .Fn getopt | ||
| 128 | returns \-1. | ||
| 129 | .Sh EXAMPLES | ||
| 130 | .Bd -literal -compact | ||
| 131 | int bflag, ch, fd; | ||
| 132 | |||
| 133 | bflag = 0; | ||
| 134 | while ((ch = getopt(argc, argv, "bf:")) != -1) { | ||
| 135 | switch (ch) { | ||
| 136 | case 'b': | ||
| 137 | bflag = 1; | ||
| 138 | break; | ||
| 139 | case 'f': | ||
| 140 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | ||
| 141 | (void)fprintf(stderr, | ||
| 142 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
| 143 | exit(1); | ||
| 144 | } | ||
| 145 | break; | ||
| 146 | case '?': | ||
| 147 | default: | ||
| 148 | usage(); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | argc -= optind; | ||
| 152 | argv += optind; | ||
| 153 | .Ed | ||
| 154 | .Sh SEE ALSO | ||
| 155 | .Xr getopt 1 , | ||
| 156 | .Xr getsubopt 3 | ||
| 157 | .Sh DIAGNOSTICS | ||
| 158 | If the | ||
| 159 | .Fn getopt | ||
| 160 | function encounters a character not found in the string | ||
| 161 | .Va optstring | ||
| 162 | or detects | ||
| 163 | a missing option argument it writes an error message to | ||
| 164 | .Em stderr | ||
| 165 | and returns | ||
| 166 | .Ql ? . | ||
| 167 | Setting | ||
| 168 | .Va opterr | ||
| 169 | to a zero will disable these error messages. | ||
| 170 | If | ||
| 171 | .Va optstring | ||
| 172 | has a leading | ||
| 173 | .Ql \&: | ||
| 174 | then a missing option argument causes a | ||
| 175 | .Ql \&: | ||
| 176 | to be returned in addition to suppressing any error messages. | ||
| 177 | .Pp | ||
| 178 | Option arguments are allowed to begin with | ||
| 179 | .Ql - ; | ||
| 180 | this is reasonable but reduces the amount of error checking possible. | ||
| 181 | .Sh EXTENSIONS | ||
| 182 | The | ||
| 183 | .Va optreset | ||
| 184 | variable was added to make it possible to call the | ||
| 185 | .Fn getopt | ||
| 186 | function multiple times. | ||
| 187 | This is an extension to the | ||
| 188 | .St -p1003.2 | ||
| 189 | specification. | ||
| 190 | .Sh HISTORY | ||
| 191 | The | ||
| 192 | .Fn getopt | ||
| 193 | function appeared in | ||
| 194 | .Bx 4.3 . | ||
| 195 | .Sh BUGS | ||
| 196 | The | ||
| 197 | .Fn getopt | ||
| 198 | function was once specified to return | ||
| 199 | .Dv EOF | ||
| 200 | instead of \-1. | ||
| 201 | This was changed by | ||
| 202 | .St -p1003.2-92 | ||
| 203 | to decouple | ||
| 204 | .Fn getopt | ||
| 205 | from | ||
| 206 | .Pa <stdio.h> . | ||
| 207 | .Pp | ||
| 208 | A single dash | ||
| 209 | .Pq Ql - | ||
| 210 | may be specified as a character in | ||
| 211 | .Fa optstring , | ||
| 212 | however it should | ||
| 213 | .Em never | ||
| 214 | have an argument associated with it. | ||
| 215 | This allows | ||
| 216 | .Fn getopt | ||
| 217 | to be used with programs that expect | ||
| 218 | .Ql - | ||
| 219 | as an option flag. | ||
| 220 | This practice is wrong, and should not be used in any current development. | ||
| 221 | It is provided for backward compatibility | ||
| 222 | .Em only . | ||
| 223 | By default, a single dash causes | ||
| 224 | .Fn getopt | ||
| 225 | to return \-1. | ||
| 226 | This is, we believe, compatible with System V. | ||
| 227 | .Pp | ||
| 228 | It is also possible to handle digits as option letters. | ||
| 229 | This allows | ||
| 230 | .Fn getopt | ||
| 231 | to be used with programs that expect a number | ||
| 232 | .Pq Dq Li \&-\&3 | ||
| 233 | as an option. | ||
| 234 | This practice is wrong, and should not be used in any current development. | ||
| 235 | It is provided for backward compatibility | ||
| 236 | .Em only . | ||
| 237 | The following code fragment works in most cases. | ||
| 238 | .Bd -literal -offset indent | ||
| 239 | long length; | ||
| 240 | char *p; | ||
| 241 | |||
| 242 | while ((c = getopt(argc, argv, "0123456789")) != -1) { | ||
| 243 | switch (c) { | ||
| 244 | case '0': case '1': case '2': case '3': case '4': | ||
| 245 | case '5': case '6': case '7': case '8': case '9': | ||
| 246 | p = argv[optind - 1]; | ||
| 247 | if (p[0] == '-' && p[1] == ch && !p[2]) | ||
| 248 | length = ch - '0'; | ||
| 249 | else | ||
| 250 | length = strtol(argv[optind] + 1, NULL, 10); | ||
| 251 | break; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | .Ed | ||
