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.3167
1 files changed, 98 insertions, 69 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index f843881afd..d25b497035 100644
--- a/src/lib/libc/stdlib/getopt.3
+++ b/src/lib/libc/stdlib/getopt.3
@@ -29,11 +29,11 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 32.\" $OpenBSD: getopt.3,v 1.22 2003/05/10 06:48:30 jmc Exp $
33.\" 33.\"
34.Dd April 19, 1994 34.Dd December 8, 2002
35.Dt GETOPT 3 35.Dt GETOPT 3
36.Os BSD 4.3 36.Os
37.Sh NAME 37.Sh NAME
38.Nm getopt 38.Nm getopt
39.Nd get option character from command line argument list 39.Nd get option character from command line argument list
@@ -51,30 +51,28 @@ The
51.Fn getopt 51.Fn getopt
52function incrementally parses a command line argument list 52function incrementally parses a command line argument list
53.Fa argv 53.Fa argv
54and returns the next 54and returns the next known option character.
55.Em known
56option character.
57An option character is 55An option character is
58.Em known 56.Dq known
59if it has been specified in the string of accepted option characters, 57if it has been specified in the string of accepted option characters,
60.Fa optstring . 58.Fa optstring .
61.Pp 59.Pp
62The option string 60The option string
63.Fa optstring 61.Fa optstring
64may contain the following elements: individual characters, and 62may contain the following elements: individual characters and
65characters followed by a colon to indicate an option argument 63characters followed by a colon to indicate an option argument
66is to follow. 64is to follow.
67For example, an option string 65For example, an option string
68.Li "\&""x"" 66.Qq x
69recognizes an option 67recognizes an option
70.Dq Fl x , 68.Fl x ,
71and an option string 69and an option string
72.Li "\&""x:"" 70.Qq Li x:
73recognizes an option and argument 71recognizes an option and argument
74.Dq Fl x Ar argument . 72.Fl x Ar argument .
75It does not matter to 73It does not matter to
76.Fn getopt 74.Fn getopt
77if a following argument has leading white space. 75if a following argument has leading whitespace.
78.Pp 76.Pp
79On return from 77On return from
80.Fn getopt , 78.Fn getopt ,
@@ -89,12 +87,10 @@ to
89.Fn getopt . 87.Fn getopt .
90The variable 88The variable
91.Va optopt 89.Va optopt
92saves the last 90saves the last known option character returned by
93.Em known
94option character returned by
95.Fn getopt . 91.Fn getopt .
96.Pp 92.Pp
97The variable 93The variables
98.Va opterr 94.Va opterr
99and 95and
100.Va optind 96.Va optind
@@ -119,10 +115,7 @@ must be reinitialized.
119.Pp 115.Pp
120The 116The
121.Fn getopt 117.Fn getopt
122function 118function 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 119The interpretation of options in the argument list may be cancelled
127by the option 120by the option
128.Ql -- 121.Ql --
@@ -133,49 +126,36 @@ When all options have been processed (i.e., up to the first non-option
133argument), 126argument),
134.Fn getopt 127.Fn getopt
135returns \-1. 128returns \-1.
136.Sh DIAGNOSTICS 129.Sh RETURN VALUES
137If the 130The
138.Fn getopt 131.Fn getopt
139function encounters a character not found in the string 132function returns the next known option character in
140.Va optarg 133.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 134If
150.Va optstring 135.Fn getopt
151has a leading 136encounters a character not found in
152.Ql \&: 137.Fa optstring
153then a missing option argument causes a 138or if it detects a missing option argument,
154.Ql \&: 139it returns
155to be returned in addition to suppressing any error messages. 140.Sq ? .
156.Pp 141If
157Option arguments are allowed to begin with 142.Fa optstring
158.Dq Li \- ; 143has a leading
159this is reasonable but 144.Sq \:
160reduces the amount of error checking possible. 145then a missing option argument causes
161.Sh EXTENSIONS 146.Sq \:
147to be returned instead of
148.Sq ? .
162The 149The
163.Va optreset
164variable was added to make it possible to call the
165.Fn getopt 150.Fn getopt
166function multiple times. 151function returns \-1 when the argument list is exhausted.
167This is an extension to the 152.Sh EXAMPLES
168.St -p1003.2
169specification.
170.Sh EXAMPLE
171.Bd -literal -compact 153.Bd -literal -compact
172extern char *optarg;
173extern int optind;
174int bflag, ch, fd; 154int bflag, ch, fd;
175 155
176bflag = 0; 156bflag = 0;
177while ((ch = getopt(argc, argv, "bf:")) != -1) 157while ((ch = getopt(argc, argv, "bf:")) != -1) {
178 switch(ch) { 158 switch (ch) {
179 case 'b': 159 case 'b':
180 bflag = 1; 160 bflag = 1;
181 break; 161 break;
@@ -189,31 +169,69 @@ while ((ch = getopt(argc, argv, "bf:")) != -1)
189 case '?': 169 case '?':
190 default: 170 default:
191 usage(); 171 usage();
172 }
192} 173}
193argc -= optind; 174argc -= optind;
194argv += optind; 175argv += optind;
195.Ed 176.Ed
177.Sh SEE ALSO
178.Xr getopt 1 ,
179.Xr getopt_long 3 ,
180.Xr getsubopt 3
181.Sh DIAGNOSTICS
182If the
183.Fn getopt
184function encounters a character not found in the string
185.Va optstring
186or detects
187a missing option argument it writes an error message to
188.Em stderr
189and returns
190.Ql ? .
191Setting
192.Va opterr
193to a zero will disable these error messages.
194If
195.Va optstring
196has a leading
197.Ql \&:
198then a missing option argument causes a
199.Ql \&:
200to be returned in addition to suppressing any error messages.
201.Pp
202Option arguments are allowed to begin with
203.Ql - ;
204this is reasonable but reduces the amount of error checking possible.
205.Sh EXTENSIONS
206The
207.Va optreset
208variable was added to make it possible to call the
209.Fn getopt
210function multiple times.
211This is an extension to the
212.St -p1003.2
213specification.
196.Sh HISTORY 214.Sh HISTORY
197The 215The
198.Fn getopt 216.Fn getopt
199function appeared 217function appeared in
200.Bx 4.3 . 218.Bx 4.3 .
201.Sh BUGS 219.Sh BUGS
202The 220The
203.Fn getopt 221.Fn getopt
204function was once specified to return 222function was once specified to return
205.Dv EOF 223.Dv EOF
206instead of \-1. 224instead of \-1.
207This was changed by 225This was changed by
208.St -p1003.2-92 226.St -p1003.2-92
209to decouple 227to decouple
210.Fn getopt 228.Fn getopt
211from 229from
212.Pa <stdio.h> . 230.Pa <stdio.h> .
213.Pp 231.Pp
214A single dash 232A single dash
215.Dq Li - 233.Pq Ql -
216may be specified as an character in 234may be specified as a character in
217.Fa optstring , 235.Fa optstring ,
218however it should 236however it should
219.Em never 237.Em never
@@ -221,15 +239,25 @@ have an argument associated with it.
221This allows 239This allows
222.Fn getopt 240.Fn getopt
223to be used with programs that expect 241to be used with programs that expect
224.Dq Li - 242.Ql -
225as an option flag. 243as an option flag.
226This practice is wrong, and should not be used in any current development. 244This practice is wrong, and should not be used in any current development.
227It is provided for backward compatibility 245It is provided for backward compatibility
228.Em only . 246.Em only .
247Care should be taken not to use
248.Ql -
249as the first character in
250.Fa optstring
251to avoid a semantic conflict with
252.Tn GNU
253.Fn getopt ,
254which assigns different meaning to an
255.Fa optstring
256that begins with a
257.Ql - .
229By default, a single dash causes 258By default, a single dash causes
230.Fn getopt 259.Fn getopt
231to return \-1. 260to return \-1.
232This is, we believe, compatible with System V.
233.Pp 261.Pp
234It is also possible to handle digits as option letters. 262It is also possible to handle digits as option letters.
235This allows 263This allows
@@ -242,18 +270,19 @@ It is provided for backward compatibility
242.Em only . 270.Em only .
243The following code fragment works in most cases. 271The following code fragment works in most cases.
244.Bd -literal -offset indent 272.Bd -literal -offset indent
245int length; 273int ch;
274long length;
246char *p; 275char *p;
247 276
248while ((c = getopt(argc, argv, "0123456789")) != -1) 277while ((ch = getopt(argc, argv, "0123456789")) != -1) {
249 switch (c) { 278 switch (ch) {
250 case '0': case '1': case '2': case '3': case '4': 279 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9': 280 case '5': case '6': case '7': case '8': case '9':
252 p = argv[optind - 1]; 281 p = argv[optind - 1];
253 if (p[0] == '-' && p[1] == ch && !p[2]) 282 if (p[0] == '-' && p[1] == ch && !p[2])
254 length = atoi(++p); 283 length = ch - '0';
255 else 284 else
256 length = atoi(argv[optind] + 1); 285 length = strtol(argv[optind] + 1, NULL, 10);
257 break; 286 break;
258 } 287 }
259} 288}