comparison lib-src/getopt.c @ 48647:47ec3831be14

(const): Move outside !HAVE_CONFIG_H conditional. (libintl.h): Include this if _LIBC. Otherwise include gettext.h. (wchar.h): Include, maybe. (attribute_hidden): Define if not defind. (__getopt_initialized): Use attribute_hidden. (__libc_argc, __libc_argv): Renamed from original_argc, etc. (__getopt_nonoption_flags, nonoption_flags_max_len, nonoption_flags_len): Conditional on USE_NONOPTION_FLAGS. (SWAP_FLAGS): New definitions. (exchange): Test USE_NONOPTION_FLAGS. (_getopt_initialize): Test USE_NONOPTION_FLAGS. (_getopt_internal): Error if argc < 1. New local var print_errors. Improve test for ambiguous long option. Add LIBIO support for error message output. (NONOPTION_P): Test USE_NONOPTION_FLAGS.
author Richard M. Stallman <rms@gnu.org>
date Wed, 04 Dec 2002 11:17:43 +0000
parents 134b57acef68
children 1bcc16abed79
comparison
equal deleted inserted replaced
48646:dfb045958eeb 48647:47ec3831be14
1 /* Getopt for GNU. 1 /* Getopt for GNU.
2 NOTE: The canonical source of this file is maintained with the GNU 2 NOTE: getopt is now part of the C library, so if you don't know what
3 C Library. Bugs can be reported to bug-glibc@gnu.org. 3 "Keep this file name-space clean" means, talk to drepper@gnu.org
4 4 before changing it!
5 Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 5 Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002
6 Free Software Foundation, Inc. 6 Free Software Foundation, Inc.
7 7 This file is part of the GNU C Library.
8 This program is free software; you can redistribute it and/or modify it 8
9 under the terms of the GNU General Public License as published by the 9 This program is free software; you can redistribute it and/or modify
10 Free Software Foundation; either version 2, or (at your option) any 10 it under the terms of the GNU General Public License as published by
11 later version. 11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
12 13
13 This program is distributed in the hope that it will be useful, 14 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details. 17 GNU General Public License for more details.
17 18
18 You should have received a copy of the GNU General Public License 19 You should have received a copy of the GNU General Public License along
19 along with this program; if not, write to the Free Software Foundation, 20 with this program; if not, write to the Free Software Foundation,
20 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 22
22 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. 23 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
23 Ditto for AIX 3.2 and <stdlib.h>. */ 24 Ditto for AIX 3.2 and <stdlib.h>. */
24 #ifndef _NO_PROTO 25 #ifndef _NO_PROTO
25 # define _NO_PROTO 26 # define _NO_PROTO
26 #endif 27 #endif
27 28
28 #ifdef HAVE_CONFIG_H 29 #ifdef HAVE_CONFIG_H
29 # include <config.h> 30 # include <config.h>
30 #else 31 #endif
31 # if !defined __STDC__ || !__STDC__ 32
33 #if !defined __STDC__ || !__STDC__
32 /* This is a separate conditional since some stdc systems 34 /* This is a separate conditional since some stdc systems
33 reject `defined (const)'. */ 35 reject `defined (const)'. */
34 # ifndef const 36 # ifndef const
35 # define const 37 # define const
36 # endif
37 # endif 38 # endif
38 #endif 39 #endif
39 40
40 #include <stdio.h> 41 #include <stdio.h>
41 42
72 # if HAVE_STRING_H - 0 73 # if HAVE_STRING_H - 0
73 # include <string.h> 74 # include <string.h>
74 # endif 75 # endif
75 #endif 76 #endif
76 77
77 #ifndef _ 78 #ifdef _LIBC
78 /* This is for other GNU distributions with internationalized messages. 79 # include <libintl.h>
79 When compiling libc, the _ macro is predefined. */ 80 #else
80 # ifdef HAVE_LIBINTL_H 81 /* This is for other GNU distributions with internationalized messages. */
81 # include <libintl.h> 82 # include "gettext.h"
82 # define _(msgid) gettext (msgid) 83 #endif
83 # else 84 #define _(msgid) gettext (msgid)
84 # define _(msgid) (msgid) 85
85 # endif 86 #if defined _LIBC && defined USE_IN_LIBIO
87 # include <wchar.h>
88 #endif
89
90 #ifndef attribute_hidden
91 # define attribute_hidden
86 #endif 92 #endif
87 93
88 /* This version of `getopt' appears to the caller like standard Unix `getopt' 94 /* This version of `getopt' appears to the caller like standard Unix `getopt'
89 but it behaves differently for the user, since it allows the user 95 but it behaves differently for the user, since it allows the user
90 to intersperse the options with the other arguments. 96 to intersperse the options with the other arguments.
126 132
127 /* Formerly, initialization of getopt depended on optind==0, which 133 /* Formerly, initialization of getopt depended on optind==0, which
128 causes problems with re-calling getopt as programs generally don't 134 causes problems with re-calling getopt as programs generally don't
129 know that. */ 135 know that. */
130 136
131 int __getopt_initialized; 137 int __getopt_initialized attribute_hidden;
132 138
133 /* The next char to be scanned in the option-element 139 /* The next char to be scanned in the option-element
134 in which the last option character we returned was found. 140 in which the last option character we returned was found.
135 This allows us to pick up the scan where we left off. 141 This allows us to pick up the scan where we left off.
136 142
245 251
246 static int first_nonopt; 252 static int first_nonopt;
247 static int last_nonopt; 253 static int last_nonopt;
248 254
249 #ifdef _LIBC 255 #ifdef _LIBC
256 /* Stored original parameters.
257 XXX This is no good solution. We should rather copy the args so
258 that we can compare them later. But we must not use malloc(3). */
259 extern int __libc_argc;
260 extern char **__libc_argv;
261
250 /* Bash 2.0 gives us an environment variable containing flags 262 /* Bash 2.0 gives us an environment variable containing flags
251 indicating ARGV elements that should not be considered arguments. */ 263 indicating ARGV elements that should not be considered arguments. */
252 264
265 # ifdef USE_NONOPTION_FLAGS
253 /* Defined in getopt_init.c */ 266 /* Defined in getopt_init.c */
254 extern char *__getopt_nonoption_flags; 267 extern char *__getopt_nonoption_flags;
255 268
256 static int nonoption_flags_max_len; 269 static int nonoption_flags_max_len;
257 static int nonoption_flags_len; 270 static int nonoption_flags_len;
258 271 # endif
259 static int original_argc; 272
260 static char *const *original_argv; 273 # ifdef USE_NONOPTION_FLAGS
261 274 # define SWAP_FLAGS(ch1, ch2) \
262 /* Make sure the environment variable bash 2.0 puts in the environment
263 is valid for the getopt call we must make sure that the ARGV passed
264 to getopt is that one passed to the process. */
265 static void
266 __attribute__ ((unused))
267 store_args_and_env (int argc, char *const *argv)
268 {
269 /* XXX This is no good solution. We should rather copy the args so
270 that we can compare them later. But we must not use malloc(3). */
271 original_argc = argc;
272 original_argv = argv;
273 }
274 # ifdef text_set_element
275 text_set_element (__libc_subinit, store_args_and_env);
276 # endif /* text_set_element */
277
278 # define SWAP_FLAGS(ch1, ch2) \
279 if (nonoption_flags_len > 0) \ 275 if (nonoption_flags_len > 0) \
280 { \ 276 { \
281 char __tmp = __getopt_nonoption_flags[ch1]; \ 277 char __tmp = __getopt_nonoption_flags[ch1]; \
282 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \ 278 __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
283 __getopt_nonoption_flags[ch2] = __tmp; \ 279 __getopt_nonoption_flags[ch2] = __tmp; \
284 } 280 }
281 # else
282 # define SWAP_FLAGS(ch1, ch2)
283 # endif
285 #else /* !_LIBC */ 284 #else /* !_LIBC */
286 # define SWAP_FLAGS(ch1, ch2) 285 # define SWAP_FLAGS(ch1, ch2)
287 #endif /* _LIBC */ 286 #endif /* _LIBC */
288 287
289 /* Exchange two adjacent subsequences of ARGV. 288 /* Exchange two adjacent subsequences of ARGV.
311 /* Exchange the shorter segment with the far end of the longer segment. 310 /* Exchange the shorter segment with the far end of the longer segment.
312 That puts the shorter segment into the right place. 311 That puts the shorter segment into the right place.
313 It leaves the longer segment in the right place overall, 312 It leaves the longer segment in the right place overall,
314 but it consists of two parts that need to be swapped next. */ 313 but it consists of two parts that need to be swapped next. */
315 314
316 #ifdef _LIBC 315 #if defined _LIBC && defined USE_NONOPTION_FLAGS
317 /* First make sure the handling of the `__getopt_nonoption_flags' 316 /* First make sure the handling of the `__getopt_nonoption_flags'
318 string can work normally. Our top argument must be in the range 317 string can work normally. Our top argument must be in the range
319 of the string. */ 318 of the string. */
320 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len) 319 if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
321 { 320 {
415 else if (posixly_correct != NULL) 414 else if (posixly_correct != NULL)
416 ordering = REQUIRE_ORDER; 415 ordering = REQUIRE_ORDER;
417 else 416 else
418 ordering = PERMUTE; 417 ordering = PERMUTE;
419 418
420 #ifdef _LIBC 419 #if defined _LIBC && defined USE_NONOPTION_FLAGS
421 if (posixly_correct == NULL 420 if (posixly_correct == NULL
422 && argc == original_argc && argv == original_argv) 421 && argc == __libc_argc && argv == __libc_argv)
423 { 422 {
424 if (nonoption_flags_max_len == 0) 423 if (nonoption_flags_max_len == 0)
425 { 424 {
426 if (__getopt_nonoption_flags == NULL 425 if (__getopt_nonoption_flags == NULL
427 || __getopt_nonoption_flags[0] == '\0') 426 || __getopt_nonoption_flags[0] == '\0')
513 const char *optstring; 512 const char *optstring;
514 const struct option *longopts; 513 const struct option *longopts;
515 int *longind; 514 int *longind;
516 int long_only; 515 int long_only;
517 { 516 {
517 int print_errors = opterr;
518 if (optstring[0] == ':')
519 print_errors = 0;
520
521 if (argc < 1)
522 return -1;
523
518 optarg = NULL; 524 optarg = NULL;
519 525
520 if (optind == 0 || !__getopt_initialized) 526 if (optind == 0 || !__getopt_initialized)
521 { 527 {
522 if (optind == 0) 528 if (optind == 0)
527 533
528 /* Test whether ARGV[optind] points to a non-option argument. 534 /* Test whether ARGV[optind] points to a non-option argument.
529 Either it does not have option syntax, or there is an environment flag 535 Either it does not have option syntax, or there is an environment flag
530 from the shell indicating it is not an option. The later information 536 from the shell indicating it is not an option. The later information
531 is only used when the used in the GNU libc. */ 537 is only used when the used in the GNU libc. */
532 #ifdef _LIBC 538 #if defined _LIBC && defined USE_NONOPTION_FLAGS
533 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \ 539 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
534 || (optind < nonoption_flags_len \ 540 || (optind < nonoption_flags_len \
535 && __getopt_nonoption_flags[optind] == '1')) 541 && __getopt_nonoption_flags[optind] == '1'))
536 #else 542 #else
537 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0') 543 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
662 { 668 {
663 /* First nonexact match found. */ 669 /* First nonexact match found. */
664 pfound = p; 670 pfound = p;
665 indfound = option_index; 671 indfound = option_index;
666 } 672 }
667 else 673 else if (long_only
674 || pfound->has_arg != p->has_arg
675 || pfound->flag != p->flag
676 || pfound->val != p->val)
668 /* Second or later nonexact match found. */ 677 /* Second or later nonexact match found. */
669 ambig = 1; 678 ambig = 1;
670 } 679 }
671 680
672 if (ambig && !exact) 681 if (ambig && !exact)
673 { 682 {
674 if (opterr) 683 if (print_errors)
675 fprintf (stderr, _("%s: option `%s' is ambiguous\n"), 684 {
676 argv[0], argv[optind]); 685 #if defined _LIBC && defined USE_IN_LIBIO
686 char *buf;
687
688 if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
689 argv[0], argv[optind]) >= 0)
690 {
691
692 if (_IO_fwide (stderr, 0) > 0)
693 __fwprintf (stderr, L"%s", buf);
694 else
695 fputs (buf, stderr);
696
697 free (buf);
698 }
699 #else
700 fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
701 argv[0], argv[optind]);
702 #endif
703 }
677 nextchar += strlen (nextchar); 704 nextchar += strlen (nextchar);
678 optind++; 705 optind++;
679 optopt = 0; 706 optopt = 0;
680 return '?'; 707 return '?';
681 } 708 }
690 allow it to be used on enums. */ 717 allow it to be used on enums. */
691 if (pfound->has_arg) 718 if (pfound->has_arg)
692 optarg = nameend + 1; 719 optarg = nameend + 1;
693 else 720 else
694 { 721 {
695 if (opterr) 722 if (print_errors)
696 { 723 {
724 #if defined _LIBC && defined USE_IN_LIBIO
725 char *buf;
726 int n;
727 #endif
728
697 if (argv[optind - 1][1] == '-') 729 if (argv[optind - 1][1] == '-')
698 /* --option */ 730 {
699 fprintf (stderr, 731 /* --option */
700 _("%s: option `--%s' doesn't allow an argument\n"), 732 #if defined _LIBC && defined USE_IN_LIBIO
701 argv[0], pfound->name); 733 n = __asprintf (&buf, _("\
734 %s: option `--%s' doesn't allow an argument\n"),
735 argv[0], pfound->name);
736 #else
737 fprintf (stderr, _("\
738 %s: option `--%s' doesn't allow an argument\n"),
739 argv[0], pfound->name);
740 #endif
741 }
702 else 742 else
703 /* +option or -option */ 743 {
704 fprintf (stderr, 744 /* +option or -option */
705 _("%s: option `%c%s' doesn't allow an argument\n"), 745 #if defined _LIBC && defined USE_IN_LIBIO
706 argv[0], argv[optind - 1][0], pfound->name); 746 n = __asprintf (&buf, _("\
747 %s: option `%c%s' doesn't allow an argument\n"),
748 argv[0], argv[optind - 1][0],
749 pfound->name);
750 #else
751 fprintf (stderr, _("\
752 %s: option `%c%s' doesn't allow an argument\n"),
753 argv[0], argv[optind - 1][0], pfound->name);
754 #endif
755 }
756
757 #if defined _LIBC && defined USE_IN_LIBIO
758 if (n >= 0)
759 {
760 if (_IO_fwide (stderr, 0) > 0)
761 __fwprintf (stderr, L"%s", buf);
762 else
763 fputs (buf, stderr);
764
765 free (buf);
766 }
767 #endif
707 } 768 }
708 769
709 nextchar += strlen (nextchar); 770 nextchar += strlen (nextchar);
710 771
711 optopt = pfound->val; 772 optopt = pfound->val;
716 { 777 {
717 if (optind < argc) 778 if (optind < argc)
718 optarg = argv[optind++]; 779 optarg = argv[optind++];
719 else 780 else
720 { 781 {
721 if (opterr) 782 if (print_errors)
722 fprintf (stderr, 783 {
723 _("%s: option `%s' requires an argument\n"), 784 #if defined _LIBC && defined USE_IN_LIBIO
724 argv[0], argv[optind - 1]); 785 char *buf;
786
787 if (__asprintf (&buf, _("\
788 %s: option `%s' requires an argument\n"),
789 argv[0], argv[optind - 1]) >= 0)
790 {
791 if (_IO_fwide (stderr, 0) > 0)
792 __fwprintf (stderr, L"%s", buf);
793 else
794 fputs (buf, stderr);
795
796 free (buf);
797 }
798 #else
799 fprintf (stderr,
800 _("%s: option `%s' requires an argument\n"),
801 argv[0], argv[optind - 1]);
802 #endif
803 }
725 nextchar += strlen (nextchar); 804 nextchar += strlen (nextchar);
726 optopt = pfound->val; 805 optopt = pfound->val;
727 return optstring[0] == ':' ? ':' : '?'; 806 return optstring[0] == ':' ? ':' : '?';
728 } 807 }
729 } 808 }
743 option, then it's an error. 822 option, then it's an error.
744 Otherwise interpret it as a short option. */ 823 Otherwise interpret it as a short option. */
745 if (!long_only || argv[optind][1] == '-' 824 if (!long_only || argv[optind][1] == '-'
746 || my_index (optstring, *nextchar) == NULL) 825 || my_index (optstring, *nextchar) == NULL)
747 { 826 {
748 if (opterr) 827 if (print_errors)
749 { 828 {
829 #if defined _LIBC && defined USE_IN_LIBIO
830 char *buf;
831 int n;
832 #endif
833
750 if (argv[optind][1] == '-') 834 if (argv[optind][1] == '-')
751 /* --option */ 835 {
752 fprintf (stderr, _("%s: unrecognized option `--%s'\n"), 836 /* --option */
753 argv[0], nextchar); 837 #if defined _LIBC && defined USE_IN_LIBIO
838 n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
839 argv[0], nextchar);
840 #else
841 fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
842 argv[0], nextchar);
843 #endif
844 }
754 else 845 else
755 /* +option or -option */ 846 {
756 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"), 847 /* +option or -option */
757 argv[0], argv[optind][0], nextchar); 848 #if defined _LIBC && defined USE_IN_LIBIO
849 n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
850 argv[0], argv[optind][0], nextchar);
851 #else
852 fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
853 argv[0], argv[optind][0], nextchar);
854 #endif
855 }
856
857 #if defined _LIBC && defined USE_IN_LIBIO
858 if (n >= 0)
859 {
860 if (_IO_fwide (stderr, 0) > 0)
861 __fwprintf (stderr, L"%s", buf);
862 else
863 fputs (buf, stderr);
864
865 free (buf);
866 }
867 #endif
758 } 868 }
759 nextchar = (char *) ""; 869 nextchar = (char *) "";
760 optind++; 870 optind++;
761 optopt = 0; 871 optopt = 0;
762 return '?'; 872 return '?';
773 if (*nextchar == '\0') 883 if (*nextchar == '\0')
774 ++optind; 884 ++optind;
775 885
776 if (temp == NULL || c == ':') 886 if (temp == NULL || c == ':')
777 { 887 {
778 if (opterr) 888 if (print_errors)
779 { 889 {
890 #if defined _LIBC && defined USE_IN_LIBIO
891 char *buf;
892 int n;
893 #endif
894
780 if (posixly_correct) 895 if (posixly_correct)
781 /* 1003.2 specifies the format of this message. */ 896 {
782 fprintf (stderr, _("%s: illegal option -- %c\n"), 897 /* 1003.2 specifies the format of this message. */
783 argv[0], c); 898 #if defined _LIBC && defined USE_IN_LIBIO
899 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
900 argv[0], c);
901 #else
902 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
903 #endif
904 }
784 else 905 else
785 fprintf (stderr, _("%s: invalid option -- %c\n"), 906 {
786 argv[0], c); 907 #if defined _LIBC && defined USE_IN_LIBIO
908 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
909 argv[0], c);
910 #else
911 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
912 #endif
913 }
914
915 #if defined _LIBC && defined USE_IN_LIBIO
916 if (n >= 0)
917 {
918 if (_IO_fwide (stderr, 0) > 0)
919 __fwprintf (stderr, L"%s", buf);
920 else
921 fputs (buf, stderr);
922
923 free (buf);
924 }
925 #endif
787 } 926 }
788 optopt = c; 927 optopt = c;
789 return '?'; 928 return '?';
790 } 929 }
791 /* Convenience. Treat POSIX -W foo same as long option --foo */ 930 /* Convenience. Treat POSIX -W foo same as long option --foo */
807 we must advance to the next element now. */ 946 we must advance to the next element now. */
808 optind++; 947 optind++;
809 } 948 }
810 else if (optind == argc) 949 else if (optind == argc)
811 { 950 {
812 if (opterr) 951 if (print_errors)
813 { 952 {
814 /* 1003.2 specifies the format of this message. */ 953 /* 1003.2 specifies the format of this message. */
954 #if defined _LIBC && defined USE_IN_LIBIO
955 char *buf;
956
957 if (__asprintf (&buf,
958 _("%s: option requires an argument -- %c\n"),
959 argv[0], c) >= 0)
960 {
961 if (_IO_fwide (stderr, 0) > 0)
962 __fwprintf (stderr, L"%s", buf);
963 else
964 fputs (buf, stderr);
965
966 free (buf);
967 }
968 #else
815 fprintf (stderr, _("%s: option requires an argument -- %c\n"), 969 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
816 argv[0], c); 970 argv[0], c);
971 #endif
817 } 972 }
818 optopt = c; 973 optopt = c;
819 if (optstring[0] == ':') 974 if (optstring[0] == ':')
820 c = ':'; 975 c = ':';
821 else 976 else
856 /* Second or later nonexact match found. */ 1011 /* Second or later nonexact match found. */
857 ambig = 1; 1012 ambig = 1;
858 } 1013 }
859 if (ambig && !exact) 1014 if (ambig && !exact)
860 { 1015 {
861 if (opterr) 1016 if (print_errors)
862 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"), 1017 {
863 argv[0], argv[optind]); 1018 #if defined _LIBC && defined USE_IN_LIBIO
1019 char *buf;
1020
1021 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
1022 argv[0], argv[optind]) >= 0)
1023 {
1024 if (_IO_fwide (stderr, 0) > 0)
1025 __fwprintf (stderr, L"%s", buf);
1026 else
1027 fputs (buf, stderr);
1028
1029 free (buf);
1030 }
1031 #else
1032 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
1033 argv[0], argv[optind]);
1034 #endif
1035 }
864 nextchar += strlen (nextchar); 1036 nextchar += strlen (nextchar);
865 optind++; 1037 optind++;
866 return '?'; 1038 return '?';
867 } 1039 }
868 if (pfound != NULL) 1040 if (pfound != NULL)
874 allow it to be used on enums. */ 1046 allow it to be used on enums. */
875 if (pfound->has_arg) 1047 if (pfound->has_arg)
876 optarg = nameend + 1; 1048 optarg = nameend + 1;
877 else 1049 else
878 { 1050 {
879 if (opterr) 1051 if (print_errors)
880 fprintf (stderr, _("\ 1052 {
1053 #if defined _LIBC && defined USE_IN_LIBIO
1054 char *buf;
1055
1056 if (__asprintf (&buf, _("\
881 %s: option `-W %s' doesn't allow an argument\n"), 1057 %s: option `-W %s' doesn't allow an argument\n"),
882 argv[0], pfound->name); 1058 argv[0], pfound->name) >= 0)
1059 {
1060 if (_IO_fwide (stderr, 0) > 0)
1061 __fwprintf (stderr, L"%s", buf);
1062 else
1063 fputs (buf, stderr);
1064
1065 free (buf);
1066 }
1067 #else
1068 fprintf (stderr, _("\
1069 %s: option `-W %s' doesn't allow an argument\n"),
1070 argv[0], pfound->name);
1071 #endif
1072 }
883 1073
884 nextchar += strlen (nextchar); 1074 nextchar += strlen (nextchar);
885 return '?'; 1075 return '?';
886 } 1076 }
887 } 1077 }
889 { 1079 {
890 if (optind < argc) 1080 if (optind < argc)
891 optarg = argv[optind++]; 1081 optarg = argv[optind++];
892 else 1082 else
893 { 1083 {
894 if (opterr) 1084 if (print_errors)
895 fprintf (stderr, 1085 {
896 _("%s: option `%s' requires an argument\n"), 1086 #if defined _LIBC && defined USE_IN_LIBIO
897 argv[0], argv[optind - 1]); 1087 char *buf;
1088
1089 if (__asprintf (&buf, _("\
1090 %s: option `%s' requires an argument\n"),
1091 argv[0], argv[optind - 1]) >= 0)
1092 {
1093 if (_IO_fwide (stderr, 0) > 0)
1094 __fwprintf (stderr, L"%s", buf);
1095 else
1096 fputs (buf, stderr);
1097
1098 free (buf);
1099 }
1100 #else
1101 fprintf (stderr,
1102 _("%s: option `%s' requires an argument\n"),
1103 argv[0], argv[optind - 1]);
1104 #endif
1105 }
898 nextchar += strlen (nextchar); 1106 nextchar += strlen (nextchar);
899 return optstring[0] == ':' ? ':' : '?'; 1107 return optstring[0] == ':' ? ':' : '?';
900 } 1108 }
901 } 1109 }
902 nextchar += strlen (nextchar); 1110 nextchar += strlen (nextchar);
936 we must advance to the next element now. */ 1144 we must advance to the next element now. */
937 optind++; 1145 optind++;
938 } 1146 }
939 else if (optind == argc) 1147 else if (optind == argc)
940 { 1148 {
941 if (opterr) 1149 if (print_errors)
942 { 1150 {
943 /* 1003.2 specifies the format of this message. */ 1151 /* 1003.2 specifies the format of this message. */
1152 #if defined _LIBC && defined USE_IN_LIBIO
1153 char *buf;
1154
1155 if (__asprintf (&buf, _("\
1156 %s: option requires an argument -- %c\n"),
1157 argv[0], c) >= 0)
1158 {
1159 if (_IO_fwide (stderr, 0) > 0)
1160 __fwprintf (stderr, L"%s", buf);
1161 else
1162 fputs (buf, stderr);
1163
1164 free (buf);
1165 }
1166 #else
944 fprintf (stderr, 1167 fprintf (stderr,
945 _("%s: option requires an argument -- %c\n"), 1168 _("%s: option requires an argument -- %c\n"),
946 argv[0], c); 1169 argv[0], c);
1170 #endif
947 } 1171 }
948 optopt = c; 1172 optopt = c;
949 if (optstring[0] == ':') 1173 if (optstring[0] == ':')
950 c = ':'; 1174 c = ':';
951 else 1175 else