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.3254
1 files changed, 254 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..4acbe69606
--- /dev/null
+++ b/src/lib/libc/stdlib/getopt.3
@@ -0,0 +1,254 @@
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. All advertising materials mentioning features or use of this software
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
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" $OpenBSD: getopt.3,v 1.17 2002/08/19 22:29:52 miod Exp $
33.\"
34.Dd April 19, 1994
35.Dt GETOPT 3
36.Os
37.Sh NAME
38.Nm getopt
39.Nd get option character from command line argument list
40.Sh SYNOPSIS
41.Fd #include <unistd.h>
42.Vt extern char *optarg;
43.Vt extern int optind;
44.Vt extern int optopt;
45.Vt extern int opterr;
46.Vt extern int optreset;
47.Ft int
48.Fn getopt "int argc" "char * const *argv" "const char *optstring"
49.Sh DESCRIPTION
50The
51.Fn getopt
52function incrementally parses a command line argument list
53.Fa argv
54and returns the next known option character.
55An option character is
56.Dq known
57if it has been specified in the string of accepted option characters,
58.Fa optstring .
59.Pp
60The option string
61.Fa optstring
62may contain the following elements: individual characters and
63characters followed by a colon to indicate an option argument
64is to follow.
65For example, an option string
66.Qq x
67recognizes an option
68.Fl x ,
69and an option string
70.Qq Li x:
71recognizes an option and argument
72.Fl x Ar argument .
73It does not matter to
74.Fn getopt
75if a following argument has leading whitespace.
76.Pp
77On return from
78.Fn getopt ,
79.Va optarg
80points to an option argument, if it is anticipated,
81and the variable
82.Va optind
83contains the index to the next
84.Fa argv
85argument for a subsequent call
86to
87.Fn getopt .
88The variable
89.Va optopt
90saves the last known option character returned by
91.Fn getopt .
92.Pp
93The variables
94.Va opterr
95and
96.Va optind
97are both initialized to 1.
98The
99.Va optind
100variable may be set to another value before a set of calls to
101.Fn getopt
102in order to skip over more or less argv entries.
103.Pp
104In order to use
105.Fn getopt
106to evaluate multiple sets of arguments, or to evaluate a single set of
107arguments multiple times,
108the variable
109.Va optreset
110must be set to 1 before the second and each additional set of calls to
111.Fn getopt ,
112and the variable
113.Va optind
114must be reinitialized.
115.Pp
116The
117.Fn getopt
118function returns \-1 when the argument list is exhausted.
119The interpretation of options in the argument list may be cancelled
120by the option
121.Ql --
122(double dash) which causes
123.Fn getopt
124to signal the end of argument processing and returns \-1.
125When all options have been processed (i.e., up to the first non-option
126argument),
127.Fn getopt
128returns \-1.
129.Sh EXAMPLES
130.Bd -literal -compact
131int bflag, ch, fd;
132
133bflag = 0;
134while ((ch = getopt(argc, argv, "bf:")) != -1) {
135 switch (ch) {
136 case 'b':
137 bflag = 1;
138 break;
139 case 'f':
140 if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
141 (void)fprintf(stderr,
142 "myname: %s: %s\en", optarg, strerror(errno));
143 exit(1);
144 }
145 break;
146 case '?':
147 default:
148 usage();
149 }
150}
151argc -= optind;
152argv += optind;
153.Ed
154.Sh SEE ALSO
155.Xr getopt 1 ,
156.Xr getsubopt 3
157.Sh DIAGNOSTICS
158If the
159.Fn getopt
160function encounters a character not found in the string
161.Va optstring
162or detects
163a missing option argument it writes an error message to
164.Em stderr
165and returns
166.Ql ? .
167Setting
168.Va opterr
169to a zero will disable these error messages.
170If
171.Va optstring
172has a leading
173.Ql \&:
174then a missing option argument causes a
175.Ql \&:
176to be returned in addition to suppressing any error messages.
177.Pp
178Option arguments are allowed to begin with
179.Ql - ;
180this is reasonable but reduces the amount of error checking possible.
181.Sh EXTENSIONS
182The
183.Va optreset
184variable was added to make it possible to call the
185.Fn getopt
186function multiple times.
187This is an extension to the
188.St -p1003.2
189specification.
190.Sh HISTORY
191The
192.Fn getopt
193function appeared in
194.Bx 4.3 .
195.Sh BUGS
196The
197.Fn getopt
198function was once specified to return
199.Dv EOF
200instead of \-1.
201This was changed by
202.St -p1003.2-92
203to decouple
204.Fn getopt
205from
206.Pa <stdio.h> .
207.Pp
208A single dash
209.Pq Ql -
210may be specified as a character in
211.Fa optstring ,
212however it should
213.Em never
214have an argument associated with it.
215This allows
216.Fn getopt
217to be used with programs that expect
218.Ql -
219as an option flag.
220This practice is wrong, and should not be used in any current development.
221It is provided for backward compatibility
222.Em only .
223By default, a single dash causes
224.Fn getopt
225to return \-1.
226This is, we believe, compatible with System V.
227.Pp
228It is also possible to handle digits as option letters.
229This allows
230.Fn getopt
231to be used with programs that expect a number
232.Pq Dq Li \&-\&3
233as an option.
234This practice is wrong, and should not be used in any current development.
235It is provided for backward compatibility
236.Em only .
237The following code fragment works in most cases.
238.Bd -literal -offset indent
239long length;
240char *p;
241
242while ((c = getopt(argc, argv, "0123456789")) != -1) {
243 switch (c) {
244 case '0': case '1': case '2': case '3': case '4':
245 case '5': case '6': case '7': case '8': case '9':
246 p = argv[optind - 1];
247 if (p[0] == '-' && p[1] == ch && !p[2])
248 length = ch - '0';
249 else
250 length = strtol(argv[optind] + 1, NULL, 10);
251 break;
252 }
253}
254.Ed