diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
| -rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 167 |
1 files changed, 98 insertions, 69 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 index f843881afd..d25b497035 100644 --- a/src/lib/libc/stdlib/getopt.3 +++ b/src/lib/libc/stdlib/getopt.3 | |||
| @@ -29,11 +29,11 @@ | |||
| 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 30 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
| 31 | .\" | 31 | .\" |
| 32 | .\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | 32 | .\" $OpenBSD: getopt.3,v 1.22 2003/05/10 06:48:30 jmc Exp $ |
| 33 | .\" | 33 | .\" |
| 34 | .Dd April 19, 1994 | 34 | .Dd December 8, 2002 |
| 35 | .Dt GETOPT 3 | 35 | .Dt GETOPT 3 |
| 36 | .Os BSD 4.3 | 36 | .Os |
| 37 | .Sh NAME | 37 | .Sh NAME |
| 38 | .Nm getopt | 38 | .Nm getopt |
| 39 | .Nd get option character from command line argument list | 39 | .Nd get option character from command line argument list |
| @@ -51,30 +51,28 @@ The | |||
| 51 | .Fn getopt | 51 | .Fn getopt |
| 52 | function incrementally parses a command line argument list | 52 | function incrementally parses a command line argument list |
| 53 | .Fa argv | 53 | .Fa argv |
| 54 | and returns the next | 54 | and returns the next known option character. |
| 55 | .Em known | ||
| 56 | option character. | ||
| 57 | An option character is | 55 | An option character is |
| 58 | .Em known | 56 | .Dq known |
| 59 | if it has been specified in the string of accepted option characters, | 57 | if it has been specified in the string of accepted option characters, |
| 60 | .Fa optstring . | 58 | .Fa optstring . |
| 61 | .Pp | 59 | .Pp |
| 62 | The option string | 60 | The option string |
| 63 | .Fa optstring | 61 | .Fa optstring |
| 64 | may contain the following elements: individual characters, and | 62 | may contain the following elements: individual characters and |
| 65 | characters followed by a colon to indicate an option argument | 63 | characters followed by a colon to indicate an option argument |
| 66 | is to follow. | 64 | is to follow. |
| 67 | For example, an option string | 65 | For example, an option string |
| 68 | .Li "\&""x"" | 66 | .Qq x |
| 69 | recognizes an option | 67 | recognizes an option |
| 70 | .Dq Fl x , | 68 | .Fl x , |
| 71 | and an option string | 69 | and an option string |
| 72 | .Li "\&""x:"" | 70 | .Qq Li x: |
| 73 | recognizes an option and argument | 71 | recognizes an option and argument |
| 74 | .Dq Fl x Ar argument . | 72 | .Fl x Ar argument . |
| 75 | It does not matter to | 73 | It does not matter to |
| 76 | .Fn getopt | 74 | .Fn getopt |
| 77 | if a following argument has leading white space. | 75 | if a following argument has leading whitespace. |
| 78 | .Pp | 76 | .Pp |
| 79 | On return from | 77 | On return from |
| 80 | .Fn getopt , | 78 | .Fn getopt , |
| @@ -89,12 +87,10 @@ to | |||
| 89 | .Fn getopt . | 87 | .Fn getopt . |
| 90 | The variable | 88 | The variable |
| 91 | .Va optopt | 89 | .Va optopt |
| 92 | saves the last | 90 | saves the last known option character returned by |
| 93 | .Em known | ||
| 94 | option character returned by | ||
| 95 | .Fn getopt . | 91 | .Fn getopt . |
| 96 | .Pp | 92 | .Pp |
| 97 | The variable | 93 | The variables |
| 98 | .Va opterr | 94 | .Va opterr |
| 99 | and | 95 | and |
| 100 | .Va optind | 96 | .Va optind |
| @@ -119,10 +115,7 @@ must be reinitialized. | |||
| 119 | .Pp | 115 | .Pp |
| 120 | The | 116 | The |
| 121 | .Fn getopt | 117 | .Fn getopt |
| 122 | function | 118 | function returns \-1 when the argument list is exhausted. |
| 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 | 119 | The interpretation of options in the argument list may be cancelled |
| 127 | by the option | 120 | by the option |
| 128 | .Ql -- | 121 | .Ql -- |
| @@ -133,49 +126,36 @@ When all options have been processed (i.e., up to the first non-option | |||
| 133 | argument), | 126 | argument), |
| 134 | .Fn getopt | 127 | .Fn getopt |
| 135 | returns \-1. | 128 | returns \-1. |
| 136 | .Sh DIAGNOSTICS | 129 | .Sh RETURN VALUES |
| 137 | If the | 130 | The |
| 138 | .Fn getopt | 131 | .Fn getopt |
| 139 | function encounters a character not found in the string | 132 | function returns the next known option character in |
| 140 | .Va optarg | 133 | .Fa optstring . |
| 141 | or detects | ||
| 142 | a missing option argument it writes an error message and returns | ||
| 143 | .Ql ? | ||
| 144 | to the | ||
| 145 | .Em stderr . | ||
| 146 | Setting | ||
| 147 | .Va opterr | ||
| 148 | to a zero will disable these error messages. | ||
| 149 | If | 134 | If |
| 150 | .Va optstring | 135 | .Fn getopt |
| 151 | has a leading | 136 | encounters a character not found in |
| 152 | .Ql \&: | 137 | .Fa optstring |
| 153 | then a missing option argument causes a | 138 | or if it detects a missing option argument, |
| 154 | .Ql \&: | 139 | it returns |
| 155 | to be returned in addition to suppressing any error messages. | 140 | .Sq ? . |
| 156 | .Pp | 141 | If |
| 157 | Option arguments are allowed to begin with | 142 | .Fa optstring |
| 158 | .Dq Li \- ; | 143 | has a leading |
| 159 | this is reasonable but | 144 | .Sq \: |
| 160 | reduces the amount of error checking possible. | 145 | then a missing option argument causes |
| 161 | .Sh EXTENSIONS | 146 | .Sq \: |
| 147 | to be returned instead of | ||
| 148 | .Sq ? . | ||
| 162 | The | 149 | The |
| 163 | .Va optreset | ||
| 164 | variable was added to make it possible to call the | ||
| 165 | .Fn getopt | 150 | .Fn getopt |
| 166 | function multiple times. | 151 | function returns \-1 when the argument list is exhausted. |
| 167 | This is an extension to the | 152 | .Sh EXAMPLES |
| 168 | .St -p1003.2 | ||
| 169 | specification. | ||
| 170 | .Sh EXAMPLE | ||
| 171 | .Bd -literal -compact | 153 | .Bd -literal -compact |
| 172 | extern char *optarg; | ||
| 173 | extern int optind; | ||
| 174 | int bflag, ch, fd; | 154 | int bflag, ch, fd; |
| 175 | 155 | ||
| 176 | bflag = 0; | 156 | bflag = 0; |
| 177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | 157 | while ((ch = getopt(argc, argv, "bf:")) != -1) { |
| 178 | switch(ch) { | 158 | switch (ch) { |
| 179 | case 'b': | 159 | case 'b': |
| 180 | bflag = 1; | 160 | bflag = 1; |
| 181 | break; | 161 | break; |
| @@ -189,31 +169,69 @@ while ((ch = getopt(argc, argv, "bf:")) != -1) | |||
| 189 | case '?': | 169 | case '?': |
| 190 | default: | 170 | default: |
| 191 | usage(); | 171 | usage(); |
| 172 | } | ||
| 192 | } | 173 | } |
| 193 | argc -= optind; | 174 | argc -= optind; |
| 194 | argv += optind; | 175 | argv += optind; |
| 195 | .Ed | 176 | .Ed |
| 177 | .Sh SEE ALSO | ||
| 178 | .Xr getopt 1 , | ||
| 179 | .Xr getopt_long 3 , | ||
| 180 | .Xr getsubopt 3 | ||
| 181 | .Sh DIAGNOSTICS | ||
| 182 | If the | ||
| 183 | .Fn getopt | ||
| 184 | function encounters a character not found in the string | ||
| 185 | .Va optstring | ||
| 186 | or detects | ||
| 187 | a missing option argument it writes an error message to | ||
| 188 | .Em stderr | ||
| 189 | and returns | ||
| 190 | .Ql ? . | ||
| 191 | Setting | ||
| 192 | .Va opterr | ||
| 193 | to a zero will disable these error messages. | ||
| 194 | If | ||
| 195 | .Va optstring | ||
| 196 | has a leading | ||
| 197 | .Ql \&: | ||
| 198 | then a missing option argument causes a | ||
| 199 | .Ql \&: | ||
| 200 | to be returned in addition to suppressing any error messages. | ||
| 201 | .Pp | ||
| 202 | Option arguments are allowed to begin with | ||
| 203 | .Ql - ; | ||
| 204 | this is reasonable but reduces the amount of error checking possible. | ||
| 205 | .Sh EXTENSIONS | ||
| 206 | The | ||
| 207 | .Va optreset | ||
| 208 | variable was added to make it possible to call the | ||
| 209 | .Fn getopt | ||
| 210 | function multiple times. | ||
| 211 | This is an extension to the | ||
| 212 | .St -p1003.2 | ||
| 213 | specification. | ||
| 196 | .Sh HISTORY | 214 | .Sh HISTORY |
| 197 | The | 215 | The |
| 198 | .Fn getopt | 216 | .Fn getopt |
| 199 | function appeared | 217 | function appeared in |
| 200 | .Bx 4.3 . | 218 | .Bx 4.3 . |
| 201 | .Sh BUGS | 219 | .Sh BUGS |
| 202 | The | 220 | The |
| 203 | .Fn getopt | 221 | .Fn getopt |
| 204 | function was once specified to return | 222 | function was once specified to return |
| 205 | .Dv EOF | 223 | .Dv EOF |
| 206 | instead of \-1. | 224 | instead of \-1. |
| 207 | This was changed by | 225 | This was changed by |
| 208 | .St -p1003.2-92 | 226 | .St -p1003.2-92 |
| 209 | to decouple | 227 | to decouple |
| 210 | .Fn getopt | 228 | .Fn getopt |
| 211 | from | 229 | from |
| 212 | .Pa <stdio.h> . | 230 | .Pa <stdio.h> . |
| 213 | .Pp | 231 | .Pp |
| 214 | A single dash | 232 | A single dash |
| 215 | .Dq Li - | 233 | .Pq Ql - |
| 216 | may be specified as an character in | 234 | may be specified as a character in |
| 217 | .Fa optstring , | 235 | .Fa optstring , |
| 218 | however it should | 236 | however it should |
| 219 | .Em never | 237 | .Em never |
| @@ -221,15 +239,25 @@ have an argument associated with it. | |||
| 221 | This allows | 239 | This allows |
| 222 | .Fn getopt | 240 | .Fn getopt |
| 223 | to be used with programs that expect | 241 | to be used with programs that expect |
| 224 | .Dq Li - | 242 | .Ql - |
| 225 | as an option flag. | 243 | as an option flag. |
| 226 | This practice is wrong, and should not be used in any current development. | 244 | This practice is wrong, and should not be used in any current development. |
| 227 | It is provided for backward compatibility | 245 | It is provided for backward compatibility |
| 228 | .Em only . | 246 | .Em only . |
| 247 | Care should be taken not to use | ||
| 248 | .Ql - | ||
| 249 | as the first character in | ||
| 250 | .Fa optstring | ||
| 251 | to avoid a semantic conflict with | ||
| 252 | .Tn GNU | ||
| 253 | .Fn getopt , | ||
| 254 | which assigns different meaning to an | ||
| 255 | .Fa optstring | ||
| 256 | that begins with a | ||
| 257 | .Ql - . | ||
| 229 | By default, a single dash causes | 258 | By default, a single dash causes |
| 230 | .Fn getopt | 259 | .Fn getopt |
| 231 | to return \-1. | 260 | to return \-1. |
| 232 | This is, we believe, compatible with System V. | ||
| 233 | .Pp | 261 | .Pp |
| 234 | It is also possible to handle digits as option letters. | 262 | It is also possible to handle digits as option letters. |
| 235 | This allows | 263 | This allows |
| @@ -242,18 +270,19 @@ It is provided for backward compatibility | |||
| 242 | .Em only . | 270 | .Em only . |
| 243 | The following code fragment works in most cases. | 271 | The following code fragment works in most cases. |
| 244 | .Bd -literal -offset indent | 272 | .Bd -literal -offset indent |
| 245 | int length; | 273 | int ch; |
| 274 | long length; | ||
| 246 | char *p; | 275 | char *p; |
| 247 | 276 | ||
| 248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | 277 | while ((ch = getopt(argc, argv, "0123456789")) != -1) { |
| 249 | switch (c) { | 278 | switch (ch) { |
| 250 | case '0': case '1': case '2': case '3': case '4': | 279 | case '0': case '1': case '2': case '3': case '4': |
| 251 | case '5': case '6': case '7': case '8': case '9': | 280 | case '5': case '6': case '7': case '8': case '9': |
| 252 | p = argv[optind - 1]; | 281 | p = argv[optind - 1]; |
| 253 | if (p[0] == '-' && p[1] == ch && !p[2]) | 282 | if (p[0] == '-' && p[1] == ch && !p[2]) |
| 254 | length = atoi(++p); | 283 | length = ch - '0'; |
| 255 | else | 284 | else |
| 256 | length = atoi(argv[optind] + 1); | 285 | length = strtol(argv[optind] + 1, NULL, 10); |
| 257 | break; | 286 | break; |
| 258 | } | 287 | } |
| 259 | } | 288 | } |
