diff options
author | cvs2svn <admin@example.com> | 2025-04-14 17:32:06 +0000 |
---|---|---|
committer | cvs2svn <admin@example.com> | 2025-04-14 17:32:06 +0000 |
commit | eb8dd9dca1228af0cd132f515509051ecfabf6f6 (patch) | |
tree | edb6da6af7e865d488dc1a29309f1e1ec226e603 /src/lib/libc/stdlib/getopt.3 | |
parent | 247f0352e0ed72a4f476db9dc91f4d982bc83eb2 (diff) | |
download | openbsd-tb_20250414.tar.gz openbsd-tb_20250414.tar.bz2 openbsd-tb_20250414.zip |
This commit was manufactured by cvs2git to create tag 'tb_20250414'.tb_20250414
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 365 |
1 files changed, 0 insertions, 365 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 deleted file mode 100644 index c7e28ff91f..0000000000 --- a/src/lib/libc/stdlib/getopt.3 +++ /dev/null | |||
@@ -1,365 +0,0 @@ | |||
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. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: getopt.3,v 1.48 2022/07/25 02:25:55 jsg Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: July 25 2022 $ | ||
31 | .Dt GETOPT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getopt | ||
35 | .Nd get option character from command line argument list | ||
36 | .Sh SYNOPSIS | ||
37 | .In unistd.h | ||
38 | .Vt extern char *optarg; | ||
39 | .Vt extern int opterr; | ||
40 | .Vt extern int optind; | ||
41 | .Vt extern int optopt; | ||
42 | .Vt extern int optreset; | ||
43 | .Ft int | ||
44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | ||
45 | .Sh DESCRIPTION | ||
46 | The | ||
47 | .Fn getopt | ||
48 | function incrementally parses a command line argument list | ||
49 | .Fa argv | ||
50 | and returns the next | ||
51 | .Em known | ||
52 | option character. | ||
53 | An option character is | ||
54 | .Em known | ||
55 | if it has been specified in the string of accepted option characters, | ||
56 | .Fa optstring . | ||
57 | .Pp | ||
58 | The option string | ||
59 | .Fa optstring | ||
60 | may contain the following elements: individual characters, | ||
61 | characters followed by a colon, and characters followed by two colons. | ||
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. | ||
66 | For example, an option string | ||
67 | .Qq x | ||
68 | recognizes an option | ||
69 | .Fl x , | ||
70 | and an option string | ||
71 | .Qq Li x: | ||
72 | recognizes an option and argument | ||
73 | .Fl x Ar argument . | ||
74 | It does not matter to | ||
75 | .Fn getopt | ||
76 | if a following argument has leading whitespace; except in the case where | ||
77 | the argument is optional, denoted with two colons, no leading whitespace | ||
78 | is permitted. | ||
79 | .Pp | ||
80 | On return from | ||
81 | .Fn getopt , | ||
82 | .Va optarg | ||
83 | points to an option argument, if it is anticipated, | ||
84 | and the variable | ||
85 | .Va optind | ||
86 | contains the index to the next | ||
87 | .Fa argv | ||
88 | argument for a subsequent call | ||
89 | to | ||
90 | .Fn getopt . | ||
91 | .Pp | ||
92 | The variables | ||
93 | .Va opterr | ||
94 | and | ||
95 | .Va optind | ||
96 | are both initialized to 1. | ||
97 | The | ||
98 | .Va optind | ||
99 | variable may be set to another value larger than 0 before a set of calls to | ||
100 | .Fn getopt | ||
101 | in order to skip over more or less | ||
102 | .Fa argv | ||
103 | entries. | ||
104 | An | ||
105 | .Va optind | ||
106 | value of 0 is reserved for compatibility with GNU | ||
107 | .Fn getopt . | ||
108 | .Pp | ||
109 | In order to use | ||
110 | .Fn getopt | ||
111 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
112 | arguments multiple times, | ||
113 | the variable | ||
114 | .Va optreset | ||
115 | must be set to 1 before the second and each additional set of calls to | ||
116 | .Fn getopt , | ||
117 | and the variable | ||
118 | .Va optind | ||
119 | must be reinitialized. | ||
120 | .Pp | ||
121 | The | ||
122 | .Fn getopt | ||
123 | function returns \-1 when the argument list is exhausted. | ||
124 | The interpretation of options in the argument list may be cancelled | ||
125 | by the option | ||
126 | .Ql -- | ||
127 | (double dash) which causes | ||
128 | .Fn getopt | ||
129 | to signal the end of argument processing and return \-1. | ||
130 | When all options have been processed (i.e., up to the first non-option | ||
131 | argument), | ||
132 | .Fn getopt | ||
133 | returns \-1. | ||
134 | .Sh RETURN VALUES | ||
135 | The | ||
136 | .Fn getopt | ||
137 | function returns the next known option character in | ||
138 | .Fa optstring . | ||
139 | If | ||
140 | .Fn getopt | ||
141 | encounters a character not found in | ||
142 | .Fa optstring | ||
143 | or if it detects a missing option argument, | ||
144 | it returns | ||
145 | .Sq \&? | ||
146 | (question mark). | ||
147 | If | ||
148 | .Fa optstring | ||
149 | has a leading | ||
150 | .Sq \&: | ||
151 | then a missing option argument causes | ||
152 | .Sq \&: | ||
153 | to be returned instead of | ||
154 | .Sq \&? . | ||
155 | In either case, the variable | ||
156 | .Va optopt | ||
157 | is set to the character that caused the error. | ||
158 | The | ||
159 | .Fn getopt | ||
160 | function returns \-1 when the argument list is exhausted. | ||
161 | .Sh EXAMPLES | ||
162 | The following code accepts the options | ||
163 | .Fl b | ||
164 | and | ||
165 | .Fl f Ar argument | ||
166 | and adjusts | ||
167 | .Va argc | ||
168 | and | ||
169 | .Va argv | ||
170 | after option argument processing has completed. | ||
171 | .Bd -literal -offset indent | ||
172 | int bflag, ch, fd; | ||
173 | |||
174 | bflag = 0; | ||
175 | while ((ch = getopt(argc, argv, "bf:")) != -1) { | ||
176 | switch (ch) { | ||
177 | case 'b': | ||
178 | bflag = 1; | ||
179 | break; | ||
180 | case 'f': | ||
181 | if ((fd = open(optarg, O_RDONLY)) == -1) | ||
182 | err(1, "%s", optarg); | ||
183 | break; | ||
184 | default: | ||
185 | usage(); | ||
186 | } | ||
187 | } | ||
188 | argc -= optind; | ||
189 | argv += optind; | ||
190 | .Ed | ||
191 | .Sh DIAGNOSTICS | ||
192 | If the | ||
193 | .Fn getopt | ||
194 | function encounters a character not found in the string | ||
195 | .Fa optstring | ||
196 | or detects | ||
197 | a missing option argument, it writes an error message to | ||
198 | .Em stderr | ||
199 | and returns | ||
200 | .Ql \&? . | ||
201 | Setting | ||
202 | .Va opterr | ||
203 | to a zero will disable these error messages. | ||
204 | If | ||
205 | .Fa optstring | ||
206 | has a leading | ||
207 | .Ql \&: | ||
208 | then a missing option argument causes a | ||
209 | .Ql \&: | ||
210 | to be returned in addition to suppressing any error messages. | ||
211 | .Pp | ||
212 | Option arguments are allowed to begin with | ||
213 | .Ql - ; | ||
214 | this is reasonable but reduces the amount of error checking possible. | ||
215 | .Sh SEE ALSO | ||
216 | .Xr getopt 1 , | ||
217 | .Xr getopt_long 3 , | ||
218 | .Xr getsubopt 3 | ||
219 | .Sh STANDARDS | ||
220 | The | ||
221 | .Fn getopt | ||
222 | function implements a superset of the functionality specified by | ||
223 | .St -p1003.1 . | ||
224 | .Pp | ||
225 | The following extensions are supported: | ||
226 | .Bl -bullet | ||
227 | .It | ||
228 | The | ||
229 | .Va optreset | ||
230 | variable was added to make it possible to call the | ||
231 | .Fn getopt | ||
232 | function multiple times. | ||
233 | .It | ||
234 | If the | ||
235 | .Va optind | ||
236 | variable is set to 0, | ||
237 | .Fn getopt | ||
238 | will behave as if the | ||
239 | .Va optreset | ||
240 | variable has been set. | ||
241 | This is for compatibility with | ||
242 | .Tn GNU | ||
243 | .Fn getopt . | ||
244 | New code should use | ||
245 | .Va optreset | ||
246 | instead. | ||
247 | .It | ||
248 | If the first character of | ||
249 | .Fa optstring | ||
250 | is a plus sign | ||
251 | .Pq Ql + , | ||
252 | it will be ignored. | ||
253 | This is for compatibility with | ||
254 | .Tn GNU | ||
255 | .Fn getopt . | ||
256 | .It | ||
257 | If the first character of | ||
258 | .Fa optstring | ||
259 | is a dash | ||
260 | .Pq Ql - , | ||
261 | non-options will be returned as arguments to the option character | ||
262 | .Ql \e1 . | ||
263 | This is for compatibility with | ||
264 | .Tn GNU | ||
265 | .Fn getopt . | ||
266 | .It | ||
267 | A single dash | ||
268 | .Pq Ql - | ||
269 | may be specified as a character in | ||
270 | .Fa optstring , | ||
271 | however it should | ||
272 | .Em never | ||
273 | have an argument associated with it. | ||
274 | This allows | ||
275 | .Fn getopt | ||
276 | to be used with programs that expect | ||
277 | .Ql - | ||
278 | as an option flag. | ||
279 | This practice is wrong, and should not be used in any current development. | ||
280 | It is provided for backward compatibility | ||
281 | .Em only . | ||
282 | Care should be taken not to use | ||
283 | .Ql - | ||
284 | as the first character in | ||
285 | .Fa optstring | ||
286 | to avoid a semantic conflict with | ||
287 | .Tn GNU | ||
288 | .Fn getopt | ||
289 | semantics (see above). | ||
290 | By default, a single dash causes | ||
291 | .Fn getopt | ||
292 | to return \-1. | ||
293 | .El | ||
294 | .Pp | ||
295 | Historic | ||
296 | .Bx | ||
297 | versions of | ||
298 | .Fn getopt | ||
299 | set | ||
300 | .Fa optopt | ||
301 | to the last option character processed. | ||
302 | However, this conflicts with | ||
303 | .St -p1003.1 | ||
304 | which stipulates that | ||
305 | .Fa optopt | ||
306 | be set to the last character that caused an error. | ||
307 | .Sh HISTORY | ||
308 | The | ||
309 | .Fn getopt | ||
310 | function first appeared in | ||
311 | .At III | ||
312 | and was reimplemented for | ||
313 | .Bx 4.3 . | ||
314 | .Sh BUGS | ||
315 | The | ||
316 | .Fn getopt | ||
317 | function was once specified to return | ||
318 | .Dv EOF | ||
319 | instead of \-1. | ||
320 | This was changed by | ||
321 | .St -p1003.2-92 | ||
322 | to decouple | ||
323 | .Fn getopt | ||
324 | from | ||
325 | .In stdio.h . | ||
326 | .Pp | ||
327 | It is possible to handle digits as option letters. | ||
328 | This allows | ||
329 | .Fn getopt | ||
330 | to be used with programs that expect a number | ||
331 | .Pq Dq Li \-3 | ||
332 | as an option. | ||
333 | This practice is wrong, and should not be used in any current development. | ||
334 | It is provided for backward compatibility | ||
335 | .Em only . | ||
336 | The following code fragment works in most cases and can handle mixed | ||
337 | number and letter arguments. | ||
338 | .Bd -literal -offset indent | ||
339 | int aflag = 0, bflag = 0, ch, lastch = '\e0'; | ||
340 | int length = -1, newarg = 1, prevoptind = 1; | ||
341 | |||
342 | while ((ch = getopt(argc, argv, "0123456789ab")) != -1) { | ||
343 | switch (ch) { | ||
344 | case '0': case '1': case '2': case '3': case '4': | ||
345 | case '5': case '6': case '7': case '8': case '9': | ||
346 | if (newarg || !isdigit(lastch)) | ||
347 | length = 0; | ||
348 | else if (length > INT_MAX / 10) | ||
349 | usage(); | ||
350 | length = (length * 10) + (ch - '0'); | ||
351 | break; | ||
352 | case 'a': | ||
353 | aflag = 1; | ||
354 | break; | ||
355 | case 'b': | ||
356 | bflag = 1; | ||
357 | break; | ||
358 | default: | ||
359 | usage(); | ||
360 | } | ||
361 | lastch = ch; | ||
362 | newarg = optind != prevoptind; | ||
363 | prevoptind = optind; | ||
364 | } | ||
365 | .Ed | ||