diff options
Diffstat (limited to 'lib/getopt.c')
-rw-r--r-- | lib/getopt.c | 144 |
1 files changed, 38 insertions, 106 deletions
diff --git a/lib/getopt.c b/lib/getopt.c index 289d137e..6dcdbebf 100644 --- a/lib/getopt.c +++ b/lib/getopt.c | |||
@@ -2,24 +2,26 @@ | |||
2 | NOTE: getopt is now part of the C library, so if you don't know what | 2 | NOTE: getopt is now part of the C library, so if you don't know what |
3 | "Keep this file name-space clean" means, talk to drepper@gnu.org | 3 | "Keep this file name-space clean" means, talk to drepper@gnu.org |
4 | before changing it! | 4 | before changing it! |
5 | Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002 | 5 | |
6 | Free Software Foundation, Inc. | 6 | Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, |
7 | 1996, 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, | ||
8 | Inc. | ||
9 | |||
7 | This file is part of the GNU C Library. | 10 | This file is part of the GNU C Library. |
8 | 11 | ||
9 | The GNU C Library is free software; you can redistribute it and/or | 12 | This program is free software; you can redistribute it and/or modify |
10 | modify it under the terms of the GNU Lesser General Public | 13 | it under the terms of the GNU General Public License as published by |
11 | License as published by the Free Software Foundation; either | 14 | the Free Software Foundation; either version 2, or (at your option) |
12 | version 2.1 of the License, or (at your option) any later version. | 15 | any later version. |
13 | 16 | ||
14 | The GNU C Library is distributed in the hope that it will be useful, | 17 | This program is distributed in the hope that it will be useful, |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | Lesser General Public License for more details. | 20 | GNU General Public License for more details. |
18 | 21 | ||
19 | You should have received a copy of the GNU Lesser General Public | 22 | You should have received a copy of the GNU General Public License along |
20 | License along with the GNU C Library; if not, write to the Free | 23 | with this program; if not, write to the Free Software Foundation, |
21 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | 24 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
22 | 02111-1307 USA. */ | ||
23 | 25 | ||
24 | /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. | 26 | /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. |
25 | Ditto for AIX 3.2 and <stdlib.h>. */ | 27 | Ditto for AIX 3.2 and <stdlib.h>. */ |
@@ -31,14 +33,6 @@ | |||
31 | # include <config.h> | 33 | # include <config.h> |
32 | #endif | 34 | #endif |
33 | 35 | ||
34 | #if !defined __STDC__ || !__STDC__ | ||
35 | /* This is a separate conditional since some stdc systems | ||
36 | reject `defined (const)'. */ | ||
37 | # ifndef const | ||
38 | # define const | ||
39 | # endif | ||
40 | #endif | ||
41 | |||
42 | #include <stdio.h> | 36 | #include <stdio.h> |
43 | 37 | ||
44 | /* Comment out all this code if we are using the GNU C Library, and are not | 38 | /* Comment out all this code if we are using the GNU C Library, and are not |
@@ -69,26 +63,22 @@ | |||
69 | # include <unistd.h> | 63 | # include <unistd.h> |
70 | #endif /* GNU C library. */ | 64 | #endif /* GNU C library. */ |
71 | 65 | ||
66 | #include <string.h> | ||
67 | |||
72 | #ifdef VMS | 68 | #ifdef VMS |
73 | # include <unixlib.h> | 69 | # include <unixlib.h> |
74 | # if HAVE_STRING_H - 0 | ||
75 | # include <string.h> | ||
76 | # endif | ||
77 | #endif | 70 | #endif |
78 | 71 | ||
79 | #ifndef _ | 72 | #ifdef _LIBC |
73 | # include <libintl.h> | ||
74 | #else | ||
80 | /* This is for other GNU distributions with internationalized messages. */ | 75 | /* This is for other GNU distributions with internationalized messages. */ |
81 | # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC | 76 | # include "gettext.h" |
82 | # include <libintl.h> | 77 | #endif |
83 | # ifndef _ | 78 | #define _(msgid) gettext (msgid) |
84 | # define _(msgid) gettext (msgid) | 79 | |
85 | # endif | 80 | #if defined _LIBC && defined USE_IN_LIBIO |
86 | # else | 81 | # include <wchar.h> |
87 | # define _(msgid) (msgid) | ||
88 | # endif | ||
89 | # if defined _LIBC && defined USE_IN_LIBIO | ||
90 | # include <wchar.h> | ||
91 | # endif | ||
92 | #endif | 82 | #endif |
93 | 83 | ||
94 | #ifndef attribute_hidden | 84 | #ifndef attribute_hidden |
@@ -197,20 +187,7 @@ static enum | |||
197 | /* Value of POSIXLY_CORRECT environment variable. */ | 187 | /* Value of POSIXLY_CORRECT environment variable. */ |
198 | static char *posixly_correct; | 188 | static char *posixly_correct; |
199 | 189 | ||
200 | #ifdef __GNU_LIBRARY__ | 190 | #ifndef __GNU_LIBRARY__ |
201 | /* We want to avoid inclusion of string.h with non-GNU libraries | ||
202 | because there are many ways it can cause trouble. | ||
203 | On some systems, it contains special magic macros that don't work | ||
204 | in GCC. */ | ||
205 | # include <string.h> | ||
206 | # define my_index strchr | ||
207 | #else | ||
208 | |||
209 | # if HAVE_STRING_H | ||
210 | # include <string.h> | ||
211 | # else | ||
212 | # include <strings.h> | ||
213 | # endif | ||
214 | 191 | ||
215 | /* Avoid depending on library functions or files | 192 | /* Avoid depending on library functions or files |
216 | whose names are inconsistent. */ | 193 | whose names are inconsistent. */ |
@@ -219,32 +196,6 @@ static char *posixly_correct; | |||
219 | extern char *getenv (); | 196 | extern char *getenv (); |
220 | #endif | 197 | #endif |
221 | 198 | ||
222 | static char * | ||
223 | my_index (str, chr) | ||
224 | const char *str; | ||
225 | int chr; | ||
226 | { | ||
227 | while (*str) | ||
228 | { | ||
229 | if (*str == chr) | ||
230 | return (char *) str; | ||
231 | str++; | ||
232 | } | ||
233 | return 0; | ||
234 | } | ||
235 | |||
236 | /* If using GCC, we can safely declare strlen this way. | ||
237 | If not using GCC, it is ok not to declare it. */ | ||
238 | #ifdef __GNUC__ | ||
239 | /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h. | ||
240 | That was relevant to code that was here before. */ | ||
241 | # if (!defined __STDC__ || !__STDC__) && !defined strlen | ||
242 | /* gcc with -traditional declares the built-in strlen to return int, | ||
243 | and has done so at least since version 2.4.5. -- rms. */ | ||
244 | extern int strlen (const char *); | ||
245 | # endif /* not __STDC__ */ | ||
246 | #endif /* __GNUC__ */ | ||
247 | |||
248 | #endif /* not __GNU_LIBRARY__ */ | 199 | #endif /* not __GNU_LIBRARY__ */ |
249 | 200 | ||
250 | /* Handle permutation of arguments. */ | 201 | /* Handle permutation of arguments. */ |
@@ -298,13 +249,8 @@ static int nonoption_flags_len; | |||
298 | `first_nonopt' and `last_nonopt' are relocated so that they describe | 249 | `first_nonopt' and `last_nonopt' are relocated so that they describe |
299 | the new indices of the non-options in ARGV after they are moved. */ | 250 | the new indices of the non-options in ARGV after they are moved. */ |
300 | 251 | ||
301 | #if defined __STDC__ && __STDC__ | ||
302 | static void exchange (char **); | ||
303 | #endif | ||
304 | |||
305 | static void | 252 | static void |
306 | exchange (argv) | 253 | exchange (char **argv) |
307 | char **argv; | ||
308 | { | 254 | { |
309 | int bottom = first_nonopt; | 255 | int bottom = first_nonopt; |
310 | int middle = last_nonopt; | 256 | int middle = last_nonopt; |
@@ -384,14 +330,8 @@ exchange (argv) | |||
384 | 330 | ||
385 | /* Initialize the internal data when the first call is made. */ | 331 | /* Initialize the internal data when the first call is made. */ |
386 | 332 | ||
387 | #if defined __STDC__ && __STDC__ | ||
388 | static const char *_getopt_initialize (int, char *const *, const char *); | ||
389 | #endif | ||
390 | static const char * | 333 | static const char * |
391 | _getopt_initialize (argc, argv, optstring) | 334 | _getopt_initialize (int argc, char *const *argv, const char *optstring) |
392 | int argc; | ||
393 | char *const *argv; | ||
394 | const char *optstring; | ||
395 | { | 335 | { |
396 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 | 336 | /* Start processing options with ARGV-element 1 (since ARGV-element 0 |
397 | is the program name); the sequence of previously skipped | 337 | is the program name); the sequence of previously skipped |
@@ -510,13 +450,9 @@ _getopt_initialize (argc, argv, optstring) | |||
510 | long-named options. */ | 450 | long-named options. */ |
511 | 451 | ||
512 | int | 452 | int |
513 | _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | 453 | _getopt_internal (int argc, char *const *argv, |
514 | int argc; | 454 | const char *optstring, const struct option *longopts, |
515 | char *const *argv; | 455 | int *longind, int long_only) |
516 | const char *optstring; | ||
517 | const struct option *longopts; | ||
518 | int *longind; | ||
519 | int long_only; | ||
520 | { | 456 | { |
521 | int print_errors = opterr; | 457 | int print_errors = opterr; |
522 | if (optstring[0] == ':') | 458 | if (optstring[0] == ':') |
@@ -641,7 +577,8 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |||
641 | 577 | ||
642 | if (longopts != NULL | 578 | if (longopts != NULL |
643 | && (argv[optind][1] == '-' | 579 | && (argv[optind][1] == '-' |
644 | || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1]))))) | 580 | || (long_only |
581 | && (argv[optind][2] || !strchr (optstring, argv[optind][1]))))) | ||
645 | { | 582 | { |
646 | char *nameend; | 583 | char *nameend; |
647 | const struct option *p; | 584 | const struct option *p; |
@@ -826,7 +763,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |||
826 | option, then it's an error. | 763 | option, then it's an error. |
827 | Otherwise interpret it as a short option. */ | 764 | Otherwise interpret it as a short option. */ |
828 | if (!long_only || argv[optind][1] == '-' | 765 | if (!long_only || argv[optind][1] == '-' |
829 | || my_index (optstring, *nextchar) == NULL) | 766 | || strchr (optstring, *nextchar) == NULL) |
830 | { | 767 | { |
831 | if (print_errors) | 768 | if (print_errors) |
832 | { | 769 | { |
@@ -881,7 +818,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |||
881 | 818 | ||
882 | { | 819 | { |
883 | char c = *nextchar++; | 820 | char c = *nextchar++; |
884 | char *temp = my_index (optstring, c); | 821 | char *temp = strchr (optstring, c); |
885 | 822 | ||
886 | /* Increment `optind' when we start to process its last character. */ | 823 | /* Increment `optind' when we start to process its last character. */ |
887 | if (*nextchar == '\0') | 824 | if (*nextchar == '\0') |
@@ -1191,10 +1128,7 @@ _getopt_internal (argc, argv, optstring, longopts, longind, long_only) | |||
1191 | } | 1128 | } |
1192 | 1129 | ||
1193 | int | 1130 | int |
1194 | getopt (argc, argv, optstring) | 1131 | getopt (int argc, char *const *argv, const char *optstring) |
1195 | int argc; | ||
1196 | char *const *argv; | ||
1197 | const char *optstring; | ||
1198 | { | 1132 | { |
1199 | return _getopt_internal (argc, argv, optstring, | 1133 | return _getopt_internal (argc, argv, optstring, |
1200 | (const struct option *) 0, | 1134 | (const struct option *) 0, |
@@ -1210,9 +1144,7 @@ getopt (argc, argv, optstring) | |||
1210 | the above definition of `getopt'. */ | 1144 | the above definition of `getopt'. */ |
1211 | 1145 | ||
1212 | int | 1146 | int |
1213 | main (argc, argv) | 1147 | main (int argc, char **argv) |
1214 | int argc; | ||
1215 | char **argv; | ||
1216 | { | 1148 | { |
1217 | int c; | 1149 | int c; |
1218 | int digit_optind = 0; | 1150 | int digit_optind = 0; |