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.3372
1 files changed, 372 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..e0dc3701f9
--- /dev/null
+++ b/src/lib/libc/stdlib/getopt.3
@@ -0,0 +1,372 @@
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.31 2005/03/26 22:02:15 millert Exp $
29.\"
30.Dd December 17, 2002
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.Fd #include <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
46The
47.Fn getopt
48function incrementally parses a command line argument list
49.Fa argv
50and returns the next
51.Em known
52option character.
53An option character is
54.Em known
55if it has been specified in the string of accepted option characters,
56.Fa optstring .
57.Pp
58The option string
59.Fa optstring
60may contain the following elements: individual characters,
61characters followed by a colon, and characters followed by two colons.
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.
66For example, an option string
67.Qq x
68recognizes an option
69.Fl x ,
70and an option string
71.Qq Li x:
72recognizes an option and argument
73.Fl x Ar argument .
74It does not matter to
75.Fn getopt
76if a following argument has leading whitespace.
77.Pp
78On return from
79.Fn getopt ,
80.Va optarg
81points to an option argument, if it is anticipated,
82and the variable
83.Va optind
84contains the index to the next
85.Fa argv
86argument for a subsequent call
87to
88.Fn getopt .
89.Pp
90The variables
91.Va opterr
92and
93.Va optind
94are both initialized to 1.
95The
96.Va optind
97variable may be set to another value larger than 0 before a set of calls to
98.Fn getopt
99in order to skip over more or less
100.Fa argv
101entries.
102An
103.Va optind
104value of 0 is reserved for compatibility with GNU
105.Fn getopt .
106.Pp
107In order to use
108.Fn getopt
109to evaluate multiple sets of arguments, or to evaluate a single set of
110arguments multiple times,
111the variable
112.Va optreset
113must be set to 1 before the second and each additional set of calls to
114.Fn getopt ,
115and the variable
116.Va optind
117must be reinitialized.
118.Pp
119The
120.Fn getopt
121function returns \-1 when the argument list is exhausted.
122The interpretation of options in the argument list may be cancelled
123by the option
124.Ql --
125(double dash) which causes
126.Fn getopt
127to signal the end of argument processing and return \-1.
128When all options have been processed (i.e., up to the first non-option
129argument),
130.Fn getopt
131returns \-1.
132.Sh RETURN VALUES
133The
134.Fn getopt
135function returns the next known option character in
136.Fa optstring .
137If
138.Fn getopt
139encounters a character not found in
140.Fa optstring
141or if it detects a missing option argument,
142it returns
143.Sq \&?
144(question mark).
145If
146.Fa optstring
147has a leading
148.Sq \&:
149then a missing option argument causes
150.Sq \&:
151to be returned instead of
152.Sq \&? .
153In either case, the variable
154.Va optopt
155is set to the character that caused the error.
156The
157.Fn getopt
158function returns \-1 when the argument list is exhausted.
159.Sh ENVIRONMENT
160.Bl -tag -width POSIXLY_CORRECTXX
161.It Ev POSIXLY_CORRECT
162If set, a leading
163.Sq -
164in
165.Ar optstring
166is ignored.
167.El
168.Sh EXAMPLES
169.Bd -literal -compact
170extern char *optarg;
171extern int optind;
172int bflag, ch, fd;
173
174bflag = 0;
175while ((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, 0)) < 0) {
182 (void)fprintf(stderr,
183 "myname: %s: %s\en", optarg, strerror(errno));
184 exit(1);
185 }
186 break;
187 case '?':
188 default:
189 usage();
190 }
191}
192argc -= optind;
193argv += optind;
194.Ed
195.Sh DIAGNOSTICS
196If the
197.Fn getopt
198function encounters a character not found in the string
199.Fa optstring
200or detects
201a missing option argument it writes an error message to
202.Em stderr
203and returns
204.Ql \&? .
205Setting
206.Va opterr
207to a zero will disable these error messages.
208If
209.Fa optstring
210has a leading
211.Ql \&:
212then a missing option argument causes a
213.Ql \&:
214to be returned in addition to suppressing any error messages.
215.Pp
216Option arguments are allowed to begin with
217.Ql - ;
218this 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
224The
225.Fn getopt
226function implements a superset of the functionality specified by
227.St -p1003.1 .
228.Pp
229The following extensions are supported:
230.Bl -tag -width "xxx"
231.It Li o
232The
233.Va optreset
234variable was added to make it possible to call the
235.Fn getopt
236function multiple times.
237.It Li o
238If the
239.Va optind
240variable is set to 0,
241.Fn getopt
242will behave as if the
243.Va optreset
244variable has been set.
245This is for compatibility with
246.Tn GNU
247.Fn getopt .
248New code should use
249.Va optreset
250instead.
251.It Li o
252If the first character of
253.Fa optstring
254is a plus sign
255.Pq Ql + ,
256it will be ignored.
257This is for compatibility with
258.Tn GNU
259.Fn getopt .
260.It Li o
261If the first character of
262.Fa optstring
263is a dash
264.Pq Ql - ,
265non-options will be returned as arguments to the option character
266.Ql \e1 .
267This is for compatibility with
268.Tn GNU
269.Fn getopt .
270.It Li o
271A single dash
272.Pq Ql -
273may be specified as a character in
274.Fa optstring ,
275however it should
276.Em never
277have an argument associated with it.
278This allows
279.Fn getopt
280to be used with programs that expect
281.Ql -
282as an option flag.
283This practice is wrong, and should not be used in any current development.
284It is provided for backward compatibility
285.Em only .
286Care should be taken not to use
287.Ql -
288as the first character in
289.Fa optstring
290to avoid a semantic conflict with
291.Tn GNU
292.Fn getopt
293semantics (see above).
294By default, a single dash causes
295.Fn getopt
296to return \-1.
297.El
298.Pp
299Unlike
300.Tn GNU
301.Fn getopt ,
302.Ox
303does not permute the argument vector to allow non-options to be
304interspersed with options on the command line.
305Programs requiring this behavior should use
306.Xr getopt_long 3
307instead.
308Because of this (and unlike
309.Tn GNU ) ,
310the
311.Ox
312.Fn getopt
313supports optional arguments separated by whitespace.
314.Pp
315Historic
316.Bx
317versions of
318.Fn getopt
319set
320.Fa optopt
321to the last option character processed.
322However, this conflicts with
323.St -p1003.1
324which stipulates that
325.Fa optopt
326be set to the last character that caused an error.
327.Sh HISTORY
328The
329.Fn getopt
330function appeared in
331.Bx 4.3 .
332.Sh BUGS
333The
334.Fn getopt
335function was once specified to return
336.Dv EOF
337instead of \-1.
338This was changed by
339.St -p1003.2-92
340to decouple
341.Fn getopt
342from
343.Aq Pa stdio.h .
344.Pp
345It is possible to handle digits as option letters.
346This allows
347.Fn getopt
348to be used with programs that expect a number
349.Pq Dq Li \-3
350as an option.
351This practice is wrong, and should not be used in any current development.
352It is provided for backward compatibility
353.Em only .
354The following code fragment works in most cases.
355.Bd -literal -offset indent
356int ch;
357long length;
358char *p;
359
360while ((ch = getopt(argc, argv, "0123456789")) != -1) {
361 switch (ch) {
362 case '0': case '1': case '2': case '3': case '4':
363 case '5': case '6': case '7': case '8': case '9':
364 p = argv[optind - 1];
365 if (p[0] == '-' && p[1] == ch && !p[2])
366 length = ch - '0';
367 else
368 length = strtol(argv[optind] + 1, NULL, 10);
369 break;
370 }
371}
372.Ed