diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
| -rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 261 |
1 files changed, 261 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..4340ff54fd --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
| @@ -0,0 +1,261 @@ | |||
| 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.6 1998/05/05 19:36:03 deraadt Exp $ | ||
| 33 | .\" | ||
| 34 | .Dd April 19, 1994 | ||
| 35 | .Dt GETOPT 3 | ||
| 36 | .Os BSD 4.3 | ||
| 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 | ||
| 55 | .Em known | ||
| 56 | option character. | ||
| 57 | An option character is | ||
| 58 | .Em known | ||
| 59 | if it has been specified in the string of accepted option characters, | ||
| 60 | .Fa optstring . | ||
| 61 | .Pp | ||
| 62 | The option string | ||
| 63 | .Fa optstring | ||
| 64 | may contain the following elements: individual characters, and | ||
| 65 | characters followed by a colon to indicate an option argument | ||
| 66 | is to follow. | ||
| 67 | For example, an option string | ||
| 68 | .Li "\&""x"" | ||
| 69 | recognizes an option | ||
| 70 | .Dq Fl x , | ||
| 71 | and an option string | ||
| 72 | .Li "\&""x:"" | ||
| 73 | recognizes an option and argument | ||
| 74 | .Dq Fl x Ar argument . | ||
| 75 | It does not matter to | ||
| 76 | .Fn getopt | ||
| 77 | if a following argument has leading white space. | ||
| 78 | .Pp | ||
| 79 | On return from | ||
| 80 | .Fn getopt , | ||
| 81 | .Va optarg | ||
| 82 | points to an option argument, if it is anticipated, | ||
| 83 | and the variable | ||
| 84 | .Va optind | ||
| 85 | contains the index to the next | ||
| 86 | .Fa argv | ||
| 87 | argument for a subsequent call | ||
| 88 | to | ||
| 89 | .Fn getopt . | ||
| 90 | The variable | ||
| 91 | .Va optopt | ||
| 92 | saves the last | ||
| 93 | .Em known | ||
| 94 | option character returned by | ||
| 95 | .Fn getopt . | ||
| 96 | .Pp | ||
| 97 | The variable | ||
| 98 | .Va opterr | ||
| 99 | and | ||
| 100 | .Va optind | ||
| 101 | are both initialized to 1. | ||
| 102 | The | ||
| 103 | .Va optind | ||
| 104 | variable may be set to another value before a set of calls to | ||
| 105 | .Fn getopt | ||
| 106 | in order to skip over more or less argv entries. | ||
| 107 | .Pp | ||
| 108 | In order to use | ||
| 109 | .Fn getopt | ||
| 110 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
| 111 | arguments multiple times, | ||
| 112 | the variable | ||
| 113 | .Va optreset | ||
| 114 | must be set to 1 before the second and each additional set of calls to | ||
| 115 | .Fn getopt , | ||
| 116 | and the variable | ||
| 117 | .Va optind | ||
| 118 | must be reinitialized. | ||
| 119 | .Pp | ||
| 120 | The | ||
| 121 | .Fn getopt | ||
| 122 | function | ||
| 123 | returns \-1 | ||
| 124 | when the argument list is exhausted, or a non-recognized | ||
| 125 | option is encountered. | ||
| 126 | The interpretation of options in the argument list may be cancelled | ||
| 127 | by the option | ||
| 128 | .Ql -- | ||
| 129 | (double dash) which causes | ||
| 130 | .Fn getopt | ||
| 131 | to signal the end of argument processing and returns \-1. | ||
| 132 | When all options have been processed (i.e., up to the first non-option | ||
| 133 | argument), | ||
| 134 | .Fn getopt | ||
| 135 | returns \-1. | ||
| 136 | .Sh DIAGNOSTICS | ||
| 137 | If the | ||
| 138 | .Fn getopt | ||
| 139 | function encounters a character not found in the string | ||
| 140 | .Va optstring | ||
| 141 | or detects | ||
| 142 | a missing option argument it writes an error message to | ||
| 143 | .Em stderr | ||
| 144 | and returns | ||
| 145 | .Ql ? . | ||
| 146 | Setting | ||
| 147 | .Va opterr | ||
| 148 | to a zero will disable these error messages. | ||
| 149 | If | ||
| 150 | .Va optstring | ||
| 151 | has a leading | ||
| 152 | .Ql \&: | ||
| 153 | then a missing option argument causes a | ||
| 154 | .Ql \&: | ||
| 155 | to be returned in addition to suppressing any error messages. | ||
| 156 | .Pp | ||
| 157 | Option arguments are allowed to begin with | ||
| 158 | .Dq Li \- ; | ||
| 159 | this is reasonable but | ||
| 160 | reduces the amount of error checking possible. | ||
| 161 | .Sh EXTENSIONS | ||
| 162 | The | ||
| 163 | .Va optreset | ||
| 164 | variable was added to make it possible to call the | ||
| 165 | .Fn getopt | ||
| 166 | function multiple times. | ||
| 167 | This is an extension to the | ||
| 168 | .St -p1003.2 | ||
| 169 | specification. | ||
| 170 | .Sh EXAMPLE | ||
| 171 | .Bd -literal -compact | ||
| 172 | extern char *optarg; | ||
| 173 | extern int optind; | ||
| 174 | int bflag, ch, fd; | ||
| 175 | |||
| 176 | bflag = 0; | ||
| 177 | while ((ch = getopt(argc, argv, "bf:")) != -1) { | ||
| 178 | switch (ch) { | ||
| 179 | case 'b': | ||
| 180 | bflag = 1; | ||
| 181 | break; | ||
| 182 | case 'f': | ||
| 183 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | ||
| 184 | (void)fprintf(stderr, | ||
| 185 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
| 186 | exit(1); | ||
| 187 | } | ||
| 188 | break; | ||
| 189 | case '?': | ||
| 190 | default: | ||
| 191 | usage(); | ||
| 192 | } | ||
| 193 | } | ||
| 194 | argc -= optind; | ||
| 195 | argv += optind; | ||
| 196 | .Ed | ||
| 197 | .Sh HISTORY | ||
| 198 | The | ||
| 199 | .Fn getopt | ||
| 200 | function appeared | ||
| 201 | .Bx 4.3 . | ||
| 202 | .Sh BUGS | ||
| 203 | The | ||
| 204 | .Fn getopt | ||
| 205 | function was once specified to return | ||
| 206 | .Dv EOF | ||
| 207 | instead of \-1. | ||
| 208 | This was changed by | ||
| 209 | .St -p1003.2-92 | ||
| 210 | to decouple | ||
| 211 | .Fn getopt | ||
| 212 | from | ||
| 213 | .Pa <stdio.h> . | ||
| 214 | .Pp | ||
| 215 | A single dash | ||
| 216 | .Dq Li - | ||
| 217 | may be specified as a character in | ||
| 218 | .Fa optstring , | ||
| 219 | however it should | ||
| 220 | .Em never | ||
| 221 | have an argument associated with it. | ||
| 222 | This allows | ||
| 223 | .Fn getopt | ||
| 224 | to be used with programs that expect | ||
| 225 | .Dq Li - | ||
| 226 | as an option flag. | ||
| 227 | This practice is wrong, and should not be used in any current development. | ||
| 228 | It is provided for backward compatibility | ||
| 229 | .Em only . | ||
| 230 | By default, a single dash causes | ||
| 231 | .Fn getopt | ||
| 232 | to return \-1. | ||
| 233 | This is, we believe, compatible with System V. | ||
| 234 | .Pp | ||
| 235 | It is also possible to handle digits as option letters. | ||
| 236 | This allows | ||
| 237 | .Fn getopt | ||
| 238 | to be used with programs that expect a number | ||
| 239 | .Pq Dq Li \&-\&3 | ||
| 240 | as an option. | ||
| 241 | This practice is wrong, and should not be used in any current development. | ||
| 242 | It is provided for backward compatibility | ||
| 243 | .Em only . | ||
| 244 | The following code fragment works in most cases. | ||
| 245 | .Bd -literal -offset indent | ||
| 246 | int length; | ||
| 247 | char *p; | ||
| 248 | |||
| 249 | while ((c = getopt(argc, argv, "0123456789")) != -1) | ||
| 250 | switch (c) { | ||
| 251 | case '0': case '1': case '2': case '3': case '4': | ||
| 252 | case '5': case '6': case '7': case '8': case '9': | ||
| 253 | p = argv[optind - 1]; | ||
| 254 | if (p[0] == '-' && p[1] == ch && !p[2]) | ||
| 255 | length = atoi(++p); | ||
| 256 | else | ||
| 257 | length = atoi(argv[optind] + 1); | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | .Ed | ||
