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.3128
1 files changed, 61 insertions, 67 deletions
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3
index f843881afd..4acbe69606 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.17 2002/08/19 22:29:52 miod Exp $
33.\" 33.\"
34.Dd April 19, 1994 34.Dd April 19, 1994
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,31 +126,58 @@ 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.
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
136.Sh DIAGNOSTICS 157.Sh DIAGNOSTICS
137If the 158If the
138.Fn getopt 159.Fn getopt
139function encounters a character not found in the string 160function encounters a character not found in the string
140.Va optarg 161.Va optstring
141or detects 162or detects
142a missing option argument it writes an error message and returns 163a missing option argument it writes an error message to
143.Ql ? 164.Em stderr
144to the 165and returns
145.Em stderr . 166.Ql ? .
146Setting 167Setting
147.Va opterr 168.Va opterr
148to a zero will disable these error messages. 169to a zero will disable these error messages.
149If 170If
150.Va optstring 171.Va optstring
151has a leading 172has a leading
152.Ql \&: 173.Ql \&:
153then a missing option argument causes a 174then a missing option argument causes a
154.Ql \&: 175.Ql \&:
155to be returned in addition to suppressing any error messages. 176to be returned in addition to suppressing any error messages.
156.Pp 177.Pp
157Option arguments are allowed to begin with 178Option arguments are allowed to begin with
158.Dq Li \- ; 179.Ql - ;
159this is reasonable but 180this is reasonable but reduces the amount of error checking possible.
160reduces the amount of error checking possible.
161.Sh EXTENSIONS 181.Sh EXTENSIONS
162The 182The
163.Va optreset 183.Va optreset
@@ -167,53 +187,27 @@ function multiple times.
167This is an extension to the 187This is an extension to the
168.St -p1003.2 188.St -p1003.2
169specification. 189specification.
170.Sh EXAMPLE
171.Bd -literal -compact
172extern char *optarg;
173extern int optind;
174int bflag, ch, fd;
175
176bflag = 0;
177while ((ch = getopt(argc, argv, "bf:")) != -1)
178 switch(ch) {
179 case 'b':
180 bflag = 1;
181 break;
182 case 'f':
183 if ((fd = open(optarg, O_RDONLY, 0)) < 0) {
184 (void)fprintf(stderr,
185 "myname: %s: %s\en", optarg, strerror(errno));
186 exit(1);
187 }
188 break;
189 case '?':
190 default:
191 usage();
192}
193argc -= optind;
194argv += optind;
195.Ed
196.Sh HISTORY 190.Sh HISTORY
197The 191The
198.Fn getopt 192.Fn getopt
199function appeared 193function appeared in
200.Bx 4.3 . 194.Bx 4.3 .
201.Sh BUGS 195.Sh BUGS
202The 196The
203.Fn getopt 197.Fn getopt
204function was once specified to return 198function was once specified to return
205.Dv EOF 199.Dv EOF
206instead of \-1. 200instead of \-1.
207This was changed by 201This was changed by
208.St -p1003.2-92 202.St -p1003.2-92
209to decouple 203to decouple
210.Fn getopt 204.Fn getopt
211from 205from
212.Pa <stdio.h> . 206.Pa <stdio.h> .
213.Pp 207.Pp
214A single dash 208A single dash
215.Dq Li - 209.Pq Ql -
216may be specified as an character in 210may be specified as a character in
217.Fa optstring , 211.Fa optstring ,
218however it should 212however it should
219.Em never 213.Em never
@@ -221,7 +215,7 @@ have an argument associated with it.
221This allows 215This allows
222.Fn getopt 216.Fn getopt
223to be used with programs that expect 217to be used with programs that expect
224.Dq Li - 218.Ql -
225as an option flag. 219as an option flag.
226This practice is wrong, and should not be used in any current development. 220This practice is wrong, and should not be used in any current development.
227It is provided for backward compatibility 221It is provided for backward compatibility
@@ -242,18 +236,18 @@ It is provided for backward compatibility
242.Em only . 236.Em only .
243The following code fragment works in most cases. 237The following code fragment works in most cases.
244.Bd -literal -offset indent 238.Bd -literal -offset indent
245int length; 239long length;
246char *p; 240char *p;
247 241
248while ((c = getopt(argc, argv, "0123456789")) != -1) 242while ((c = getopt(argc, argv, "0123456789")) != -1) {
249 switch (c) { 243 switch (c) {
250 case '0': case '1': case '2': case '3': case '4': 244 case '0': case '1': case '2': case '3': case '4':
251 case '5': case '6': case '7': case '8': case '9': 245 case '5': case '6': case '7': case '8': case '9':
252 p = argv[optind - 1]; 246 p = argv[optind - 1];
253 if (p[0] == '-' && p[1] == ch && !p[2]) 247 if (p[0] == '-' && p[1] == ch && !p[2])
254 length = atoi(++p); 248 length = ch - '0';
255 else 249 else
256 length = atoi(argv[optind] + 1); 250 length = strtol(argv[optind] + 1, NULL, 10);
257 break; 251 break;
258 } 252 }
259} 253}