Update some functions from GLIBC.
Showing
6 changed files
with
707 additions
and
250 deletions
1 | AUTOMAKE_OPTIONS = ansi2knr | ||
1 | noinst_LIBRARIES = libmailutils.a | 2 | noinst_LIBRARIES = libmailutils.a |
2 | 3 | ||
3 | libmailutils_a_SOURCES = basename.c getopt.c getopt1.c md5.c getline.c \ | 4 | libmailutils_a_SOURCES = basename.c getopt.c getopt1.c md5.c getline.c \ |
4 | xstrdup.c xmalloc.c argcv.c | 5 | xstrdup.c xmalloc.c argcv.c |
5 | 6 | ||
6 | EXTRA_DIST = alloca.c snprintf.c strtok_r.c xstrtol.c | 7 | EXTRA_DIST = alloca.c snprintf.c strchrnul.c strndup.c strnlen.c strtok_r.c \ |
8 | xstrtol.c vasprintf.c | ||
7 | 9 | ||
8 | noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h \ | 10 | noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h \ |
9 | argcv.h error.h | 11 | argcv.h error.h | ... | ... |
1 | /* Getopt for GNU. | 1 | /* Getopt for GNU. |
2 | NOTE: getopt is now part of the C library, so if you don't know what | 2 | NOTE: The canonical source of this file is maintained with the GNU |
3 | "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu | 3 | C Library. Bugs can be reported to bug-glibc@gnu.org. |
4 | before changing it! | ||
5 | 4 | ||
6 | Copyright (C) 1987, 88, 89, 90, 91, 92, 1993 | 5 | Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000 |
7 | Free Software Foundation, Inc. | 6 | Free Software Foundation, Inc. |
8 | 7 | ||
9 | This program is free software; you can redistribute it and/or modify it | 8 | This program is free software; you can redistribute it and/or modify it |
10 | under the terms of the GNU General Public License as published by the | 9 | under the terms of the GNU General Public License as published by the |
... | @@ -17,38 +16,25 @@ | ... | @@ -17,38 +16,25 @@ |
17 | GNU General Public License for more details. | 16 | GNU General Public License for more details. |
18 | 17 | ||
19 | You should have received a copy of the GNU General Public License | 18 | You should have received a copy of the GNU General Public License |
20 | along with this program; if not, write to the Free Software | 19 | along with this program; if not, write to the Free Software Foundation, |
21 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | 20 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
22 | 21 | ||
23 | /* NOTE!!! AIX requires this to be the first thing in the file. | 22 | /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. |
24 | Do not put ANYTHING before it! */ | 23 | Ditto for AIX 3.2 and <stdlib.h>. */ |
25 | #if !defined (__GNUC__) && defined (_AIX) | 24 | #ifndef _NO_PROTO |
26 | #pragma alloca | 25 | # define _NO_PROTO |
27 | #endif | 26 | #endif |
28 | 27 | ||
29 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
30 | #include "config.h" | 29 | # include <config.h> |
31 | #endif | ||
32 | |||
33 | #ifdef __GNUC__ | ||
34 | #define alloca __builtin_alloca | ||
35 | #else /* not __GNUC__ */ | ||
36 | #if defined (HAVE_ALLOCA_H) || (defined(sparc) && (defined(sun) || (!defined(USG) && !defined(SVR4) && !defined(__svr4__)))) | ||
37 | #include <alloca.h> | ||
38 | #else | ||
39 | #ifndef _AIX | ||
40 | char *alloca (); | ||
41 | #endif | 30 | #endif |
42 | #endif /* alloca.h */ | ||
43 | #endif /* not __GNUC__ */ | ||
44 | 31 | ||
45 | #if !__STDC__ && !defined(const) && IN_GCC | 32 | #if !defined __STDC__ || !__STDC__ |
46 | #define const | 33 | /* This is a separate conditional since some stdc systems |
47 | #endif | 34 | reject `defined (const)'. */ |
48 | 35 | # ifndef const | |
49 | /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. */ | 36 | # define const |
50 | #ifndef _NO_PROTO | 37 | # endif |
51 | #define _NO_PROTO | ||
52 | #endif | 38 | #endif |
53 | 39 | ||
54 | #include <stdio.h> | 40 | #include <stdio.h> |
... | @@ -61,24 +47,44 @@ char *alloca (); | ... | @@ -61,24 +47,44 @@ char *alloca (); |
61 | program understand `configure --with-gnu-libc' and omit the object files, | 47 | program understand `configure --with-gnu-libc' and omit the object files, |
62 | it is simpler to just do this in the source for each such file. */ | 48 | it is simpler to just do this in the source for each such file. */ |
63 | 49 | ||
64 | #if defined (_LIBC) || !defined (__GNU_LIBRARY__) | 50 | #define GETOPT_INTERFACE_VERSION 2 |
51 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 | ||
52 | # include <gnu-versions.h> | ||
53 | # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION | ||
54 | # define ELIDE_CODE | ||
55 | # endif | ||
56 | #endif | ||
57 | |||
58 | #ifndef ELIDE_CODE | ||
65 | 59 | ||
66 | 60 | ||
67 | /* This needs to come after some library #include | 61 | /* This needs to come after some library #include |
68 | to get __GNU_LIBRARY__ defined. */ | 62 | to get __GNU_LIBRARY__ defined. */ |
69 | #ifdef __GNU_LIBRARY__ | 63 | #ifdef __GNU_LIBRARY__ |
70 | #undef alloca | ||
71 | /* Don't include stdlib.h for non-GNU C libraries because some of them | 64 | /* Don't include stdlib.h for non-GNU C libraries because some of them |
72 | contain conflicting prototypes for getopt. */ | 65 | contain conflicting prototypes for getopt. */ |
73 | #include <stdlib.h> | 66 | # include <stdlib.h> |
74 | #else /* Not GNU C library. */ | 67 | # include <unistd.h> |
75 | #define __alloca alloca | 68 | #endif /* GNU C library. */ |
76 | #endif /* GNU C library. */ | 69 | |
70 | #ifdef VMS | ||
71 | # include <unixlib.h> | ||
72 | # if HAVE_STRING_H - 0 | ||
73 | # include <string.h> | ||
74 | # endif | ||
75 | #endif | ||
77 | 76 | ||
78 | /* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a | 77 | #ifndef _ |
79 | long-named option. Because this is not POSIX.2 compliant, it is | 78 | /* This is for other GNU distributions with internationalized messages. */ |
80 | being phased out. */ | 79 | # if defined HAVE_LIBINTL_H || defined _LIBC |
81 | /* #define GETOPT_COMPAT */ | 80 | # include <libintl.h> |
81 | # ifndef _ | ||
82 | # define _(msgid) gettext (msgid) | ||
83 | # endif | ||
84 | # else | ||
85 | # define _(msgid) (msgid) | ||
86 | # endif | ||
87 | #endif | ||
82 | 88 | ||
83 | /* This version of `getopt' appears to the caller like standard Unix `getopt' | 89 | /* This version of `getopt' appears to the caller like standard Unix `getopt' |
84 | but it behaves differently for the user, since it allows the user | 90 | but it behaves differently for the user, since it allows the user |
... | @@ -102,7 +108,7 @@ char *alloca (); | ... | @@ -102,7 +108,7 @@ char *alloca (); |
102 | Also, when `ordering' is RETURN_IN_ORDER, | 108 | Also, when `ordering' is RETURN_IN_ORDER, |
103 | each non-option ARGV-element is returned here. */ | 109 | each non-option ARGV-element is returned here. */ |
104 | 110 | ||
105 | char *optarg = 0; | 111 | char *optarg; |
106 | 112 | ||
107 | /* Index in ARGV of the next element to be scanned. | 113 | /* Index in ARGV of the next element to be scanned. |
108 | This is used for communication to and from the caller | 114 | This is used for communication to and from the caller |
... | @@ -110,14 +116,20 @@ char *optarg = 0; | ... | @@ -110,14 +116,20 @@ char *optarg = 0; |
110 | 116 | ||
111 | On entry to `getopt', zero means this is the first call; initialize. | 117 | On entry to `getopt', zero means this is the first call; initialize. |
112 | 118 | ||
113 | When `getopt' returns EOF, this is the index of the first of the | 119 | When `getopt' returns -1, this is the index of the first of the |
114 | non-option elements that the caller should itself scan. | 120 | non-option elements that the caller should itself scan. |
115 | 121 | ||
116 | Otherwise, `optind' communicates from one call to the next | 122 | Otherwise, `optind' communicates from one call to the next |
117 | how much of ARGV has been scanned so far. */ | 123 | how much of ARGV has been scanned so far. */ |
118 | 124 | ||
119 | /* XXX 1003.2 says this must be 1 before any call. */ | 125 | /* 1003.2 says this must be 1 before any call. */ |
120 | int optind = 0; | 126 | int optind = 1; |
127 | |||
128 | /* Formerly, initialization of getopt depended on optind==0, which | ||
129 | causes problems with re-calling getopt as programs generally don't | ||
130 | know that. */ | ||
131 | |||
132 | int __getopt_initialized; | ||
121 | 133 | ||
122 | /* The next char to be scanned in the option-element | 134 | /* The next char to be scanned in the option-element |
123 | in which the last option character we returned was found. | 135 | in which the last option character we returned was found. |
... | @@ -166,28 +178,37 @@ int optopt = '?'; | ... | @@ -166,28 +178,37 @@ int optopt = '?'; |
166 | 178 | ||
167 | The special argument `--' forces an end of option-scanning regardless | 179 | The special argument `--' forces an end of option-scanning regardless |
168 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only | 180 | of the value of `ordering'. In the case of RETURN_IN_ORDER, only |
169 | `--' can cause `getopt' to return EOF with `optind' != ARGC. */ | 181 | `--' can cause `getopt' to return -1 with `optind' != ARGC. */ |
170 | 182 | ||
171 | static enum | 183 | static enum |
172 | { | 184 | { |
173 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER | 185 | REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER |
174 | } | 186 | } ordering; |
175 | ordering; | 187 | |
188 | /* Value of POSIXLY_CORRECT environment variable. */ | ||
189 | static char *posixly_correct; | ||
176 | 190 | ||
177 | #ifdef __GNU_LIBRARY__ | 191 | #ifdef __GNU_LIBRARY__ |
178 | /* We want to avoid inclusion of string.h with non-GNU libraries | 192 | /* We want to avoid inclusion of string.h with non-GNU libraries |
179 | because there are many ways it can cause trouble. | 193 | because there are many ways it can cause trouble. |
180 | On some systems, it contains special magic macros that don't work | 194 | On some systems, it contains special magic macros that don't work |
181 | in GCC. */ | 195 | in GCC. */ |
182 | #include <string.h> | 196 | # include <string.h> |
183 | #define my_index strchr | 197 | # define my_index strchr |
184 | #define my_bcopy(src, dst, n) memcpy ((dst), (src), (n)) | ||
185 | #else | 198 | #else |
186 | 199 | ||
200 | # if HAVE_STRING_H | ||
201 | # include <string.h> | ||
202 | # else | ||
203 | # include <strings.h> | ||
204 | # endif | ||
205 | |||
187 | /* Avoid depending on library functions or files | 206 | /* Avoid depending on library functions or files |
188 | whose names are inconsistent. */ | 207 | whose names are inconsistent. */ |
189 | 208 | ||
190 | char *getenv (); | 209 | #ifndef getenv |
210 | extern char *getenv (); | ||
211 | #endif | ||
191 | 212 | ||
192 | static char * | 213 | static char * |
193 | my_index (str, chr) | 214 | my_index (str, chr) |
... | @@ -203,17 +224,19 @@ my_index (str, chr) | ... | @@ -203,17 +224,19 @@ my_index (str, chr) |
203 | return 0; | 224 | return 0; |
204 | } | 225 | } |
205 | 226 | ||
206 | static void | 227 | /* If using GCC, we can safely declare strlen this way. |
207 | my_bcopy (from, to, size) | 228 | If not using GCC, it is ok not to declare it. */ |
208 | const char *from; | 229 | #ifdef __GNUC__ |
209 | char *to; | 230 | /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. |
210 | int size; | 231 | That was relevant to code that was here before. */ |
211 | { | 232 | # if (!defined __STDC__ || !__STDC__) && !defined strlen |
212 | int i; | 233 | /* gcc with -traditional declares the built-in strlen to return int, |
213 | for (i = 0; i < size; i++) | 234 | and has done so at least since version 2.4.5. -- rms. */ |
214 | to[i] = from[i]; | 235 | extern int strlen (const char *); |
215 | } | 236 | # endif /* not __STDC__ */ |
216 | #endif /* GNU C library. */ | 237 | #endif /* __GNUC__ */ |
238 | |||
239 | #endif /* not __GNU_LIBRARY__ */ | ||
217 | 240 | ||
218 | /* Handle permutation of arguments. */ | 241 | /* Handle permutation of arguments. */ |
219 | 242 | ||
... | @@ -224,6 +247,46 @@ my_bcopy (from, to, size) | ... | @@ -224,6 +247,46 @@ my_bcopy (from, to, size) |
224 | static int first_nonopt; | 247 | static int first_nonopt; |
225 | static int last_nonopt; | 248 | static int last_nonopt; |
226 | 249 | ||
250 | #ifdef _LIBC | ||
251 | /* Bash 2.0 gives us an environment variable containing flags | ||
252 | indicating ARGV elements that should not be considered arguments. */ | ||
253 | |||
254 | /* Defined in getopt_init.c */ | ||
255 | extern char *__getopt_nonoption_flags; | ||
256 | |||
257 | static int nonoption_flags_max_len; | ||
258 | static int nonoption_flags_len; | ||
259 | |||
260 | static int original_argc; | ||
261 | static char *const *original_argv; | ||
262 | |||
263 | /* Make sure the environment variable bash 2.0 puts in the environment | ||
264 | is valid for the getopt call we must make sure that the ARGV passed | ||
265 | to getopt is that one passed to the process. */ | ||
266 | static void | ||
267 | __attribute__ ((unused)) | ||
268 | store_args_and_env (int argc, char *const *argv) | ||
269 | { | ||
270 | /* XXX This is no good solution. We should rather copy the args so | ||
271 | that we can compare them later. But we must not use malloc(3). */ | ||
272 | original_argc = argc; | ||
273 | original_argv = argv; | ||
274 | } | ||
275 | # ifdef text_set_element | ||
276 | text_set_element (__libc_subinit, store_args_and_env); | ||
277 | # endif /* text_set_element */ | ||
278 | |||
279 | # define SWAP_FLAGS(ch1, ch2) \ | ||
280 | if (nonoption_flags_len > 0) \ | ||
281 | { \ | ||
282 | char __tmp = __getopt_nonoption_flags[ch1]; \ | ||
283 | __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ | ||
284 | __getopt_nonoption_flags[ch2] = __tmp; \ | ||
285 | } | ||
286 | #else /* !_LIBC */ | ||
287 | # define SWAP_FLAGS(ch1, ch2) | ||
288 | #endif /* _LIBC */ | ||
289 | |||
227 | /* Exchange two adjacent subsequences of ARGV. | 290 | /* Exchange two adjacent subsequences of ARGV. |
228 | One subsequence is elements [first_nonopt,last_nonopt) | 291 | One subsequence is elements [first_nonopt,last_nonopt) |
229 | which contains all the non-options that have been skipped so far. | 292 | which contains all the non-options that have been skipped so far. |
... | @@ -233,27 +296,160 @@ static int last_nonopt; | ... | @@ -233,27 +296,160 @@ static int last_nonopt; |
233 | `first_nonopt' and `last_nonopt' are relocated so that they describe | 296 | `first_nonopt' and `last_nonopt' are relocated so that they describe |
234 | the new indices of the non-options in ARGV after they are moved. */ | 297 | the new indices of the non-options in ARGV after they are moved. */ |
235 | 298 | ||
299 | #if defined __STDC__ && __STDC__ | ||
300 | static void exchange (char **); | ||
301 | #endif | ||
302 | |||
236 | static void | 303 | static void |
237 | exchange (argv) | 304 | exchange (argv) |
238 | char **argv; | 305 | char **argv; |
239 | { | 306 | { |
240 | int nonopts_size = (last_nonopt - first_nonopt) * sizeof (char *); | 307 | int bottom = first_nonopt; |
241 | char **temp = (char **) __alloca (nonopts_size); | 308 | int middle = last_nonopt; |
309 | int top = optind; | ||
310 | char *tem; | ||
311 | |||
312 | /* Exchange the shorter segment with the far end of the longer segment. | ||
313 | That puts the shorter segment into the right place. | ||
314 | It leaves the longer segment in the right place overall, | ||
315 | but it consists of two parts that need to be swapped next. */ | ||
316 | |||
317 | #ifdef _LIBC | ||
318 | /* First make sure the handling of the `__getopt_nonoption_flags' | ||
319 | string can work normally. Our top argument must be in the range | ||
320 | of the string. */ | ||
321 | if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) | ||
322 | { | ||
323 | /* We must extend the array. The user plays games with us and | ||
324 | presents new arguments. */ | ||
325 | char *new_str = malloc (top + 1); | ||
326 | if (new_str == NULL) | ||
327 | nonoption_flags_len = nonoption_flags_max_len = 0; | ||
328 | else | ||
329 | { | ||
330 | memset (__mempcpy (new_str, __getopt_nonoption_flags, | ||
331 | nonoption_flags_max_len), | ||
332 | '\0', top + 1 - nonoption_flags_max_len); | ||
333 | nonoption_flags_max_len = top + 1; | ||
334 | __getopt_nonoption_flags = new_str; | ||
335 | } | ||
336 | } | ||
337 | #endif | ||
242 | 338 | ||
243 | /* Interchange the two blocks of data in ARGV. */ | 339 | while (top > middle && middle > bottom) |
340 | { | ||
341 | if (top - middle > middle - bottom) | ||
342 | { | ||
343 | /* Bottom segment is the short one. */ | ||
344 | int len = middle - bottom; | ||
345 | register int i; | ||
244 | 346 | ||
245 | my_bcopy ((char *) &argv[first_nonopt], (char *) temp, nonopts_size); | 347 | /* Swap it with the top part of the top segment. */ |
246 | my_bcopy ((char *) &argv[last_nonopt], (char *) &argv[first_nonopt], | 348 | for (i = 0; i < len; i++) |
247 | (optind - last_nonopt) * sizeof (char *)); | 349 | { |
248 | my_bcopy ((char *) temp, | 350 | tem = argv[bottom + i]; |
249 | (char *) &argv[first_nonopt + optind - last_nonopt], | 351 | argv[bottom + i] = argv[top - (middle - bottom) + i]; |
250 | nonopts_size); | 352 | argv[top - (middle - bottom) + i] = tem; |
353 | SWAP_FLAGS (bottom + i, top - (middle - bottom) + i); | ||
354 | } | ||
355 | /* Exclude the moved bottom segment from further swapping. */ | ||
356 | top -= len; | ||
357 | } | ||
358 | else | ||
359 | { | ||
360 | /* Top segment is the short one. */ | ||
361 | int len = top - middle; | ||
362 | register int i; | ||
363 | |||
364 | /* Swap it with the bottom part of the bottom segment. */ | ||
365 | for (i = 0; i < len; i++) | ||
366 | { | ||
367 | tem = argv[bottom + i]; | ||
368 | argv[bottom + i] = argv[middle + i]; | ||
369 | argv[middle + i] = tem; | ||
370 | SWAP_FLAGS (bottom + i, middle + i); | ||
371 | } | ||
372 | /* Exclude the moved top segment from further swapping. */ | ||
373 | bottom += len; | ||
374 | } | ||
375 | } | ||
251 | 376 | ||
252 | /* Update records for the slots the non-options now occupy. */ | 377 | /* Update records for the slots the non-options now occupy. */ |
253 | 378 | ||
254 | first_nonopt += (optind - last_nonopt); | 379 | first_nonopt += (optind - last_nonopt); |
255 | last_nonopt = optind; | 380 | last_nonopt = optind; |
256 | } | 381 | } |
382 | |||
383 | /* Initialize the internal data when the first call is made. */ | ||
384 | |||
385 | #if defined __STDC__ && __STDC__ | ||
386 | static const char *_getopt_initialize (int, char *const *, const char *); | ||
387 | #endif | ||
388 | static const char * | ||
389 | _getopt_initialize (argc, argv, optstring) | ||
390 | int argc; | ||
391 | char *const *argv; | ||
392 | const char *optstring; | ||
393 | { | ||
394 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 | ||
395 | is the program name); the sequence of previously skipped | ||
396 | non-option ARGV-elements is empty. */ | ||
397 | |||
398 | first_nonopt = last_nonopt = optind; | ||
399 | |||
400 | nextchar = NULL; | ||
401 | |||
402 | posixly_correct = getenv ("POSIXLY_CORRECT"); | ||
403 | |||
404 | /* Determine how to handle the ordering of options and nonoptions. */ | ||
405 | |||
406 | if (optstring[0] == '-') | ||
407 | { | ||
408 | ordering = RETURN_IN_ORDER; | ||
409 | ++optstring; | ||
410 | } | ||
411 | else if (optstring[0] == '+') | ||
412 | { | ||
413 | ordering = REQUIRE_ORDER; | ||
414 | ++optstring; | ||
415 | } | ||
416 | else if (posixly_correct != NULL) | ||
417 | ordering = REQUIRE_ORDER; | ||
418 | else | ||
419 | ordering = PERMUTE; | ||
420 | |||
421 | #ifdef _LIBC | ||
422 | if (posixly_correct == NULL | ||
423 | && argc == original_argc && argv == original_argv) | ||
424 | { | ||
425 | if (nonoption_flags_max_len == 0) | ||
426 | { | ||
427 | if (__getopt_nonoption_flags == NULL | ||
428 | || __getopt_nonoption_flags[0] == '\0') | ||
429 | nonoption_flags_max_len = -1; | ||
430 | else | ||
431 | { | ||
432 | const char *orig_str = __getopt_nonoption_flags; | ||
433 | int len = nonoption_flags_max_len = strlen (orig_str); | ||
434 | if (nonoption_flags_max_len < argc) | ||
435 | nonoption_flags_max_len = argc; | ||
436 | __getopt_nonoption_flags = | ||
437 | (char *) malloc (nonoption_flags_max_len); | ||
438 | if (__getopt_nonoption_flags == NULL) | ||
439 | nonoption_flags_max_len = -1; | ||
440 | else | ||
441 | memset (__mempcpy (__getopt_nonoption_flags, orig_str, len), | ||
442 | '\0', nonoption_flags_max_len - len); | ||
443 | } | ||
444 | } | ||
445 | nonoption_flags_len = nonoption_flags_max_len; | ||
446 | } | ||
447 | else | ||
448 | nonoption_flags_len = 0; | ||
449 | #endif | ||
450 | |||
451 | return optstring; | ||
452 | } | ||
257 | 453 | ||
258 | /* Scan elements of ARGV (whose length is ARGC) for option characters | 454 | /* Scan elements of ARGV (whose length is ARGC) for option characters |
259 | given in OPTSTRING. | 455 | given in OPTSTRING. |
... | @@ -268,7 +464,7 @@ exchange (argv) | ... | @@ -268,7 +464,7 @@ exchange (argv) |
268 | updating `optind' and `nextchar' so that the next call to `getopt' can | 464 | updating `optind' and `nextchar' so that the next call to `getopt' can |
269 | resume the scan with the following option character or ARGV-element. | 465 | resume the scan with the following option character or ARGV-element. |
270 | 466 | ||
271 | If there are no more option characters, `getopt' returns `EOF'. | 467 | If there are no more option characters, `getopt' returns -1. |
272 | Then `optind' is the index in ARGV of the first ARGV-element | 468 | Then `optind' is the index in ARGV of the first ARGV-element |
273 | that is not an option. (The ARGV-elements have been permuted | 469 | that is not an option. (The ARGV-elements have been permuted |
274 | so that those that are not options now come last.) | 470 | so that those that are not options now come last.) |
... | @@ -320,41 +516,46 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -320,41 +516,46 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
320 | int *longind; | 516 | int *longind; |
321 | int long_only; | 517 | int long_only; |
322 | { | 518 | { |
323 | int option_index; | 519 | int print_errors = opterr; |
520 | if (optstring[0] == ':') | ||
521 | print_errors = 0; | ||
324 | 522 | ||
325 | optarg = 0; | 523 | if (argc < 1) |
524 | return -1; | ||
326 | 525 | ||
327 | /* Initialize the internal data when the first call is made. | 526 | optarg = NULL; |
328 | Start processing options with ARGV-element 1 (since ARGV-element 0 | ||
329 | is the program name); the sequence of previously skipped | ||
330 | non-option ARGV-elements is empty. */ | ||
331 | 527 | ||
332 | if (optind == 0) | 528 | if (optind == 0 || !__getopt_initialized) |
333 | { | 529 | { |
334 | first_nonopt = last_nonopt = optind = 1; | 530 | if (optind == 0) |
335 | 531 | optind = 1; /* Don't scan ARGV[0], the program name. */ | |
336 | nextchar = NULL; | 532 | optstring = _getopt_initialize (argc, argv, optstring); |
337 | 533 | __getopt_initialized = 1; | |
338 | /* Determine how to handle the ordering of options and nonoptions. */ | ||
339 | |||
340 | if (optstring[0] == '-') | ||
341 | { | ||
342 | ordering = RETURN_IN_ORDER; | ||
343 | ++optstring; | ||
344 | } | ||
345 | else if (optstring[0] == '+') | ||
346 | { | ||
347 | ordering = REQUIRE_ORDER; | ||
348 | ++optstring; | ||
349 | } | ||
350 | else if (getenv ("POSIXLY_CORRECT") != NULL) | ||
351 | ordering = REQUIRE_ORDER; | ||
352 | else | ||
353 | ordering = PERMUTE; | ||
354 | } | 534 | } |
355 | 535 | ||
536 | /* Test whether ARGV[optind] points to a non-option argument. | ||
537 | Either it does not have option syntax, or there is an environment flag | ||
538 | from the shell indicating it is not an option. The later information | ||
539 | is only used when the used in the GNU libc. */ | ||
540 | #ifdef _LIBC | ||
541 | # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ | ||
542 | || (optind < nonoption_flags_len \ | ||
543 | && __getopt_nonoption_flags[optind] == '1')) | ||
544 | #else | ||
545 | # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') | ||
546 | #endif | ||
547 | |||
356 | if (nextchar == NULL || *nextchar == '\0') | 548 | if (nextchar == NULL || *nextchar == '\0') |
357 | { | 549 | { |
550 | /* Advance to the next ARGV-element. */ | ||
551 | |||
552 | /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been | ||
553 | moved back by the user (who may also have changed the arguments). */ | ||
554 | if (last_nonopt > optind) | ||
555 | last_nonopt = optind; | ||
556 | if (first_nonopt > optind) | ||
557 | first_nonopt = optind; | ||
558 | |||
358 | if (ordering == PERMUTE) | 559 | if (ordering == PERMUTE) |
359 | { | 560 | { |
360 | /* If we have just processed some options following some non-options, | 561 | /* If we have just processed some options following some non-options, |
... | @@ -365,24 +566,18 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -365,24 +566,18 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
365 | else if (last_nonopt != optind) | 566 | else if (last_nonopt != optind) |
366 | first_nonopt = optind; | 567 | first_nonopt = optind; |
367 | 568 | ||
368 | /* Now skip any additional non-options | 569 | /* Skip any additional non-options |
369 | and extend the range of non-options previously skipped. */ | 570 | and extend the range of non-options previously skipped. */ |
370 | 571 | ||
371 | while (optind < argc | 572 | while (optind < argc && NONOPTION_P) |
372 | && (argv[optind][0] != '-' || argv[optind][1] == '\0') | ||
373 | #ifdef GETOPT_COMPAT | ||
374 | && (longopts == NULL | ||
375 | || argv[optind][0] != '+' || argv[optind][1] == '\0') | ||
376 | #endif /* GETOPT_COMPAT */ | ||
377 | ) | ||
378 | optind++; | 573 | optind++; |
379 | last_nonopt = optind; | 574 | last_nonopt = optind; |
380 | } | 575 | } |
381 | 576 | ||
382 | /* Special ARGV-element `--' means premature end of options. | 577 | /* The special ARGV-element `--' means premature end of options. |
383 | Skip it like a null option, | 578 | Skip it like a null option, |
384 | then exchange with previous non-options as if it were an option, | 579 | then exchange with previous non-options as if it were an option, |
385 | then skip everything else like a non-option. */ | 580 | then skip everything else like a non-option. */ |
386 | 581 | ||
387 | if (optind != argc && !strcmp (argv[optind], "--")) | 582 | if (optind != argc && !strcmp (argv[optind], "--")) |
388 | { | 583 | { |
... | @@ -398,7 +593,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -398,7 +593,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
398 | } | 593 | } |
399 | 594 | ||
400 | /* If we have done all the ARGV-elements, stop the scan | 595 | /* If we have done all the ARGV-elements, stop the scan |
401 | and back over any non-options that we skipped and permuted. */ | 596 | and back over any non-options that we skipped and permuted. */ |
402 | 597 | ||
403 | if (optind == argc) | 598 | if (optind == argc) |
404 | { | 599 | { |
... | @@ -406,56 +601,64 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -406,56 +601,64 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
406 | that we previously skipped, so the caller will digest them. */ | 601 | that we previously skipped, so the caller will digest them. */ |
407 | if (first_nonopt != last_nonopt) | 602 | if (first_nonopt != last_nonopt) |
408 | optind = first_nonopt; | 603 | optind = first_nonopt; |
409 | return EOF; | 604 | return -1; |
410 | } | 605 | } |
411 | 606 | ||
412 | /* If we have come to a non-option and did not permute it, | 607 | /* If we have come to a non-option and did not permute it, |
413 | either stop the scan or describe it to the caller and pass it by. */ | 608 | either stop the scan or describe it to the caller and pass it by. */ |
414 | 609 | ||
415 | if ((argv[optind][0] != '-' || argv[optind][1] == '\0') | 610 | if (NONOPTION_P) |
416 | #ifdef GETOPT_COMPAT | ||
417 | && (longopts == NULL | ||
418 | || argv[optind][0] != '+' || argv[optind][1] == '\0') | ||
419 | #endif /* GETOPT_COMPAT */ | ||
420 | ) | ||
421 | { | 611 | { |
422 | if (ordering == REQUIRE_ORDER) | 612 | if (ordering == REQUIRE_ORDER) |
423 | return EOF; | 613 | return -1; |
424 | optarg = argv[optind++]; | 614 | optarg = argv[optind++]; |
425 | return 1; | 615 | return 1; |
426 | } | 616 | } |
427 | 617 | ||
428 | /* We have found another option-ARGV-element. | 618 | /* We have found another option-ARGV-element. |
429 | Start decoding its characters. */ | 619 | Skip the initial punctuation. */ |
430 | 620 | ||
431 | nextchar = (argv[optind] + 1 | 621 | nextchar = (argv[optind] + 1 |
432 | + (longopts != NULL && argv[optind][1] == '-')); | 622 | + (longopts != NULL && argv[optind][1] == '-')); |
433 | } | 623 | } |
434 | 624 | ||
625 | /* Decode the current option-ARGV-element. */ | ||
626 | |||
627 | /* Check whether the ARGV-element is a long option. | ||
628 | |||
629 | If long_only and the ARGV-element has the form "-f", where f is | ||
630 | a valid short option, don't consider it an abbreviated form of | ||
631 | a long option that starts with f. Otherwise there would be no | ||
632 | way to give the -f short option. | ||
633 | |||
634 | On the other hand, if there's a long option "fubar" and | ||
635 | the ARGV-element is "-fu", do consider that an abbreviation of | ||
636 | the long option, just like "--fu", and not "-f" with arg "u". | ||
637 | |||
638 | This distinction seems to be the most useful approach. */ | ||
639 | |||
435 | if (longopts != NULL | 640 | if (longopts != NULL |
436 | && ((argv[optind][0] == '-' | 641 | && (argv[optind][1] == '-' |
437 | && (argv[optind][1] == '-' || long_only)) | 642 | || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) |
438 | #ifdef GETOPT_COMPAT | ||
439 | || argv[optind][0] == '+' | ||
440 | #endif /* GETOPT_COMPAT */ | ||
441 | )) | ||
442 | { | 643 | { |
644 | char *nameend; | ||
443 | const struct option *p; | 645 | const struct option *p; |
444 | char *s = nextchar; | 646 | const struct option *pfound = NULL; |
445 | int exact = 0; | 647 | int exact = 0; |
446 | int ambig = 0; | 648 | int ambig = 0; |
447 | const struct option *pfound = NULL; | 649 | int indfound = -1; |
448 | int indfound; | 650 | int option_index; |
449 | 651 | ||
450 | while (*s && *s != '=') | 652 | for (nameend = nextchar; *nameend && *nameend != '='; nameend++) |
451 | s++; | 653 | /* Do nothing. */ ; |
452 | 654 | ||
453 | /* Test all options for either exact match or abbreviated matches. */ | 655 | /* Test all long options for either exact match |
454 | for (p = longopts, option_index = 0; p->name; | 656 | or abbreviated matches. */ |
455 | p++, option_index++) | 657 | for (p = longopts, option_index = 0; p->name; p++, option_index++) |
456 | if (!strncmp (p->name, nextchar, s - nextchar)) | 658 | if (!strncmp (p->name, nextchar, nameend - nextchar)) |
457 | { | 659 | { |
458 | if (s - nextchar == strlen (p->name)) | 660 | if ((unsigned int) (nameend - nextchar) |
661 | == (unsigned int) strlen (p->name)) | ||
459 | { | 662 | { |
460 | /* Exact match found. */ | 663 | /* Exact match found. */ |
461 | pfound = p; | 664 | pfound = p; |
... | @@ -469,18 +672,22 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -469,18 +672,22 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
469 | pfound = p; | 672 | pfound = p; |
470 | indfound = option_index; | 673 | indfound = option_index; |
471 | } | 674 | } |
472 | else | 675 | else if (long_only |
473 | /* Second nonexact match found. */ | 676 | || pfound->has_arg != p->has_arg |
677 | || pfound->flag != p->flag | ||
678 | || pfound->val != p->val) | ||
679 | /* Second or later nonexact match found. */ | ||
474 | ambig = 1; | 680 | ambig = 1; |
475 | } | 681 | } |
476 | 682 | ||
477 | if (ambig && !exact) | 683 | if (ambig && !exact) |
478 | { | 684 | { |
479 | if (opterr) | 685 | if (print_errors) |
480 | fprintf (stderr, "%s: option `%s' is ambiguous\n", | 686 | fprintf (stderr, _("%s: option `%s' is ambiguous\n"), |
481 | argv[0], argv[optind]); | 687 | argv[0], argv[optind]); |
482 | nextchar += strlen (nextchar); | 688 | nextchar += strlen (nextchar); |
483 | optind++; | 689 | optind++; |
690 | optopt = 0; | ||
484 | return '?'; | 691 | return '?'; |
485 | } | 692 | } |
486 | 693 | ||
... | @@ -488,28 +695,31 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -488,28 +695,31 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
488 | { | 695 | { |
489 | option_index = indfound; | 696 | option_index = indfound; |
490 | optind++; | 697 | optind++; |
491 | if (*s) | 698 | if (*nameend) |
492 | { | 699 | { |
493 | /* Don't test has_arg with >, because some C compilers don't | 700 | /* Don't test has_arg with >, because some C compilers don't |
494 | allow it to be used on enums. */ | 701 | allow it to be used on enums. */ |
495 | if (pfound->has_arg) | 702 | if (pfound->has_arg) |
496 | optarg = s + 1; | 703 | optarg = nameend + 1; |
497 | else | 704 | else |
498 | { | 705 | { |
499 | if (opterr) | 706 | if (print_errors) |
500 | { | 707 | { |
501 | if (argv[optind - 1][1] == '-') | 708 | if (argv[optind - 1][1] == '-') |
502 | /* --option */ | 709 | /* --option */ |
503 | fprintf (stderr, | 710 | fprintf (stderr, |
504 | "%s: option `--%s' doesn't allow an argument\n", | 711 | _("%s: option `--%s' doesn't allow an argument\n"), |
505 | argv[0], pfound->name); | 712 | argv[0], pfound->name); |
506 | else | 713 | else |
507 | /* +option or -option */ | 714 | /* +option or -option */ |
508 | fprintf (stderr, | 715 | fprintf (stderr, |
509 | "%s: option `%c%s' doesn't allow an argument\n", | 716 | _("%s: option `%c%s' doesn't allow an argument\n"), |
510 | argv[0], argv[optind - 1][0], pfound->name); | 717 | argv[0], argv[optind - 1][0], pfound->name); |
511 | } | 718 | } |
719 | |||
512 | nextchar += strlen (nextchar); | 720 | nextchar += strlen (nextchar); |
721 | |||
722 | optopt = pfound->val; | ||
513 | return '?'; | 723 | return '?'; |
514 | } | 724 | } |
515 | } | 725 | } |
... | @@ -519,10 +729,12 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -519,10 +729,12 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
519 | optarg = argv[optind++]; | 729 | optarg = argv[optind++]; |
520 | else | 730 | else |
521 | { | 731 | { |
522 | if (opterr) | 732 | if (print_errors) |
523 | fprintf (stderr, "%s: option `%s' requires an argument\n", | 733 | fprintf (stderr, |
524 | argv[0], argv[optind - 1]); | 734 | _("%s: option `%s' requires an argument\n"), |
735 | argv[0], argv[optind - 1]); | ||
525 | nextchar += strlen (nextchar); | 736 | nextchar += strlen (nextchar); |
737 | optopt = pfound->val; | ||
526 | return optstring[0] == ':' ? ':' : '?'; | 738 | return optstring[0] == ':' ? ':' : '?'; |
527 | } | 739 | } |
528 | } | 740 | } |
... | @@ -536,34 +748,33 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -536,34 +748,33 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
536 | } | 748 | } |
537 | return pfound->val; | 749 | return pfound->val; |
538 | } | 750 | } |
751 | |||
539 | /* Can't find it as a long option. If this is not getopt_long_only, | 752 | /* Can't find it as a long option. If this is not getopt_long_only, |
540 | or the option starts with '--' or is not a valid short | 753 | or the option starts with '--' or is not a valid short |
541 | option, then it's an error. | 754 | option, then it's an error. |
542 | Otherwise interpret it as a short option. */ | 755 | Otherwise interpret it as a short option. */ |
543 | if (!long_only || argv[optind][1] == '-' | 756 | if (!long_only || argv[optind][1] == '-' |
544 | #ifdef GETOPT_COMPAT | ||
545 | || argv[optind][0] == '+' | ||
546 | #endif /* GETOPT_COMPAT */ | ||
547 | || my_index (optstring, *nextchar) == NULL) | 757 | || my_index (optstring, *nextchar) == NULL) |
548 | { | 758 | { |
549 | if (opterr) | 759 | if (print_errors) |
550 | { | 760 | { |
551 | if (argv[optind][1] == '-') | 761 | if (argv[optind][1] == '-') |
552 | /* --option */ | 762 | /* --option */ |
553 | fprintf (stderr, "%s: unrecognized option `--%s'\n", | 763 | fprintf (stderr, _("%s: unrecognized option `--%s'\n"), |
554 | argv[0], nextchar); | 764 | argv[0], nextchar); |
555 | else | 765 | else |
556 | /* +option or -option */ | 766 | /* +option or -option */ |
557 | fprintf (stderr, "%s: unrecognized option `%c%s'\n", | 767 | fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), |
558 | argv[0], argv[optind][0], nextchar); | 768 | argv[0], argv[optind][0], nextchar); |
559 | } | 769 | } |
560 | nextchar = (char *) ""; | 770 | nextchar = (char *) ""; |
561 | optind++; | 771 | optind++; |
772 | optopt = 0; | ||
562 | return '?'; | 773 | return '?'; |
563 | } | 774 | } |
564 | } | 775 | } |
565 | 776 | ||
566 | /* Look at and handle the next option-character. */ | 777 | /* Look at and handle the next short option-character. */ |
567 | 778 | ||
568 | { | 779 | { |
569 | char c = *nextchar++; | 780 | char c = *nextchar++; |
... | @@ -575,22 +786,143 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -575,22 +786,143 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
575 | 786 | ||
576 | if (temp == NULL || c == ':') | 787 | if (temp == NULL || c == ':') |
577 | { | 788 | { |
578 | if (opterr) | 789 | if (print_errors) |
579 | { | 790 | { |
580 | #if 0 | 791 | if (posixly_correct) |
581 | if (c < 040 || c >= 0177) | 792 | /* 1003.2 specifies the format of this message. */ |
582 | fprintf (stderr, "%s: unrecognized option, character code 0%o\n", | 793 | fprintf (stderr, _("%s: illegal option -- %c\n"), |
583 | argv[0], c); | 794 | argv[0], c); |
584 | else | 795 | else |
585 | fprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c); | 796 | fprintf (stderr, _("%s: invalid option -- %c\n"), |
586 | #else | 797 | argv[0], c); |
587 | /* 1003.2 specifies the format of this message. */ | ||
588 | fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c); | ||
589 | #endif | ||
590 | } | 798 | } |
591 | optopt = c; | 799 | optopt = c; |
592 | return '?'; | 800 | return '?'; |
593 | } | 801 | } |
802 | /* Convenience. Treat POSIX -W foo same as long option --foo */ | ||
803 | if (temp[0] == 'W' && temp[1] == ';') | ||
804 | { | ||
805 | char *nameend; | ||
806 | const struct option *p; | ||
807 | const struct option *pfound = NULL; | ||
808 | int exact = 0; | ||
809 | int ambig = 0; | ||
810 | int indfound = 0; | ||
811 | int option_index; | ||
812 | |||
813 | /* This is an option that requires an argument. */ | ||
814 | if (*nextchar != '\0') | ||
815 | { | ||
816 | optarg = nextchar; | ||
817 | /* If we end this ARGV-element by taking the rest as an arg, | ||
818 | we must advance to the next element now. */ | ||
819 | optind++; | ||
820 | } | ||
821 | else if (optind == argc) | ||
822 | { | ||
823 | if (print_errors) | ||
824 | { | ||
825 | /* 1003.2 specifies the format of this message. */ | ||
826 | fprintf (stderr, _("%s: option requires an argument -- %c\n"), | ||
827 | argv[0], c); | ||
828 | } | ||
829 | optopt = c; | ||
830 | if (optstring[0] == ':') | ||
831 | c = ':'; | ||
832 | else | ||
833 | c = '?'; | ||
834 | return c; | ||
835 | } | ||
836 | else | ||
837 | /* We already incremented `optind' once; | ||
838 | increment it again when taking next ARGV-elt as argument. */ | ||
839 | optarg = argv[optind++]; | ||
840 | |||
841 | /* optarg is now the argument, see if it's in the | ||
842 | table of longopts. */ | ||
843 | |||
844 | for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++) | ||
845 | /* Do nothing. */ ; | ||
846 | |||
847 | /* Test all long options for either exact match | ||
848 | or abbreviated matches. */ | ||
849 | for (p = longopts, option_index = 0; p->name; p++, option_index++) | ||
850 | if (!strncmp (p->name, nextchar, nameend - nextchar)) | ||
851 | { | ||
852 | if ((unsigned int) (nameend - nextchar) == strlen (p->name)) | ||
853 | { | ||
854 | /* Exact match found. */ | ||
855 | pfound = p; | ||
856 | indfound = option_index; | ||
857 | exact = 1; | ||
858 | break; | ||
859 | } | ||
860 | else if (pfound == NULL) | ||
861 | { | ||
862 | /* First nonexact match found. */ | ||
863 | pfound = p; | ||
864 | indfound = option_index; | ||
865 | } | ||
866 | else | ||
867 | /* Second or later nonexact match found. */ | ||
868 | ambig = 1; | ||
869 | } | ||
870 | if (ambig && !exact) | ||
871 | { | ||
872 | if (print_errors) | ||
873 | fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), | ||
874 | argv[0], argv[optind]); | ||
875 | nextchar += strlen (nextchar); | ||
876 | optind++; | ||
877 | return '?'; | ||
878 | } | ||
879 | if (pfound != NULL) | ||
880 | { | ||
881 | option_index = indfound; | ||
882 | if (*nameend) | ||
883 | { | ||
884 | /* Don't test has_arg with >, because some C compilers don't | ||
885 | allow it to be used on enums. */ | ||
886 | if (pfound->has_arg) | ||
887 | optarg = nameend + 1; | ||
888 | else | ||
889 | { | ||
890 | if (print_errors) | ||
891 | fprintf (stderr, _("\ | ||
892 | %s: option `-W %s' doesn't allow an argument\n"), | ||
893 | argv[0], pfound->name); | ||
894 | |||
895 | nextchar += strlen (nextchar); | ||
896 | return '?'; | ||
897 | } | ||
898 | } | ||
899 | else if (pfound->has_arg == 1) | ||
900 | { | ||
901 | if (optind < argc) | ||
902 | optarg = argv[optind++]; | ||
903 | else | ||
904 | { | ||
905 | if (print_errors) | ||
906 | fprintf (stderr, | ||
907 | _("%s: option `%s' requires an argument\n"), | ||
908 | argv[0], argv[optind - 1]); | ||
909 | nextchar += strlen (nextchar); | ||
910 | return optstring[0] == ':' ? ':' : '?'; | ||
911 | } | ||
912 | } | ||
913 | nextchar += strlen (nextchar); | ||
914 | if (longind != NULL) | ||
915 | *longind = option_index; | ||
916 | if (pfound->flag) | ||
917 | { | ||
918 | *(pfound->flag) = pfound->val; | ||
919 | return 0; | ||
920 | } | ||
921 | return pfound->val; | ||
922 | } | ||
923 | nextchar = NULL; | ||
924 | return 'W'; /* Let the application handle it. */ | ||
925 | } | ||
594 | if (temp[1] == ':') | 926 | if (temp[1] == ':') |
595 | { | 927 | { |
596 | if (temp[2] == ':') | 928 | if (temp[2] == ':') |
... | @@ -602,7 +934,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -602,7 +934,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
602 | optind++; | 934 | optind++; |
603 | } | 935 | } |
604 | else | 936 | else |
605 | optarg = 0; | 937 | optarg = NULL; |
606 | nextchar = NULL; | 938 | nextchar = NULL; |
607 | } | 939 | } |
608 | else | 940 | else |
... | @@ -617,16 +949,12 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -617,16 +949,12 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
617 | } | 949 | } |
618 | else if (optind == argc) | 950 | else if (optind == argc) |
619 | { | 951 | { |
620 | if (opterr) | 952 | if (print_errors) |
621 | { | 953 | { |
622 | #if 0 | ||
623 | fprintf (stderr, "%s: option `-%c' requires an argument\n", | ||
624 | argv[0], c); | ||
625 | #else | ||
626 | /* 1003.2 specifies the format of this message. */ | 954 | /* 1003.2 specifies the format of this message. */ |
627 | fprintf (stderr, "%s: option requires an argument -- %c\n", | 955 | fprintf (stderr, |
956 | _("%s: option requires an argument -- %c\n"), | ||
628 | argv[0], c); | 957 | argv[0], c); |
629 | #endif | ||
630 | } | 958 | } |
631 | optopt = c; | 959 | optopt = c; |
632 | if (optstring[0] == ':') | 960 | if (optstring[0] == ':') |
... | @@ -636,7 +964,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | ... | @@ -636,7 +964,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) |
636 | } | 964 | } |
637 | else | 965 | else |
638 | /* We already incremented `optind' once; | 966 | /* We already incremented `optind' once; |
639 | increment it again when taking next ARGV-elt as argument. */ | 967 | increment it again when taking next ARGV-elt as argument. */ |
640 | optarg = argv[optind++]; | 968 | optarg = argv[optind++]; |
641 | nextchar = NULL; | 969 | nextchar = NULL; |
642 | } | 970 | } |
... | @@ -657,7 +985,7 @@ getopt (argc, argv, optstring) | ... | @@ -657,7 +985,7 @@ getopt (argc, argv, optstring) |
657 | 0); | 985 | 0); |
658 | } | 986 | } |
659 | 987 | ||
660 | #endif /* _LIBC or not __GNU_LIBRARY__. */ | 988 | #endif /* Not ELIDE_CODE. */ |
661 | 989 | ||
662 | #ifdef TEST | 990 | #ifdef TEST |
663 | 991 | ||
... | @@ -677,7 +1005,7 @@ main (argc, argv) | ... | @@ -677,7 +1005,7 @@ main (argc, argv) |
677 | int this_option_optind = optind ? optind : 1; | 1005 | int this_option_optind = optind ? optind : 1; |
678 | 1006 | ||
679 | c = getopt (argc, argv, "abc:d:0123456789"); | 1007 | c = getopt (argc, argv, "abc:d:0123456789"); |
680 | if (c == EOF) | 1008 | if (c == -1) |
681 | break; | 1009 | break; |
682 | 1010 | ||
683 | switch (c) | 1011 | switch (c) | ... | ... |
1 | /* Declarations for getopt. | 1 | /* Declarations for getopt. |
2 | Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | 2 | Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc. |
3 | 3 | NOTE: The canonical source of this file is maintained with the GNU C Library. | |
4 | Bugs can be reported to bug-glibc@gnu.org. | ||
4 | This program is free software; you can redistribute it and/or modify it | 5 | This program is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU General Public License as published by the | 6 | under the terms of the GNU General Public License as published by the |
6 | Free Software Foundation; either version 2, or (at your option) any | 7 | Free Software Foundation; either version 2, or (at your option) any |
... | @@ -13,14 +14,17 @@ | ... | @@ -13,14 +14,17 @@ |
13 | 14 | ||
14 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software | 16 | along with this program; if not, write to the Free Software |
16 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18 | USA. */ | ||
17 | 19 | ||
18 | #ifndef _GETOPT_H | 20 | #ifndef _GETOPT_H |
19 | #define _GETOPT_H 1 | 21 | |
22 | #ifndef __need_getopt | ||
23 | # define _GETOPT_H 1 | ||
24 | #endif | ||
20 | 25 | ||
21 | #ifdef __cplusplus | 26 | #ifdef __cplusplus |
22 | extern "C" | 27 | extern "C" { |
23 | { | ||
24 | #endif | 28 | #endif |
25 | 29 | ||
26 | /* For communication from `getopt' to the caller. | 30 | /* For communication from `getopt' to the caller. |
... | @@ -29,7 +33,7 @@ extern "C" | ... | @@ -29,7 +33,7 @@ extern "C" |
29 | Also, when `ordering' is RETURN_IN_ORDER, | 33 | Also, when `ordering' is RETURN_IN_ORDER, |
30 | each non-option ARGV-element is returned here. */ | 34 | each non-option ARGV-element is returned here. */ |
31 | 35 | ||
32 | extern char *optarg; | 36 | extern char *optarg; |
33 | 37 | ||
34 | /* Index in ARGV of the next element to be scanned. | 38 | /* Index in ARGV of the next element to be scanned. |
35 | This is used for communication to and from the caller | 39 | This is used for communication to and from the caller |
... | @@ -37,32 +41,33 @@ extern "C" | ... | @@ -37,32 +41,33 @@ extern "C" |
37 | 41 | ||
38 | On entry to `getopt', zero means this is the first call; initialize. | 42 | On entry to `getopt', zero means this is the first call; initialize. |
39 | 43 | ||
40 | When `getopt' returns EOF, this is the index of the first of the | 44 | When `getopt' returns -1, this is the index of the first of the |
41 | non-option elements that the caller should itself scan. | 45 | non-option elements that the caller should itself scan. |
42 | 46 | ||
43 | Otherwise, `optind' communicates from one call to the next | 47 | Otherwise, `optind' communicates from one call to the next |
44 | how much of ARGV has been scanned so far. */ | 48 | how much of ARGV has been scanned so far. */ |
45 | 49 | ||
46 | extern int optind; | 50 | extern int optind; |
47 | 51 | ||
48 | /* Callers store zero here to inhibit the error message `getopt' prints | 52 | /* Callers store zero here to inhibit the error message `getopt' prints |
49 | for unrecognized options. */ | 53 | for unrecognized options. */ |
50 | 54 | ||
51 | extern int opterr; | 55 | extern int opterr; |
52 | 56 | ||
53 | /* Set to an option character which was unrecognized. */ | 57 | /* Set to an option character which was unrecognized. */ |
54 | 58 | ||
55 | extern int optopt; | 59 | extern int optopt; |
56 | 60 | ||
61 | #ifndef __need_getopt | ||
57 | /* Describe the long-named options requested by the application. | 62 | /* Describe the long-named options requested by the application. |
58 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector | 63 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector |
59 | of `struct option' terminated by an element containing a name which is | 64 | of `struct option' terminated by an element containing a name which is |
60 | zero. | 65 | zero. |
61 | 66 | ||
62 | The field `has_arg' is: | 67 | The field `has_arg' is: |
63 | no_argument (or 0) if the option does not take an argument, | 68 | no_argument (or 0) if the option does not take an argument, |
64 | required_argument (or 1) if the option requires an argument, | 69 | required_argument (or 1) if the option requires an argument, |
65 | optional_argument (or 2) if the option takes an optional argument. | 70 | optional_argument (or 2) if the option takes an optional argument. |
66 | 71 | ||
67 | If the field `flag' is not NULL, it points to a variable that is set | 72 | If the field `flag' is not NULL, it points to a variable that is set |
68 | to the value given in the field `val' when the option is found, but | 73 | to the value given in the field `val' when the option is found, but |
... | @@ -75,56 +80,90 @@ extern "C" | ... | @@ -75,56 +80,90 @@ extern "C" |
75 | one). For long options that have a zero `flag' field, `getopt' | 80 | one). For long options that have a zero `flag' field, `getopt' |
76 | returns the contents of the `val' field. */ | 81 | returns the contents of the `val' field. */ |
77 | 82 | ||
78 | struct option | 83 | struct option |
79 | { | 84 | { |
80 | #if __STDC__ | 85 | # if defined __STDC__ && __STDC__ |
81 | const char *name; | 86 | const char *name; |
82 | #else | 87 | # else |
83 | char *name; | 88 | char *name; |
84 | #endif | 89 | # endif |
85 | /* has_arg can't be an enum because some compilers complain about | 90 | /* has_arg can't be an enum because some compilers complain about |
86 | type mismatches in all the code that assumes it is an int. */ | 91 | type mismatches in all the code that assumes it is an int. */ |
87 | int has_arg; | 92 | int has_arg; |
88 | int *flag; | 93 | int *flag; |
89 | int val; | 94 | int val; |
90 | }; | 95 | }; |
91 | 96 | ||
92 | /* Names for the values of the `has_arg' field of `struct option'. */ | 97 | /* Names for the values of the `has_arg' field of `struct option'. */ |
93 | 98 | ||
94 | #define no_argument 0 | 99 | # define no_argument 0 |
95 | #define required_argument 1 | 100 | # define required_argument 1 |
96 | #define optional_argument 2 | 101 | # define optional_argument 2 |
102 | #endif /* need getopt */ | ||
103 | |||
97 | 104 | ||
98 | #if __STDC__ | 105 | /* Get definitions and prototypes for functions to process the |
99 | #if defined(__GNU_LIBRARY__) | 106 | arguments in ARGV (ARGC of them, minus the program name) for |
107 | options given in OPTS. | ||
108 | |||
109 | Return the option character from OPTS just read. Return -1 when | ||
110 | there are no more options. For unrecognized options, or options | ||
111 | missing arguments, `optopt' is set to the option letter, and '?' is | ||
112 | returned. | ||
113 | |||
114 | The OPTS string is a list of characters which are recognized option | ||
115 | letters, optionally followed by colons, specifying that that letter | ||
116 | takes an argument, to be placed in `optarg'. | ||
117 | |||
118 | If a letter in OPTS is followed by two colons, its argument is | ||
119 | optional. This behavior is specific to the GNU `getopt'. | ||
120 | |||
121 | The argument `--' causes premature termination of argument | ||
122 | scanning, explicitly telling `getopt' that there are no more | ||
123 | options. | ||
124 | |||
125 | If OPTS begins with `--', then non-option arguments are treated as | ||
126 | arguments to the option '\0'. This behavior is specific to the GNU | ||
127 | `getopt'. */ | ||
128 | |||
129 | #if defined __STDC__ && __STDC__ | ||
130 | # ifdef __GNU_LIBRARY__ | ||
100 | /* Many other libraries have conflicting prototypes for getopt, with | 131 | /* Many other libraries have conflicting prototypes for getopt, with |
101 | differences in the consts, in stdlib.h. To avoid compilation | 132 | differences in the consts, in stdlib.h. To avoid compilation |
102 | errors, only prototype getopt for the GNU C library. */ | 133 | errors, only prototype getopt for the GNU C library. */ |
103 | extern int getopt (int argc, char *const *argv, const char *shortopts); | 134 | extern int getopt (int __argc, char *const *__argv, const char *__shortopts); |
104 | #else /* not __GNU_LIBRARY__ */ | 135 | # else /* not __GNU_LIBRARY__ */ |
105 | extern int getopt (); | 136 | extern int getopt (); |
106 | #endif /* not __GNU_LIBRARY__ */ | 137 | # endif /* __GNU_LIBRARY__ */ |
107 | extern int getopt_long (int argc, char *const *argv, const char *shortopts, | 138 | |
108 | const struct option *longopts, int *longind); | 139 | # ifndef __need_getopt |
109 | extern int getopt_long_only (int argc, char *const *argv, | 140 | extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts, |
110 | const char *shortopts, | 141 | const struct option *__longopts, int *__longind); |
111 | const struct option *longopts, int *longind); | 142 | extern int getopt_long_only (int __argc, char *const *__argv, |
143 | const char *__shortopts, | ||
144 | const struct option *__longopts, int *__longind); | ||
112 | 145 | ||
113 | /* Internal only. Users should not call this directly. */ | 146 | /* Internal only. Users should not call this directly. */ |
114 | extern int _getopt_internal (int argc, char *const *argv, | 147 | extern int _getopt_internal (int __argc, char *const *__argv, |
115 | const char *shortopts, | 148 | const char *__shortopts, |
116 | const struct option *longopts, int *longind, | 149 | const struct option *__longopts, int *__longind, |
117 | int long_only); | 150 | int __long_only); |
118 | #else /* not __STDC__ */ | 151 | # endif |
119 | extern int getopt (); | 152 | #else /* not __STDC__ */ |
120 | extern int getopt_long (); | 153 | extern int getopt (); |
121 | extern int getopt_long_only (); | 154 | # ifndef __need_getopt |
122 | 155 | extern int getopt_long (); | |
123 | extern int _getopt_internal (); | 156 | extern int getopt_long_only (); |
124 | #endif /* not __STDC__ */ | 157 | |
158 | extern int _getopt_internal (); | ||
159 | # endif | ||
160 | #endif /* __STDC__ */ | ||
125 | 161 | ||
126 | #ifdef __cplusplus | 162 | #ifdef __cplusplus |
127 | } | 163 | } |
128 | #endif | 164 | #endif |
129 | 165 | ||
130 | #endif /* _GETOPT_H */ | 166 | /* Make sure we later can get all the definitions and declarations. */ |
167 | #undef __need_getopt | ||
168 | |||
169 | #endif /* getopt.h */ | ... | ... |
1 | /* getopt_long and getopt_long_only entry points for GNU getopt. | 1 | /* getopt_long and getopt_long_only entry points for GNU getopt. |
2 | Copyright (C) 1987, 88, 89, 90, 91, 92, 1993 | 2 | Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 |
3 | Free Software Foundation, Inc. | 3 | Free Software Foundation, Inc. |
4 | NOTE: The canonical source of this file is maintained with the GNU C Library. | ||
5 | Bugs can be reported to bug-glibc@gnu.org. | ||
4 | 6 | ||
5 | This program is free software; you can redistribute it and/or modify it | 7 | This program is free software; you can redistribute it and/or modify it |
6 | under the terms of the GNU General Public License as published by the | 8 | under the terms of the GNU General Public License as published by the |
... | @@ -13,18 +15,22 @@ | ... | @@ -13,18 +15,22 @@ |
13 | GNU General Public License for more details. | 15 | GNU General Public License for more details. |
14 | 16 | ||
15 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | 18 | along with this program; if not, write to the Free Software Foundation, |
17 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | 19 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
18 | 20 | ||
19 | #ifdef HAVE_CONFIG_H | 21 | #ifdef HAVE_CONFIG_H |
20 | #include "config.h" | 22 | #include <config.h> |
21 | #endif | 23 | #endif |
22 | 24 | ||
23 | #include "getopt.h" | 25 | #include "getopt.h" |
24 | 26 | ||
25 | #if !__STDC__ && !defined(const) && IN_GCC | 27 | #if !defined __STDC__ || !__STDC__ |
28 | /* This is a separate conditional since some stdc systems | ||
29 | reject `defined (const)'. */ | ||
30 | #ifndef const | ||
26 | #define const | 31 | #define const |
27 | #endif | 32 | #endif |
33 | #endif | ||
28 | 34 | ||
29 | #include <stdio.h> | 35 | #include <stdio.h> |
30 | 36 | ||
... | @@ -36,15 +42,21 @@ | ... | @@ -36,15 +42,21 @@ |
36 | program understand `configure --with-gnu-libc' and omit the object files, | 42 | program understand `configure --with-gnu-libc' and omit the object files, |
37 | it is simpler to just do this in the source for each such file. */ | 43 | it is simpler to just do this in the source for each such file. */ |
38 | 44 | ||
39 | #if defined (_LIBC) || !defined (__GNU_LIBRARY__) | 45 | #define GETOPT_INTERFACE_VERSION 2 |
46 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 | ||
47 | #include <gnu-versions.h> | ||
48 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION | ||
49 | #define ELIDE_CODE | ||
50 | #endif | ||
51 | #endif | ||
52 | |||
53 | #ifndef ELIDE_CODE | ||
40 | 54 | ||
41 | 55 | ||
42 | /* This needs to come after some library #include | 56 | /* This needs to come after some library #include |
43 | to get __GNU_LIBRARY__ defined. */ | 57 | to get __GNU_LIBRARY__ defined. */ |
44 | #ifdef __GNU_LIBRARY__ | 58 | #ifdef __GNU_LIBRARY__ |
45 | #include <stdlib.h> | 59 | #include <stdlib.h> |
46 | #else | ||
47 | char *getenv (); | ||
48 | #endif | 60 | #endif |
49 | 61 | ||
50 | #ifndef NULL | 62 | #ifndef NULL |
... | @@ -79,7 +91,7 @@ getopt_long_only (argc, argv, options, long_options, opt_index) | ... | @@ -79,7 +91,7 @@ getopt_long_only (argc, argv, options, long_options, opt_index) |
79 | } | 91 | } |
80 | 92 | ||
81 | 93 | ||
82 | #endif /* _LIBC or not __GNU_LIBRARY__. */ | 94 | #endif /* Not ELIDE_CODE. */ |
83 | 95 | ||
84 | #ifdef TEST | 96 | #ifdef TEST |
85 | 97 | ||
... | @@ -110,7 +122,7 @@ main (argc, argv) | ... | @@ -110,7 +122,7 @@ main (argc, argv) |
110 | 122 | ||
111 | c = getopt_long (argc, argv, "abc:d:0123456789", | 123 | c = getopt_long (argc, argv, "abc:d:0123456789", |
112 | long_options, &option_index); | 124 | long_options, &option_index); |
113 | if (c == EOF) | 125 | if (c == -1) |
114 | break; | 126 | break; |
115 | 127 | ||
116 | switch (c) | 128 | switch (c) | ... | ... |
lib/strndup.c
0 → 100644
1 | /* Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc. | ||
2 | This file is part of the GNU C Library. | ||
3 | |||
4 | The GNU C Library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public License as | ||
6 | published by the Free Software Foundation; either version 2 of the | ||
7 | License, or (at your option) any later version. | ||
8 | |||
9 | The GNU C Library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public | ||
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | ||
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include "config.h" | ||
21 | #endif | ||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <sys/types.h> | ||
25 | |||
26 | #if defined _LIBC || defined STDC_HEADERS | ||
27 | # include <stdlib.h> | ||
28 | # include <string.h> | ||
29 | #else | ||
30 | char *malloc (); | ||
31 | #endif | ||
32 | |||
33 | char * | ||
34 | strndup (s, n) | ||
35 | const char *s; | ||
36 | size_t n; | ||
37 | { | ||
38 | size_t len = strnlen (s, n); | ||
39 | char *nouveau = malloc (len + 1); | ||
40 | |||
41 | if (nouveau == NULL) | ||
42 | return NULL; | ||
43 | |||
44 | nouveau[len] = '\0'; | ||
45 | return (char *) memcpy (nouveau, s, len); | ||
46 | } |
lib/strnlen.c
0 → 100644
1 | /* Find the length of STRING, but scan at most MAXLEN characters. | ||
2 | Copyright (C) 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc. | ||
3 | This file is part of the GNU C Library. | ||
4 | |||
5 | The GNU C Library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public License as | ||
7 | published by the Free Software Foundation; either version 2 of the | ||
8 | License, or (at your option) any later version. | ||
9 | |||
10 | The GNU C Library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public | ||
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | ||
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. */ | ||
19 | |||
20 | #include <string.h> | ||
21 | |||
22 | /* Find the length of STRING, but scan at most MAXLEN characters. | ||
23 | If no '\0' terminator is found in that many characters, return MAXLEN. */ | ||
24 | |||
25 | size_t | ||
26 | strnlen (const char *string, size_t maxlen) | ||
27 | { | ||
28 | const char *end = memchr (string, '\0', maxlen); | ||
29 | return end ? (size_t) (end - string) : maxlen; | ||
30 | } |
-
Please register or sign in to post a comment