diff options
Diffstat (limited to 'src/lib/libc/stdlib/getopt.3')
-rw-r--r-- | src/lib/libc/stdlib/getopt.3 | 128 |
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 |
52 | function incrementally parses a command line argument list | 52 | function incrementally parses a command line argument list |
53 | .Fa argv | 53 | .Fa argv |
54 | and returns the next | 54 | and returns the next known option character. |
55 | .Em known | ||
56 | option character. | ||
57 | An option character is | 55 | An option character is |
58 | .Em known | 56 | .Dq known |
59 | if it has been specified in the string of accepted option characters, | 57 | if it has been specified in the string of accepted option characters, |
60 | .Fa optstring . | 58 | .Fa optstring . |
61 | .Pp | 59 | .Pp |
62 | The option string | 60 | The option string |
63 | .Fa optstring | 61 | .Fa optstring |
64 | may contain the following elements: individual characters, and | 62 | may contain the following elements: individual characters and |
65 | characters followed by a colon to indicate an option argument | 63 | characters followed by a colon to indicate an option argument |
66 | is to follow. | 64 | is to follow. |
67 | For example, an option string | 65 | For example, an option string |
68 | .Li "\&""x"" | 66 | .Qq x |
69 | recognizes an option | 67 | recognizes an option |
70 | .Dq Fl x , | 68 | .Fl x , |
71 | and an option string | 69 | and an option string |
72 | .Li "\&""x:"" | 70 | .Qq Li x: |
73 | recognizes an option and argument | 71 | recognizes an option and argument |
74 | .Dq Fl x Ar argument . | 72 | .Fl x Ar argument . |
75 | It does not matter to | 73 | It does not matter to |
76 | .Fn getopt | 74 | .Fn getopt |
77 | if a following argument has leading white space. | 75 | if a following argument has leading whitespace. |
78 | .Pp | 76 | .Pp |
79 | On return from | 77 | On return from |
80 | .Fn getopt , | 78 | .Fn getopt , |
@@ -89,12 +87,10 @@ to | |||
89 | .Fn getopt . | 87 | .Fn getopt . |
90 | The variable | 88 | The variable |
91 | .Va optopt | 89 | .Va optopt |
92 | saves the last | 90 | saves the last known option character returned by |
93 | .Em known | ||
94 | option character returned by | ||
95 | .Fn getopt . | 91 | .Fn getopt . |
96 | .Pp | 92 | .Pp |
97 | The variable | 93 | The variables |
98 | .Va opterr | 94 | .Va opterr |
99 | and | 95 | and |
100 | .Va optind | 96 | .Va optind |
@@ -119,10 +115,7 @@ must be reinitialized. | |||
119 | .Pp | 115 | .Pp |
120 | The | 116 | The |
121 | .Fn getopt | 117 | .Fn getopt |
122 | function | 118 | function returns \-1 when the argument list is exhausted. |
123 | returns \-1 | ||
124 | when the argument list is exhausted, or a non-recognized | ||
125 | option is encountered. | ||
126 | The interpretation of options in the argument list may be cancelled | 119 | The interpretation of options in the argument list may be cancelled |
127 | by the option | 120 | by the option |
128 | .Ql -- | 121 | .Ql -- |
@@ -133,31 +126,58 @@ When all options have been processed (i.e., up to the first non-option | |||
133 | argument), | 126 | argument), |
134 | .Fn getopt | 127 | .Fn getopt |
135 | returns \-1. | 128 | returns \-1. |
129 | .Sh EXAMPLES | ||
130 | .Bd -literal -compact | ||
131 | int bflag, ch, fd; | ||
132 | |||
133 | bflag = 0; | ||
134 | while ((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 | } | ||
151 | argc -= optind; | ||
152 | argv += optind; | ||
153 | .Ed | ||
154 | .Sh SEE ALSO | ||
155 | .Xr getopt 1 , | ||
156 | .Xr getsubopt 3 | ||
136 | .Sh DIAGNOSTICS | 157 | .Sh DIAGNOSTICS |
137 | If the | 158 | If the |
138 | .Fn getopt | 159 | .Fn getopt |
139 | function encounters a character not found in the string | 160 | function encounters a character not found in the string |
140 | .Va optarg | 161 | .Va optstring |
141 | or detects | 162 | or detects |
142 | a missing option argument it writes an error message and returns | 163 | a missing option argument it writes an error message to |
143 | .Ql ? | 164 | .Em stderr |
144 | to the | 165 | and returns |
145 | .Em stderr . | 166 | .Ql ? . |
146 | Setting | 167 | Setting |
147 | .Va opterr | 168 | .Va opterr |
148 | to a zero will disable these error messages. | 169 | to a zero will disable these error messages. |
149 | If | 170 | If |
150 | .Va optstring | 171 | .Va optstring |
151 | has a leading | 172 | has a leading |
152 | .Ql \&: | 173 | .Ql \&: |
153 | then a missing option argument causes a | 174 | then a missing option argument causes a |
154 | .Ql \&: | 175 | .Ql \&: |
155 | to be returned in addition to suppressing any error messages. | 176 | to be returned in addition to suppressing any error messages. |
156 | .Pp | 177 | .Pp |
157 | Option arguments are allowed to begin with | 178 | Option arguments are allowed to begin with |
158 | .Dq Li \- ; | 179 | .Ql - ; |
159 | this is reasonable but | 180 | this is reasonable but reduces the amount of error checking possible. |
160 | reduces the amount of error checking possible. | ||
161 | .Sh EXTENSIONS | 181 | .Sh EXTENSIONS |
162 | The | 182 | The |
163 | .Va optreset | 183 | .Va optreset |
@@ -167,53 +187,27 @@ function multiple times. | |||
167 | This is an extension to the | 187 | This is an extension to the |
168 | .St -p1003.2 | 188 | .St -p1003.2 |
169 | specification. | 189 | specification. |
170 | .Sh EXAMPLE | ||
171 | .Bd -literal -compact | ||
172 | extern char *optarg; | ||
173 | extern int optind; | ||
174 | int bflag, ch, fd; | ||
175 | |||
176 | bflag = 0; | ||
177 | while ((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 | } | ||
193 | argc -= optind; | ||
194 | argv += optind; | ||
195 | .Ed | ||
196 | .Sh HISTORY | 190 | .Sh HISTORY |
197 | The | 191 | The |
198 | .Fn getopt | 192 | .Fn getopt |
199 | function appeared | 193 | function appeared in |
200 | .Bx 4.3 . | 194 | .Bx 4.3 . |
201 | .Sh BUGS | 195 | .Sh BUGS |
202 | The | 196 | The |
203 | .Fn getopt | 197 | .Fn getopt |
204 | function was once specified to return | 198 | function was once specified to return |
205 | .Dv EOF | 199 | .Dv EOF |
206 | instead of \-1. | 200 | instead of \-1. |
207 | This was changed by | 201 | This was changed by |
208 | .St -p1003.2-92 | 202 | .St -p1003.2-92 |
209 | to decouple | 203 | to decouple |
210 | .Fn getopt | 204 | .Fn getopt |
211 | from | 205 | from |
212 | .Pa <stdio.h> . | 206 | .Pa <stdio.h> . |
213 | .Pp | 207 | .Pp |
214 | A single dash | 208 | A single dash |
215 | .Dq Li - | 209 | .Pq Ql - |
216 | may be specified as an character in | 210 | may be specified as a character in |
217 | .Fa optstring , | 211 | .Fa optstring , |
218 | however it should | 212 | however it should |
219 | .Em never | 213 | .Em never |
@@ -221,7 +215,7 @@ have an argument associated with it. | |||
221 | This allows | 215 | This allows |
222 | .Fn getopt | 216 | .Fn getopt |
223 | to be used with programs that expect | 217 | to be used with programs that expect |
224 | .Dq Li - | 218 | .Ql - |
225 | as an option flag. | 219 | as an option flag. |
226 | This practice is wrong, and should not be used in any current development. | 220 | This practice is wrong, and should not be used in any current development. |
227 | It is provided for backward compatibility | 221 | It is provided for backward compatibility |
@@ -242,18 +236,18 @@ It is provided for backward compatibility | |||
242 | .Em only . | 236 | .Em only . |
243 | The following code fragment works in most cases. | 237 | The following code fragment works in most cases. |
244 | .Bd -literal -offset indent | 238 | .Bd -literal -offset indent |
245 | int length; | 239 | long length; |
246 | char *p; | 240 | char *p; |
247 | 241 | ||
248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | 242 | while ((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 | } |