/[inetutils]/inetutils/libinetutils/getopt.c
ViewVC logotype

Diff of /inetutils/libinetutils/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by miles, Mon Sep 22 20:30:12 1997 UTC revision 1.2 by jbailey, Wed Dec 11 13:19:52 2002 UTC
# Line 1  Line 1 
1  /* Getopt for GNU.  /* Getopt for GNU.
2     NOTE: getopt is now part of the C library, so if you don't know what     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 roland@gnu.ai.mit.edu     "Keep this file name-space clean" means, talk to drepper@gnu.org
4     before changing it!     before changing it!
5       Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002
    Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97  
6          Free Software Foundation, Inc.          Free Software Foundation, Inc.
7       This file is part of the GNU C Library.
8    
9     NOTE: The canonical source of this file is maintained with the GNU C Library.     This program is free software; you can redistribute it and/or modify
10     Bugs can be reported to bug-glibc@prep.ai.mit.edu.     it under the terms of the GNU General Public License as published by
11       the Free Software Foundation; either version 2, or (at your option)
12     This program is free software; you can redistribute it and/or modify it     any later version.
    under the terms of the GNU General Public License as published by the  
    Free Software Foundation; either version 2, or (at your option) any  
    later version.  
13    
14     This program is distributed in the hope that it will be useful,     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.     GNU General Public License for more details.
18    
19     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU General Public License along
20     along with this program; if not, write to the Free Software     with this program; if not, write to the Free Software Foundation,
21     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
    USA.  */  
22    
23  /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.  /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24     Ditto for AIX 3.2 and <stdlib.h>.  */     Ditto for AIX 3.2 and <stdlib.h>.  */
25  #ifndef _NO_PROTO  #ifndef _NO_PROTO
26  #define _NO_PROTO  # define _NO_PROTO
27  #endif  #endif
28    
29  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
30  #include <config.h>  # include <config.h>
31  #endif  #endif
32    
33  #if !defined (__STDC__) || !__STDC__  #if !defined __STDC__ || !__STDC__
34  /* This is a separate conditional since some stdc systems  /* This is a separate conditional since some stdc systems
35     reject `defined (const)'.  */     reject `defined (const)'.  */
36  #ifndef const  # ifndef const
37  #define const  #  define const
38  #endif  # endif
39  #endif  #endif
40    
41  #include <stdio.h>  #include <stdio.h>
# Line 53  Line 49 
49     it is simpler to just do this in the source for each such file.  */     it is simpler to just do this in the source for each such file.  */
50    
51  #define GETOPT_INTERFACE_VERSION 2  #define GETOPT_INTERFACE_VERSION 2
52  #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2  #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
53  #include <gnu-versions.h>  # include <gnu-versions.h>
54  #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION  # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
55  #define ELIDE_CODE  #  define ELIDE_CODE
56  #endif  # endif
57  #endif  #endif
58    
59  #ifndef ELIDE_CODE  #ifndef ELIDE_CODE
# Line 68  Line 64 
64  #ifdef  __GNU_LIBRARY__  #ifdef  __GNU_LIBRARY__
65  /* Don't include stdlib.h for non-GNU C libraries because some of them  /* Don't include stdlib.h for non-GNU C libraries because some of them
66     contain conflicting prototypes for getopt.  */     contain conflicting prototypes for getopt.  */
67  #include <stdlib.h>  # include <stdlib.h>
68  #include <unistd.h>  # include <unistd.h>
69  #endif  /* GNU C library.  */  #endif  /* GNU C library.  */
70    
71  #ifdef VMS  #ifdef VMS
72  #include <unixlib.h>  # include <unixlib.h>
73  #if HAVE_STRING_H - 0  # if HAVE_STRING_H - 0
74  #include <string.h>  #  include <string.h>
75  #endif  # endif
 #endif  
   
 #if defined (WINDOWS32) && !defined (__CYGWIN32__)  
 /* It's not Unix, really.  See?  Capital letters.  */  
 #include <windows.h>  
 #define getpid() GetCurrentProcessId()  
76  #endif  #endif
77    
78  #ifndef _  #ifdef _LIBC
 /* This is for other GNU distributions with internationalized messages.  
    When compiling libc, the _ macro is predefined.  */  
 #ifdef HAVE_LIBINTL_H  
79  # include <libintl.h>  # include <libintl.h>
 # define _(msgid)       gettext (msgid)  
80  #else  #else
81  # define _(msgid)       (msgid)  /* This is for other GNU distributions with internationalized messages.  */
82    # include "gettext.h"
83  #endif  #endif
84    #define _(msgid) gettext (msgid)
85    
86    #if defined _LIBC && defined USE_IN_LIBIO
87    # include <wchar.h>
88    #endif
89    
90    #ifndef attribute_hidden
91    # define attribute_hidden
92  #endif  #endif
93    
94  /* This version of `getopt' appears to the caller like standard Unix `getopt'  /* This version of `getopt' appears to the caller like standard Unix `getopt'
# Line 118  Line 113 
113     Also, when `ordering' is RETURN_IN_ORDER,     Also, when `ordering' is RETURN_IN_ORDER,
114     each non-option ARGV-element is returned here.  */     each non-option ARGV-element is returned here.  */
115    
116  char *optarg = NULL;  char *optarg;
117    
118  /* Index in ARGV of the next element to be scanned.  /* Index in ARGV of the next element to be scanned.
119     This is used for communication to and from the caller     This is used for communication to and from the caller
# Line 139  int optind = 1; Line 134  int optind = 1;
134     causes problems with re-calling getopt as programs generally don't     causes problems with re-calling getopt as programs generally don't
135     know that. */     know that. */
136    
137  int __getopt_initialized = 0;  int __getopt_initialized attribute_hidden;
138    
139  /* The next char to be scanned in the option-element  /* The next char to be scanned in the option-element
140     in which the last option character we returned was found.     in which the last option character we returned was found.
# Line 203  static char *posixly_correct; Line 198  static char *posixly_correct;
198     because there are many ways it can cause trouble.     because there are many ways it can cause trouble.
199     On some systems, it contains special magic macros that don't work     On some systems, it contains special magic macros that don't work
200     in GCC.  */     in GCC.  */
201  #include <string.h>  # include <string.h>
202  #define my_index        strchr  # define my_index       strchr
203  #else  #else
204    
205    # if HAVE_STRING_H
206    #  include <string.h>
207    # else
208    #  include <strings.h>
209    # endif
210    
211  /* Avoid depending on library functions or files  /* Avoid depending on library functions or files
212     whose names are inconsistent.  */     whose names are inconsistent.  */
213    
214  char *getenv ();  #ifndef getenv
215    extern char *getenv ();
216    #endif
217    
218  static char *  static char *
219  my_index (str, chr)  my_index (str, chr)
# Line 231  my_index (str, chr) Line 234  my_index (str, chr)
234  #ifdef __GNUC__  #ifdef __GNUC__
235  /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.  /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
236     That was relevant to code that was here before.  */     That was relevant to code that was here before.  */
237  #if !defined (__STDC__) || !__STDC__  # if (!defined __STDC__ || !__STDC__) && !defined strlen
238  /* gcc with -traditional declares the built-in strlen to return int,  /* gcc with -traditional declares the built-in strlen to return int,
239     and has done so at least since version 2.4.5. -- rms.  */     and has done so at least since version 2.4.5. -- rms.  */
240  extern int strlen (const char *);  extern int strlen (const char *);
241  #endif /* not __STDC__ */  # endif /* not __STDC__ */
242  #endif /* __GNUC__ */  #endif /* __GNUC__ */
243    
244  #endif /* not __GNU_LIBRARY__ */  #endif /* not __GNU_LIBRARY__ */
# Line 250  static int first_nonopt; Line 253  static int first_nonopt;
253  static int last_nonopt;  static int last_nonopt;
254    
255  #ifdef _LIBC  #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    
262  /* Bash 2.0 gives us an environment variable containing flags  /* Bash 2.0 gives us an environment variable containing flags
263     indicating ARGV elements that should not be considered arguments.  */     indicating ARGV elements that should not be considered arguments.  */
264    
265    # ifdef USE_NONOPTION_FLAGS
266  /* Defined in getopt_init.c  */  /* Defined in getopt_init.c  */
267  extern char *__getopt_nonoption_flags;  extern char *__getopt_nonoption_flags;
268    
269  static int nonoption_flags_max_len;  static int nonoption_flags_max_len;
270  static int nonoption_flags_len;  static int nonoption_flags_len;
271    # endif
272    
273  static int original_argc;  # ifdef USE_NONOPTION_FLAGS
274  static char *const *original_argv;  #  define SWAP_FLAGS(ch1, ch2) \
   
 extern pid_t __libc_pid;  
   
 /* Make sure the environment variable bash 2.0 puts in the environment  
    is valid for the getopt call we must make sure that the ARGV passed  
    to getopt is that one passed to the process.  */  
 static void  
 __attribute__ ((unused))  
 store_args_and_env (int argc, char *const *argv)  
 {  
   /* XXX This is no good solution.  We should rather copy the args so  
      that we can compare them later.  But we must not use malloc(3).  */  
   original_argc = argc;  
   original_argv = argv;  
 }  
 # ifdef text_set_element  
 text_set_element (__libc_subinit, store_args_and_env);  
 # endif /* text_set_element */  
   
 # define SWAP_FLAGS(ch1, ch2) \  
275    if (nonoption_flags_len > 0)                                                \    if (nonoption_flags_len > 0)                                                \
276      {                                                                         \      {                                                                         \
277        char __tmp = __getopt_nonoption_flags[ch1];                             \        char __tmp = __getopt_nonoption_flags[ch1];                             \
278        __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \        __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
279        __getopt_nonoption_flags[ch2] = __tmp;                                  \        __getopt_nonoption_flags[ch2] = __tmp;                                  \
280      }      }
281    # else
282    #  define SWAP_FLAGS(ch1, ch2)
283    # endif
284  #else   /* !_LIBC */  #else   /* !_LIBC */
285  # define SWAP_FLAGS(ch1, ch2)  # define SWAP_FLAGS(ch1, ch2)
286  #endif  /* _LIBC */  #endif  /* _LIBC */
# Line 300  text_set_element (__libc_subinit, store_ Line 294  text_set_element (__libc_subinit, store_
294     `first_nonopt' and `last_nonopt' are relocated so that they describe     `first_nonopt' and `last_nonopt' are relocated so that they describe
295     the new indices of the non-options in ARGV after they are moved.  */     the new indices of the non-options in ARGV after they are moved.  */
296    
297  #if defined (__STDC__) && __STDC__  #if defined __STDC__ && __STDC__
298  static void exchange (char **);  static void exchange (char **);
299  #endif  #endif
300    
# Line 318  exchange (argv) Line 312  exchange (argv)
312       It leaves the longer segment in the right place overall,       It leaves the longer segment in the right place overall,
313       but it consists of two parts that need to be swapped next.  */       but it consists of two parts that need to be swapped next.  */
314    
315  #ifdef _LIBC  #if defined _LIBC && defined USE_NONOPTION_FLAGS
316    /* First make sure the handling of the `__getopt_nonoption_flags'    /* First make sure the handling of the `__getopt_nonoption_flags'
317       string can work normally.  Our top argument must be in the range       string can work normally.  Our top argument must be in the range
318       of the string.  */       of the string.  */
# Line 331  exchange (argv) Line 325  exchange (argv)
325          nonoption_flags_len = nonoption_flags_max_len = 0;          nonoption_flags_len = nonoption_flags_max_len = 0;
326        else        else
327          {          {
328            memcpy (new_str, __getopt_nonoption_flags, nonoption_flags_max_len);            memset (__mempcpy (new_str, __getopt_nonoption_flags,
329            memset (&new_str[nonoption_flags_max_len], '\0',                               nonoption_flags_max_len),
330                    top + 1 - nonoption_flags_max_len);                    '\0', top + 1 - nonoption_flags_max_len);
331            nonoption_flags_max_len = top + 1;            nonoption_flags_max_len = top + 1;
332            __getopt_nonoption_flags = new_str;            __getopt_nonoption_flags = new_str;
333          }          }
# Line 386  exchange (argv) Line 380  exchange (argv)
380    
381  /* Initialize the internal data when the first call is made.  */  /* Initialize the internal data when the first call is made.  */
382    
383  #if defined (__STDC__) && __STDC__  #if defined __STDC__ && __STDC__
384  static const char *_getopt_initialize (int, char *const *, const char *);  static const char *_getopt_initialize (int, char *const *, const char *);
385  #endif  #endif
386  static const char *  static const char *
# Line 422  _getopt_initialize (argc, argv, optstrin Line 416  _getopt_initialize (argc, argv, optstrin
416    else    else
417      ordering = PERMUTE;      ordering = PERMUTE;
418    
419  #ifdef _LIBC  #if defined _LIBC && defined USE_NONOPTION_FLAGS
420    if (posixly_correct == NULL    if (posixly_correct == NULL
421        && argc == original_argc && argv == original_argv)        && argc == __libc_argc && argv == __libc_argv)
422      {      {
423        if (nonoption_flags_max_len == 0)        if (nonoption_flags_max_len == 0)
424          {          {
# Line 442  _getopt_initialize (argc, argv, optstrin Line 436  _getopt_initialize (argc, argv, optstrin
436                if (__getopt_nonoption_flags == NULL)                if (__getopt_nonoption_flags == NULL)
437                  nonoption_flags_max_len = -1;                  nonoption_flags_max_len = -1;
438                else                else
439                  {                  memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
440                    memcpy (__getopt_nonoption_flags, orig_str, len);                          '\0', nonoption_flags_max_len - len);
                   memset (&__getopt_nonoption_flags[len], '\0',  
                           nonoption_flags_max_len - len);  
                 }  
441              }              }
442          }          }
443        nonoption_flags_len = nonoption_flags_max_len;        nonoption_flags_len = nonoption_flags_max_len;
# Line 523  _getopt_internal (argc, argv, optstring, Line 514  _getopt_internal (argc, argv, optstring,
514       int *longind;       int *longind;
515       int long_only;       int long_only;
516  {  {
517      int print_errors = opterr;
518      if (optstring[0] == ':')
519        print_errors = 0;
520    
521      if (argc < 1)
522        return -1;
523    
524    optarg = NULL;    optarg = NULL;
525    
526    if (optind == 0 || !__getopt_initialized)    if (optind == 0 || !__getopt_initialized)
# Line 537  _getopt_internal (argc, argv, optstring, Line 535  _getopt_internal (argc, argv, optstring,
535       Either it does not have option syntax, or there is an environment flag       Either it does not have option syntax, or there is an environment flag
536       from the shell indicating it is not an option.  The later information       from the shell indicating it is not an option.  The later information
537       is only used when the used in the GNU libc.  */       is only used when the used in the GNU libc.  */
538  #ifdef _LIBC  #if defined _LIBC && defined USE_NONOPTION_FLAGS
539  #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'        \  # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'       \
540                       || (optind < nonoption_flags_len                         \                        || (optind < nonoption_flags_len                        \
541                           && __getopt_nonoption_flags[optind] == '1'))                            && __getopt_nonoption_flags[optind] == '1'))
542  #else  #else
543  #define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')  # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
544  #endif  #endif
545    
546    if (nextchar == NULL || *nextchar == '\0')    if (nextchar == NULL || *nextchar == '\0')
# Line 672  _getopt_internal (argc, argv, optstring, Line 670  _getopt_internal (argc, argv, optstring,
670                  pfound = p;                  pfound = p;
671                  indfound = option_index;                  indfound = option_index;
672                }                }
673              else              else if (long_only
674                         || pfound->has_arg != p->has_arg
675                         || pfound->flag != p->flag
676                         || pfound->val != p->val)
677                /* Second or later nonexact match found.  */                /* Second or later nonexact match found.  */
678                ambig = 1;                ambig = 1;
679            }            }
680    
681        if (ambig && !exact)        if (ambig && !exact)
682          {          {
683            if (opterr)            if (print_errors)
684              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),              {
685                       argv[0], argv[optind]);  #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                }
704            nextchar += strlen (nextchar);            nextchar += strlen (nextchar);
705            optind++;            optind++;
706            optopt = 0;            optopt = 0;
# Line 700  _getopt_internal (argc, argv, optstring, Line 719  _getopt_internal (argc, argv, optstring,
719                  optarg = nameend + 1;                  optarg = nameend + 1;
720                else                else
721                  {                  {
722                    if (opterr)                    if (print_errors)
723                     if (argv[optind - 1][1] == '-')                      {
724                      /* --option */  #if defined _LIBC && defined USE_IN_LIBIO
725                      fprintf (stderr,                        char *buf;
726                       _("%s: option `--%s' doesn't allow an argument\n"),                        int n;
727                       argv[0], pfound->name);  #endif
728                     else  
729                      /* +option or -option */                        if (argv[optind - 1][1] == '-')
730                      fprintf (stderr,                          {
731                       _("%s: option `%c%s' doesn't allow an argument\n"),                            /* --option */
732                       argv[0], argv[optind - 1][0], pfound->name);  #if defined _LIBC && defined USE_IN_LIBIO
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                            }
742                          else
743                            {
744                              /* +option or -option */
745    #if defined _LIBC && defined USE_IN_LIBIO
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
768                        }
769    
770                    nextchar += strlen (nextchar);                    nextchar += strlen (nextchar);
771    
# Line 724  _getopt_internal (argc, argv, optstring, Line 779  _getopt_internal (argc, argv, optstring,
779                  optarg = argv[optind++];                  optarg = argv[optind++];
780                else                else
781                  {                  {
782                    if (opterr)                    if (print_errors)
783                      fprintf (stderr,                      {
784                             _("%s: option `%s' requires an argument\n"),  #if defined _LIBC && defined USE_IN_LIBIO
785                             argv[0], argv[optind - 1]);                        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                        }
804                    nextchar += strlen (nextchar);                    nextchar += strlen (nextchar);
805                    optopt = pfound->val;                    optopt = pfound->val;
806                    return optstring[0] == ':' ? ':' : '?';                    return optstring[0] == ':' ? ':' : '?';
# Line 751  _getopt_internal (argc, argv, optstring, Line 824  _getopt_internal (argc, argv, optstring,
824        if (!long_only || argv[optind][1] == '-'        if (!long_only || argv[optind][1] == '-'
825            || my_index (optstring, *nextchar) == NULL)            || my_index (optstring, *nextchar) == NULL)
826          {          {
827            if (opterr)            if (print_errors)
828              {              {
829    #if defined _LIBC && defined USE_IN_LIBIO
830                  char *buf;
831                  int n;
832    #endif
833    
834                if (argv[optind][1] == '-')                if (argv[optind][1] == '-')
835                  /* --option */                  {
836                  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),                    /* --option */
837                           argv[0], nextchar);  #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                    }
845                else                else
846                  /* +option or -option */                  {
847                  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),                    /* +option or -option */
848                           argv[0], argv[optind][0], nextchar);  #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
868              }              }
869            nextchar = (char *) "";            nextchar = (char *) "";
870            optind++;            optind++;
# Line 781  _getopt_internal (argc, argv, optstring, Line 885  _getopt_internal (argc, argv, optstring,
885    
886      if (temp == NULL || c == ':')      if (temp == NULL || c == ':')
887        {        {
888          if (opterr)          if (print_errors)
889            {            {
890    #if defined _LIBC && defined USE_IN_LIBIO
891                  char *buf;
892                  int n;
893    #endif
894    
895              if (posixly_correct)              if (posixly_correct)
896                /* 1003.2 specifies the format of this message.  */                {
897                fprintf (stderr, _("%s: illegal option -- %c\n"),                  /* 1003.2 specifies the format of this message.  */
898                         argv[0], c);  #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                  }
905              else              else
906                fprintf (stderr, _("%s: invalid option -- %c\n"),                {
907                         argv[0], c);  #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
926            }            }
927          optopt = c;          optopt = c;
928          return '?';          return '?';
# Line 815  _getopt_internal (argc, argv, optstring, Line 948  _getopt_internal (argc, argv, optstring,
948            }            }
949          else if (optind == argc)          else if (optind == argc)
950            {            {
951              if (opterr)              if (print_errors)
952                {                {
953                  /* 1003.2 specifies the format of this message.  */                  /* 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
969                  fprintf (stderr, _("%s: option requires an argument -- %c\n"),                  fprintf (stderr, _("%s: option requires an argument -- %c\n"),
970                           argv[0], c);                           argv[0], c);
971    #endif
972                }                }
973              optopt = c;              optopt = c;
974              if (optstring[0] == ':')              if (optstring[0] == ':')
# Line 864  _getopt_internal (argc, argv, optstring, Line 1013  _getopt_internal (argc, argv, optstring,
1013              }              }
1014          if (ambig && !exact)          if (ambig && !exact)
1015            {            {
1016              if (opterr)              if (print_errors)
1017                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),                {
1018                         argv[0], argv[optind]);  #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                  }
1036              nextchar += strlen (nextchar);              nextchar += strlen (nextchar);
1037              optind++;              optind++;
1038              return '?';              return '?';
# Line 882  _getopt_internal (argc, argv, optstring, Line 1048  _getopt_internal (argc, argv, optstring,
1048                    optarg = nameend + 1;                    optarg = nameend + 1;
1049                  else                  else
1050                    {                    {
1051                      if (opterr)                      if (print_errors)
1052                        fprintf (stderr, _("\                        {
1053    #if defined _LIBC && defined USE_IN_LIBIO
1054                            char *buf;
1055    
1056                            if (__asprintf (&buf, _("\
1057  %s: option `-W %s' doesn't allow an argument\n"),  %s: option `-W %s' doesn't allow an argument\n"),
1058                                 argv[0], pfound->name);                                          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                          }
1073    
1074                      nextchar += strlen (nextchar);                      nextchar += strlen (nextchar);
1075                      return '?';                      return '?';
# Line 897  _getopt_internal (argc, argv, optstring, Line 1081  _getopt_internal (argc, argv, optstring,
1081                    optarg = argv[optind++];                    optarg = argv[optind++];
1082                  else                  else
1083                    {                    {
1084                      if (opterr)                      if (print_errors)
1085                        fprintf (stderr,                        {
1086                                 _("%s: option `%s' requires an argument\n"),  #if defined _LIBC && defined USE_IN_LIBIO
1087                                 argv[0], argv[optind - 1]);                          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                          }
1106                      nextchar += strlen (nextchar);                      nextchar += strlen (nextchar);
1107                      return optstring[0] == ':' ? ':' : '?';                      return optstring[0] == ':' ? ':' : '?';
1108                    }                    }
# Line 944  _getopt_internal (argc, argv, optstring, Line 1146  _getopt_internal (argc, argv, optstring,
1146                }                }
1147              else if (optind == argc)              else if (optind == argc)
1148                {                {
1149                  if (opterr)                  if (print_errors)
1150                    {                    {
1151                      /* 1003.2 specifies the format of this message.  */                      /* 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
1167                      fprintf (stderr,                      fprintf (stderr,
1168                             _("%s: option requires an argument -- %c\n"),                               _("%s: option requires an argument -- %c\n"),
1169                             argv[0], c);                               argv[0], c);
1170    #endif
1171                    }                    }
1172                  optopt = c;                  optopt = c;
1173                  if (optstring[0] == ':')                  if (optstring[0] == ':')

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26