summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/getopt.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
-rw-r--r--src/lib/libc/stdlib/getopt.3317
1 files changed, 215 insertions, 102 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index f843881afd..82f74c2c1a 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.41 2009/04/12 23:13:36 okan Exp $
33.\" 29.\"
34.Dd April 19, 1994 30.Dd $Mdocdate: April 12 2009 $
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,25 @@ if it has been specified in the string of accepted option characters,
61.Pp 57.Pp
62The option string 58The option string
63.Fa optstring 59.Fa optstring
64may contain the following elements: individual characters, and 60may contain the following elements: individual characters,
65characters followed by a colon to indicate an option argument 61characters followed by a colon, and characters followed by two colons.
66is to follow. 62A character followed by a single colon indicates that an argument
63is to follow the option on the command line.
64Two colons indicates that the argument is optional \- this is an
65extension not covered by POSIX.
67For example, an option string 66For example, an option string
68.Li "\&""x"" 67.Qq x
69recognizes an option 68recognizes an option
70.Dq Fl x , 69.Fl x ,
71and an option string 70and an option string
72.Li "\&""x:"" 71.Qq Li x:
73recognizes an option and argument 72recognizes an option and argument
74.Dq Fl x Ar argument . 73.Fl x Ar argument .
75It does not matter to 74It does not matter to
76.Fn getopt 75.Fn getopt
77if a following argument has leading white space. 76if a following argument has leading whitespace; except in the case where
77the argument is optional, denoted with two colons, no leading whitespace
78is permitted.
78.Pp 79.Pp
79On return from 80On return from
80.Fn getopt , 81.Fn getopt ,
@@ -87,23 +88,23 @@ contains the index to the next
87argument for a subsequent call 88argument for a subsequent call
88to 89to
89.Fn getopt . 90.Fn getopt .
90The variable
91.Va optopt
92saves the last
93.Em known
94option character returned by
95.Fn getopt .
96.Pp 91.Pp
97The variable 92The variables
98.Va opterr 93.Va opterr
99and 94and
100.Va optind 95.Va optind
101are both initialized to 1. 96are both initialized to 1.
102The 97The
103.Va optind 98.Va optind
104variable may be set to another value before a set of calls to 99variable may be set to another value larger than 0 before a set of calls to
105.Fn getopt 100.Fn getopt
106in order to skip over more or less argv entries. 101in order to skip over more or less
102.Fa argv
103entries.
104An
105.Va optind
106value of 0 is reserved for compatibility with GNU
107.Fn getopt .
107.Pp 108.Pp
108In order to use 109In order to use
109.Fn getopt 110.Fn getopt
@@ -119,101 +120,163 @@ must be reinitialized.
119.Pp 120.Pp
120The 121The
121.Fn getopt 122.Fn getopt
122function 123function returns \-1 when the argument list is exhausted.
123returns \-1
124when the argument list is exhausted, or a non-recognized
125option is encountered.
126The interpretation of options in the argument list may be cancelled 124The interpretation of options in the argument list may be cancelled
127by the option 125by the option
128.Ql -- 126.Ql --
129(double dash) which causes 127(double dash) which causes
130.Fn getopt 128.Fn getopt
131to signal the end of argument processing and returns \-1. 129to signal the end of argument processing and return \-1.
132When all options have been processed (i.e., up to the first non-option 130When all options have been processed (i.e., up to the first non-option
133argument), 131argument),
134.Fn getopt 132.Fn getopt
135returns \-1. 133returns \-1.
136.Sh DIAGNOSTICS 134.Sh RETURN VALUES
137If the 135The
138.Fn getopt 136.Fn getopt
139function encounters a character not found in the string 137function returns the next known option character in
140.Va optarg 138.Fa optstring .
141or detects
142a missing option argument it writes an error message and returns
143.Ql ?
144to the
145.Em stderr .
146Setting
147.Va opterr
148to a zero will disable these error messages.
149If 139If
150.Va optstring 140.Fn getopt
151has a leading 141encounters a character not found in
152.Ql \&: 142.Fa optstring
153then a missing option argument causes a 143or if it detects a missing option argument,
154.Ql \&: 144it returns
155to be returned in addition to suppressing any error messages. 145.Sq \&?
156.Pp 146(question mark).
157Option arguments are allowed to begin with 147If
158.Dq Li \- ; 148.Fa optstring
159this is reasonable but 149has a leading
160reduces the amount of error checking possible. 150.Sq \&:
161.Sh EXTENSIONS 151then a missing option argument causes
152.Sq \&:
153to be returned instead of
154.Sq \&? .
155In either case, the variable
156.Va optopt
157is set to the character that caused the error.
162The 158The
163.Va optreset
164variable was added to make it possible to call the
165.Fn getopt 159.Fn getopt
166function multiple times. 160function returns \-1 when the argument list is exhausted.
167This is an extension to the 161.Sh ENVIRONMENT
168.St -p1003.2 162.Bl -tag -width POSIXLY_CORRECTXX
169specification. 163.It Ev POSIXLY_CORRECT
170.Sh EXAMPLE 164If set, a leading
171.Bd -literal -compact 165.Sq -
172extern char *optarg; 166in
173extern int optind; 167.Ar optstring
168is ignored.
169.El
170.Sh EXAMPLES
171The following code accepts the options
172.Fl b
173and
174.Fl f Ar argument
175and adjusts
176.Va argc
177and
178.Va argv
179after option argument processing has completed.
180.Bd -literal -offset indent
174int bflag, ch, fd; 181int bflag, ch, fd;
175 182
176bflag = 0; 183bflag = 0;
177while ((ch = getopt(argc, argv, "bf:")) != -1) 184while ((ch = getopt(argc, argv, "bf:")) != -1) {
178 switch(ch) { 185 switch (ch) {
179 case 'b': 186 case 'b':
180 bflag = 1; 187 bflag = 1;
181 break; 188 break;
182 case 'f': 189 case 'f':
183 if ((fd = open(optarg, O_RDONLY, 0)) < 0) { 190 if ((fd = open(optarg, O_RDONLY, 0)) == -1)
184 (void)fprintf(stderr, 191 err(1, "%s", optarg);
185 "myname: %s: %s\en", optarg, strerror(errno));
186 exit(1);
187 }
188 break; 192 break;
189 case '?':
190 default: 193 default:
191 usage(); 194 usage();
195 /* NOTREACHED */
196 }
192} 197}
193argc -= optind; 198argc -= optind;
194argv += optind; 199argv += optind;
195.Ed 200.Ed
196.Sh HISTORY 201.Sh DIAGNOSTICS
202If the
203.Fn getopt
204function encounters a character not found in the string
205.Fa optstring
206or detects
207a missing option argument, it writes an error message to
208.Em stderr
209and returns
210.Ql \&? .
211Setting
212.Va opterr
213to a zero will disable these error messages.
214If
215.Fa optstring
216has a leading
217.Ql \&:
218then a missing option argument causes a
219.Ql \&:
220to be returned in addition to suppressing any error messages.
221.Pp
222Option arguments are allowed to begin with
223.Ql - ;
224this is reasonable but reduces the amount of error checking possible.
225.Sh SEE ALSO
226.Xr getopt 1 ,
227.Xr getopt_long 3 ,
228.Xr getsubopt 3
229.Sh STANDARDS
197The 230The
198.Fn getopt 231.Fn getopt
199function appeared 232function implements a superset of the functionality specified by
200.Bx 4.3 . 233.St -p1003.1 .
201.Sh BUGS 234.Pp
235The following extensions are supported:
236.Bl -tag -width "xxx"
237.It Li o
202The 238The
239.Va optreset
240variable was added to make it possible to call the
203.Fn getopt 241.Fn getopt
204function was once specified to return 242function multiple times.
205.Dv EOF 243.It Li o
206instead of \-1. 244If the
207This was changed by 245.Va optind
208.St -p1003.2-92 246variable is set to 0,
209to decouple
210.Fn getopt 247.Fn getopt
211from 248will behave as if the
212.Pa <stdio.h> . 249.Va optreset
213.Pp 250variable has been set.
251This is for compatibility with
252.Tn GNU
253.Fn getopt .
254New code should use
255.Va optreset
256instead.
257.It Li o
258If the first character of
259.Fa optstring
260is a plus sign
261.Pq Ql + ,
262it will be ignored.
263This is for compatibility with
264.Tn GNU
265.Fn getopt .
266.It Li o
267If the first character of
268.Fa optstring
269is a dash
270.Pq Ql - ,
271non-options will be returned as arguments to the option character
272.Ql \e1 .
273This is for compatibility with
274.Tn GNU
275.Fn getopt .
276.It Li o
214A single dash 277A single dash
215.Dq Li - 278.Pq Ql -
216may be specified as an character in 279may be specified as a character in
217.Fa optstring , 280.Fa optstring ,
218however it should 281however it should
219.Em never 282.Em never
@@ -221,40 +284,90 @@ have an argument associated with it.
221This allows 284This allows
222.Fn getopt 285.Fn getopt
223to be used with programs that expect 286to be used with programs that expect
224.Dq Li - 287.Ql -
225as an option flag. 288as an option flag.
226This practice is wrong, and should not be used in any current development. 289This practice is wrong, and should not be used in any current development.
227It is provided for backward compatibility 290It is provided for backward compatibility
228.Em only . 291.Em only .
292Care should be taken not to use
293.Ql -
294as the first character in
295.Fa optstring
296to avoid a semantic conflict with
297.Tn GNU
298.Fn getopt
299semantics (see above).
229By default, a single dash causes 300By default, a single dash causes
230.Fn getopt 301.Fn getopt
231to return \-1. 302to return \-1.
232This is, we believe, compatible with System V. 303.El
304.Pp
305Historic
306.Bx
307versions of
308.Fn getopt
309set
310.Fa optopt
311to the last option character processed.
312However, this conflicts with
313.St -p1003.1
314which stipulates that
315.Fa optopt
316be set to the last character that caused an error.
317.Sh HISTORY
318The
319.Fn getopt
320function appeared in
321.Bx 4.3 .
322.Sh BUGS
323The
324.Fn getopt
325function was once specified to return
326.Dv EOF
327instead of \-1.
328This was changed by
329.St -p1003.2-92
330to decouple
331.Fn getopt
332from
333.Aq Pa stdio.h .
233.Pp 334.Pp
234It is also possible to handle digits as option letters. 335It is possible to handle digits as option letters.
235This allows 336This allows
236.Fn getopt 337.Fn getopt
237to be used with programs that expect a number 338to be used with programs that expect a number
238.Pq Dq Li \&-\&3 339.Pq Dq Li \-3
239as an option. 340as an option.
240This practice is wrong, and should not be used in any current development. 341This practice is wrong, and should not be used in any current development.
241It is provided for backward compatibility 342It is provided for backward compatibility
242.Em only . 343.Em only .
243The following code fragment works in most cases. 344The following code fragment works in most cases and can handle mixed
345number and letter arguments.
244.Bd -literal -offset indent 346.Bd -literal -offset indent
245int length; 347int aflag = 0, bflag = 0, ch, lastch = '\e0';
246char *p; 348int length = -1, newarg = 1, prevoptind = 1;
247 349
248while ((c = getopt(argc, argv, "0123456789")) != -1) 350while ((ch = getopt(argc, argv, "0123456789ab")) != -1) {
249 switch (c) { 351 switch (ch) {
250 case '0': case '1': case '2': case '3': case '4': 352 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9': 353 case '5': case '6': case '7': case '8': case '9':
252 p = argv[optind - 1]; 354 if (newarg || !isdigit(lastch))
253 if (p[0] == '-' && p[1] == ch && !p[2]) 355 length = 0;
254 length = atoi(++p); 356 else if (length > INT_MAX / 10)
255 else 357 usage();
256 length = atoi(argv[optind] + 1); 358 length = (length * 10) + (ch - '0');
257 break; 359 break;
360 case 'a':
361 aflag = 1;
362 break;
363 case 'b':
364 bflag = 1;
365 break;
366 default:
367 usage();
258 } 368 }
369 lastch = ch;
370 newarg = optind != prevoptind;
371 prevoptind = optind;
259} 372}
260.Ed 373.Ed