diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 288 |
1 files changed, 200 insertions, 88 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 index f843881afd..e0dc3701f9 100644 --- a/src/lib/libc/stdlib/getopt.3 +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -9,11 +9,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
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 | 13 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
19 | .\" | 15 | .\" |
@@ -29,20 +25,20 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
31 | .\" | 27 | .\" |
32 | .\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | 28 | .\" $OpenBSD: getopt.3,v 1.31 2005/03/26 22:02:15 millert Exp $ |
33 | .\" | 29 | .\" |
34 | .Dd April 19, 1994 | 30 | .Dd December 17, 2002 |
35 | .Dt GETOPT 3 | 31 | .Dt GETOPT 3 |
36 | .Os BSD 4.3 | 32 | .Os |
37 | .Sh NAME | 33 | .Sh NAME |
38 | .Nm getopt | 34 | .Nm getopt |
39 | .Nd get option character from command line argument list | 35 | .Nd get option character from command line argument list |
40 | .Sh SYNOPSIS | 36 | .Sh SYNOPSIS |
41 | .Fd #include <unistd.h> | 37 | .Fd #include <unistd.h> |
42 | .Vt extern char *optarg; | 38 | .Vt extern char *optarg; |
39 | .Vt extern int opterr; | ||
43 | .Vt extern int optind; | 40 | .Vt extern int optind; |
44 | .Vt extern int optopt; | 41 | .Vt extern int optopt; |
45 | .Vt extern int opterr; | ||
46 | .Vt extern int optreset; | 42 | .Vt extern int optreset; |
47 | .Ft int | 43 | .Ft int |
48 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | 44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" |
@@ -61,20 +57,23 @@ if it has been specified in the string of accepted option characters, | |||
61 | .Pp | 57 | .Pp |
62 | The option string | 58 | The option string |
63 | .Fa optstring | 59 | .Fa optstring |
64 | may contain the following elements: individual characters, and | 60 | may contain the following elements: individual characters, |
65 | characters followed by a colon to indicate an option argument | 61 | characters followed by a colon, and characters followed by two colons. |
66 | is to follow. | 62 | A character followed by a single colon indicates that an argument |
63 | is to follow the option on the command line. | ||
64 | Two colons indicates that the argument is optional \- this is an | ||
65 | extension not covered by POSIX. | ||
67 | For example, an option string | 66 | For example, an option string |
68 | .Li "\&""x"" | 67 | .Qq x |
69 | recognizes an option | 68 | recognizes an option |
70 | .Dq Fl x , | 69 | .Fl x , |
71 | and an option string | 70 | and an option string |
72 | .Li "\&""x:"" | 71 | .Qq Li x: |
73 | recognizes an option and argument | 72 | recognizes an option and argument |
74 | .Dq Fl x Ar argument . | 73 | .Fl x Ar argument . |
75 | It does not matter to | 74 | It does not matter to |
76 | .Fn getopt | 75 | .Fn getopt |
77 | if a following argument has leading white space. | 76 | if a following argument has leading whitespace. |
78 | .Pp | 77 | .Pp |
79 | On return from | 78 | On return from |
80 | .Fn getopt , | 79 | .Fn getopt , |
@@ -87,23 +86,23 @@ contains the index to the next | |||
87 | argument for a subsequent call | 86 | argument for a subsequent call |
88 | to | 87 | to |
89 | .Fn getopt . | 88 | .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 | 89 | .Pp |
97 | The variable | 90 | The variables |
98 | .Va opterr | 91 | .Va opterr |
99 | and | 92 | and |
100 | .Va optind | 93 | .Va optind |
101 | are both initialized to 1. | 94 | are both initialized to 1. |
102 | The | 95 | The |
103 | .Va optind | 96 | .Va optind |
104 | variable may be set to another value before a set of calls to | 97 | variable may be set to another value larger than 0 before a set of calls to |
105 | .Fn getopt | 98 | .Fn getopt |
106 | in order to skip over more or less argv entries. | 99 | in order to skip over more or less |
100 | .Fa argv | ||
101 | entries. | ||
102 | An | ||
103 | .Va optind | ||
104 | value of 0 is reserved for compatibility with GNU | ||
105 | .Fn getopt . | ||
107 | .Pp | 106 | .Pp |
108 | In order to use | 107 | In order to use |
109 | .Fn getopt | 108 | .Fn getopt |
@@ -119,63 +118,62 @@ must be reinitialized. | |||
119 | .Pp | 118 | .Pp |
120 | The | 119 | The |
121 | .Fn getopt | 120 | .Fn getopt |
122 | function | 121 | 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 | 122 | The interpretation of options in the argument list may be cancelled |
127 | by the option | 123 | by the option |
128 | .Ql -- | 124 | .Ql -- |
129 | (double dash) which causes | 125 | (double dash) which causes |
130 | .Fn getopt | 126 | .Fn getopt |
131 | to signal the end of argument processing and returns \-1. | 127 | to signal the end of argument processing and return \-1. |
132 | When all options have been processed (i.e., up to the first non-option | 128 | When all options have been processed (i.e., up to the first non-option |
133 | argument), | 129 | argument), |
134 | .Fn getopt | 130 | .Fn getopt |
135 | returns \-1. | 131 | returns \-1. |
136 | .Sh DIAGNOSTICS | 132 | .Sh RETURN VALUES |
137 | If the | 133 | The |
138 | .Fn getopt | 134 | .Fn getopt |
139 | function encounters a character not found in the string | 135 | function returns the next known option character in |
140 | .Va optarg | 136 | .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 | 137 | If |
150 | .Va optstring | 138 | .Fn getopt |
151 | has a leading | 139 | encounters a character not found in |
152 | .Ql \&: | 140 | .Fa optstring |
153 | then a missing option argument causes a | 141 | or if it detects a missing option argument, |
154 | .Ql \&: | 142 | it returns |
155 | to be returned in addition to suppressing any error messages. | 143 | .Sq \&? |
156 | .Pp | 144 | (question mark). |
157 | Option arguments are allowed to begin with | 145 | If |
158 | .Dq Li \- ; | 146 | .Fa optstring |
159 | this is reasonable but | 147 | has a leading |
160 | reduces the amount of error checking possible. | 148 | .Sq \&: |
161 | .Sh EXTENSIONS | 149 | then a missing option argument causes |
150 | .Sq \&: | ||
151 | to be returned instead of | ||
152 | .Sq \&? . | ||
153 | In either case, the variable | ||
154 | .Va optopt | ||
155 | is set to the character that caused the error. | ||
162 | The | 156 | The |
163 | .Va optreset | ||
164 | variable was added to make it possible to call the | ||
165 | .Fn getopt | 157 | .Fn getopt |
166 | function multiple times. | 158 | function returns \-1 when the argument list is exhausted. |
167 | This is an extension to the | 159 | .Sh ENVIRONMENT |
168 | .St -p1003.2 | 160 | .Bl -tag -width POSIXLY_CORRECTXX |
169 | specification. | 161 | .It Ev POSIXLY_CORRECT |
170 | .Sh EXAMPLE | 162 | If set, a leading |
163 | .Sq - | ||
164 | in | ||
165 | .Ar optstring | ||
166 | is ignored. | ||
167 | .El | ||
168 | .Sh EXAMPLES | ||
171 | .Bd -literal -compact | 169 | .Bd -literal -compact |
172 | extern char *optarg; | 170 | extern char *optarg; |
173 | extern int optind; | 171 | extern int optind; |
174 | int bflag, ch, fd; | 172 | int bflag, ch, fd; |
175 | 173 | ||
176 | bflag = 0; | 174 | bflag = 0; |
177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | 175 | while ((ch = getopt(argc, argv, "bf:")) != -1) { |
178 | switch(ch) { | 176 | switch (ch) { |
179 | case 'b': | 177 | case 'b': |
180 | bflag = 1; | 178 | bflag = 1; |
181 | break; | 179 | break; |
@@ -189,31 +187,90 @@ while ((ch = getopt(argc, argv, "bf:")) != -1) | |||
189 | case '?': | 187 | case '?': |
190 | default: | 188 | default: |
191 | usage(); | 189 | usage(); |
190 | } | ||
192 | } | 191 | } |
193 | argc -= optind; | 192 | argc -= optind; |
194 | argv += optind; | 193 | argv += optind; |
195 | .Ed | 194 | .Ed |
196 | .Sh HISTORY | 195 | .Sh DIAGNOSTICS |
196 | If the | ||
197 | .Fn getopt | ||
198 | function encounters a character not found in the string | ||
199 | .Fa optstring | ||
200 | or detects | ||
201 | a missing option argument it writes an error message to | ||
202 | .Em stderr | ||
203 | and returns | ||
204 | .Ql \&? . | ||
205 | Setting | ||
206 | .Va opterr | ||
207 | to a zero will disable these error messages. | ||
208 | If | ||
209 | .Fa optstring | ||
210 | has a leading | ||
211 | .Ql \&: | ||
212 | then a missing option argument causes a | ||
213 | .Ql \&: | ||
214 | to be returned in addition to suppressing any error messages. | ||
215 | .Pp | ||
216 | Option arguments are allowed to begin with | ||
217 | .Ql - ; | ||
218 | this is reasonable but reduces the amount of error checking possible. | ||
219 | .Sh SEE ALSO | ||
220 | .Xr getopt 1 , | ||
221 | .Xr getopt_long 3 , | ||
222 | .Xr getsubopt 3 | ||
223 | .Sh STANDARDS | ||
197 | The | 224 | The |
198 | .Fn getopt | 225 | .Fn getopt |
199 | function appeared | 226 | function implements a superset of the functionality specified by |
200 | .Bx 4.3 . | 227 | .St -p1003.1 . |
201 | .Sh BUGS | 228 | .Pp |
229 | The following extensions are supported: | ||
230 | .Bl -tag -width "xxx" | ||
231 | .It Li o | ||
202 | The | 232 | The |
233 | .Va optreset | ||
234 | variable was added to make it possible to call the | ||
203 | .Fn getopt | 235 | .Fn getopt |
204 | function was once specified to return | 236 | function multiple times. |
205 | .Dv EOF | 237 | .It Li o |
206 | instead of \-1. | 238 | If the |
207 | This was changed by | 239 | .Va optind |
208 | .St -p1003.2-92 | 240 | variable is set to 0, |
209 | to decouple | ||
210 | .Fn getopt | 241 | .Fn getopt |
211 | from | 242 | will behave as if the |
212 | .Pa <stdio.h> . | 243 | .Va optreset |
213 | .Pp | 244 | variable has been set. |
245 | This is for compatibility with | ||
246 | .Tn GNU | ||
247 | .Fn getopt . | ||
248 | New code should use | ||
249 | .Va optreset | ||
250 | instead. | ||
251 | .It Li o | ||
252 | If the first character of | ||
253 | .Fa optstring | ||
254 | is a plus sign | ||
255 | .Pq Ql + , | ||
256 | it will be ignored. | ||
257 | This is for compatibility with | ||
258 | .Tn GNU | ||
259 | .Fn getopt . | ||
260 | .It Li o | ||
261 | If the first character of | ||
262 | .Fa optstring | ||
263 | is a dash | ||
264 | .Pq Ql - , | ||
265 | non-options will be returned as arguments to the option character | ||
266 | .Ql \e1 . | ||
267 | This is for compatibility with | ||
268 | .Tn GNU | ||
269 | .Fn getopt . | ||
270 | .It Li o | ||
214 | A single dash | 271 | A single dash |
215 | .Dq Li - | 272 | .Pq Ql - |
216 | may be specified as an character in | 273 | may be specified as a character in |
217 | .Fa optstring , | 274 | .Fa optstring , |
218 | however it should | 275 | however it should |
219 | .Em never | 276 | .Em never |
@@ -221,39 +278,94 @@ have an argument associated with it. | |||
221 | This allows | 278 | This allows |
222 | .Fn getopt | 279 | .Fn getopt |
223 | to be used with programs that expect | 280 | to be used with programs that expect |
224 | .Dq Li - | 281 | .Ql - |
225 | as an option flag. | 282 | as an option flag. |
226 | This practice is wrong, and should not be used in any current development. | 283 | This practice is wrong, and should not be used in any current development. |
227 | It is provided for backward compatibility | 284 | It is provided for backward compatibility |
228 | .Em only . | 285 | .Em only . |
286 | Care should be taken not to use | ||
287 | .Ql - | ||
288 | as the first character in | ||
289 | .Fa optstring | ||
290 | to avoid a semantic conflict with | ||
291 | .Tn GNU | ||
292 | .Fn getopt | ||
293 | semantics (see above). | ||
229 | By default, a single dash causes | 294 | By default, a single dash causes |
230 | .Fn getopt | 295 | .Fn getopt |
231 | to return \-1. | 296 | to return \-1. |
232 | This is, we believe, compatible with System V. | 297 | .El |
298 | .Pp | ||
299 | Unlike | ||
300 | .Tn GNU | ||
301 | .Fn getopt , | ||
302 | .Ox | ||
303 | does not permute the argument vector to allow non-options to be | ||
304 | interspersed with options on the command line. | ||
305 | Programs requiring this behavior should use | ||
306 | .Xr getopt_long 3 | ||
307 | instead. | ||
308 | Because of this (and unlike | ||
309 | .Tn GNU ) , | ||
310 | the | ||
311 | .Ox | ||
312 | .Fn getopt | ||
313 | supports optional arguments separated by whitespace. | ||
314 | .Pp | ||
315 | Historic | ||
316 | .Bx | ||
317 | versions of | ||
318 | .Fn getopt | ||
319 | set | ||
320 | .Fa optopt | ||
321 | to the last option character processed. | ||
322 | However, this conflicts with | ||
323 | .St -p1003.1 | ||
324 | which stipulates that | ||
325 | .Fa optopt | ||
326 | be set to the last character that caused an error. | ||
327 | .Sh HISTORY | ||
328 | The | ||
329 | .Fn getopt | ||
330 | function appeared in | ||
331 | .Bx 4.3 . | ||
332 | .Sh BUGS | ||
333 | The | ||
334 | .Fn getopt | ||
335 | function was once specified to return | ||
336 | .Dv EOF | ||
337 | instead of \-1. | ||
338 | This was changed by | ||
339 | .St -p1003.2-92 | ||
340 | to decouple | ||
341 | .Fn getopt | ||
342 | from | ||
343 | .Aq Pa stdio.h . | ||
233 | .Pp | 344 | .Pp |
234 | It is also possible to handle digits as option letters. | 345 | It is possible to handle digits as option letters. |
235 | This allows | 346 | This allows |
236 | .Fn getopt | 347 | .Fn getopt |
237 | to be used with programs that expect a number | 348 | to be used with programs that expect a number |
238 | .Pq Dq Li \&-\&3 | 349 | .Pq Dq Li \-3 |
239 | as an option. | 350 | as an option. |
240 | This practice is wrong, and should not be used in any current development. | 351 | This practice is wrong, and should not be used in any current development. |
241 | It is provided for backward compatibility | 352 | It is provided for backward compatibility |
242 | .Em only . | 353 | .Em only . |
243 | The following code fragment works in most cases. | 354 | The following code fragment works in most cases. |
244 | .Bd -literal -offset indent | 355 | .Bd -literal -offset indent |
245 | int length; | 356 | int ch; |
357 | long length; | ||
246 | char *p; | 358 | char *p; |
247 | 359 | ||
248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | 360 | while ((ch = getopt(argc, argv, "0123456789")) != -1) { |
249 | switch (c) { | 361 | switch (ch) { |
250 | case '0': case '1': case '2': case '3': case '4': | 362 | case '0': case '1': case '2': case '3': case '4': |
251 | case '5': case '6': case '7': case '8': case '9': | 363 | case '5': case '6': case '7': case '8': case '9': |
252 | p = argv[optind - 1]; | 364 | p = argv[optind - 1]; |
253 | if (p[0] == '-' && p[1] == ch && !p[2]) | 365 | if (p[0] == '-' && p[1] == ch && !p[2]) |
254 | length = atoi(++p); | 366 | length = ch - '0'; |
255 | else | 367 | else |
256 | length = atoi(argv[optind] + 1); | 368 | length = strtol(argv[optind] + 1, NULL, 10); |
257 | break; | 369 | break; |
258 | } | 370 | } |
259 | } | 371 | } |