xgethostname simplification
[gnulib.git] / lib / argp-help.c
index 497bae1..364305e 100644 (file)
@@ -1,5 +1,5 @@
 /* Hierarchial argument parsing help output
-   Copyright (C) 1995-2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 1995-2003, 2004 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Written by Miles Bader <miles@gnu.ai.mit.edu>.
 
 #include <config.h>
 #endif
 
-#ifndef alloca
-# ifdef __GNUC__
-#  define alloca __builtin_alloca
-#  define HAVE_ALLOCA 1
-# else
-#  if defined HAVE_ALLOCA_H || defined _LIBC
-#   include <alloca.h>
-#  else
-#   ifdef _AIX
- #pragma alloca
-#   else
-#    ifndef alloca
-char *alloca ();
-#    endif
-#   endif
-#  endif
-# endif
-#endif
-
+#include <alloca.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 #include <stdarg.h>
-#include <malloc.h>
 #include <ctype.h>
+#include <limits.h>
 #ifdef USE_IN_LIBIO
 # include <wchar.h>
 #endif
@@ -69,9 +51,25 @@ char *alloca ();
 # endif
 #endif
 
+#ifndef _LIBC
+# if HAVE_STRERROR_R
+#  if !HAVE_DECL_STRERROR_R
+char *strerror_r (int errnum, char *buf, size_t buflen);
+#  endif
+# else
+#  if !HAVE_DECL_STRERROR
+char *strerror (int errnum);
+#  endif
+# endif
+#endif
+
 #include "argp.h"
 #include "argp-fmtstream.h"
 #include "argp-namefrob.h"
+
+#ifndef SIZE_MAX
+# define SIZE_MAX ((size_t) -1)
+#endif
 \f
 /* User-selectable (using an environment variable) formatting parameters.
 
@@ -441,6 +439,8 @@ make_hol (const struct argp *argp, struct hol_cluster *cluster)
       hol->short_options = malloc (num_short_options + 1);
 
       assert (hol->entries && hol->short_options);
+      if (SIZE_MAX <= UINT_MAX)
+       assert (hol->num_entries <= SIZE_MAX / sizeof (struct hol_entry));
 
       /* Fill in the entries.  */
       so = hol->short_options;
@@ -546,7 +546,7 @@ hol_entry_short_iterate (const struct hol_entry *entry,
 }
 
 static inline int
-__attribute ((always_inline))
+__attribute__ ((always_inline))
 hol_entry_long_iterate (const struct hol_entry *entry,
                        int (*func)(const struct argp_option *opt,
                                    const struct argp_option *real,
@@ -833,6 +833,10 @@ hol_append (struct hol *hol, struct hol *more)
          char *short_options =
            malloc (hol_so_len + strlen (more->short_options) + 1);
 
+         assert (entries && short_options);
+         if (SIZE_MAX <= UINT_MAX)
+           assert (num_entries <= SIZE_MAX / sizeof (struct hol_entry));
+
          __mempcpy (__mempcpy (entries, hol->entries,
                                hol->num_entries * sizeof (struct hol_entry)),
                     more->entries,
@@ -1529,7 +1533,9 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream,
   if (! stream)
     return;
 
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
   __flockfile (stream);
+#endif
 
   if (! uparams.valid)
     fill_in_uparams (state);
@@ -1537,7 +1543,9 @@ _help (const struct argp *argp, const struct argp_state *state, FILE *stream,
   fs = __argp_make_fmtstream (stream, 0, uparams.rmargin, 0);
   if (! fs)
     {
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
       __funlockfile (stream);
+#endif
       return;
     }
 
@@ -1645,7 +1653,9 @@ Try `%s --help' or `%s --usage' for more information.\n"),
       anything = 1;
     }
 
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
   __funlockfile (stream);
+#endif
 
   if (hol)
     hol_free (hol);
@@ -1664,6 +1674,32 @@ void __argp_help (const struct argp *argp, FILE *stream,
 weak_alias (__argp_help, argp_help)
 #endif
 
+#ifndef __argp_short_program_name
+char *__argp_basename (char *name)
+{
+  char *short_name = strrchr (name, '/');
+  return short_name ? short_name + 1 : name;
+}
+
+char *
+__argp_short_program_name (void)
+{
+# if HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
+  return program_invocation_short_name;
+# elif HAVE_DECL_PROGRAM_INVOCATION_NAME
+  return __argp_basename (program_invocation_name);
+# else
+  /* FIXME: What now? Miles suggests that it is better to use NULL,
+     but currently the value is passed on directly to fputs_unlocked,
+     so that requires more changes. */
+# if __GNUC__
+#  warning No reasonable value to return
+# endif /* __GNUC__ */
+  return "";
+# endif
+}
+#endif
+
 /* Output, if appropriate, a usage message for STATE to STREAM.  FLAGS are
    from the set ARGP_HELP_*.  */
 void
@@ -1675,7 +1711,7 @@ __argp_state_help (const struct argp_state *state, FILE *stream, unsigned flags)
        flags |= ARGP_HELP_LONG_ONLY;
 
       _help (state ? state->root_argp : 0, state, stream, flags,
-            state ? state->name : program_invocation_short_name);
+            state ? state->name : __argp_short_program_name ());
 
       if (!state || ! (state->flags & ARGP_NO_EXIT))
        {
@@ -1704,7 +1740,9 @@ __argp_error (const struct argp_state *state, const char *fmt, ...)
        {
          va_list ap;
 
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
          __flockfile (stream);
+#endif
 
          va_start (ap, fmt);
 
@@ -1716,7 +1754,7 @@ __argp_error (const struct argp_state *state, const char *fmt, ...)
              __asprintf (&buf, fmt, ap);
 
              __fwprintf (stream, L"%s: %s\n",
-                         state ? state->name : program_invocation_short_name,
+                         state ? state->name : __argp_short_program_name (),
                          buf);
 
              free (buf);
@@ -1725,7 +1763,7 @@ __argp_error (const struct argp_state *state, const char *fmt, ...)
 #endif
            {
              fputs_unlocked (state
-                             ? state->name : program_invocation_short_name,
+                             ? state->name : __argp_short_program_name (),
                              stream);
              putc_unlocked (':', stream);
              putc_unlocked (' ', stream);
@@ -1739,7 +1777,9 @@ __argp_error (const struct argp_state *state, const char *fmt, ...)
 
          va_end (ap);
 
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
          __funlockfile (stream);
+#endif
        }
     }
 }
@@ -1765,16 +1805,18 @@ __argp_failure (const struct argp_state *state, int status, int errnum,
 
       if (stream)
        {
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
          __flockfile (stream);
+#endif
 
 #ifdef USE_IN_LIBIO
          if (_IO_fwide (stream, 0) > 0)
            __fwprintf (stream, L"%s",
-                       state ? state->name : program_invocation_short_name);
+                       state ? state->name : __argp_short_program_name ());
          else
 #endif
            fputs_unlocked (state
-                           ? state->name : program_invocation_short_name,
+                           ? state->name : __argp_short_program_name (),
                            stream);
 
          if (fmt)
@@ -1818,7 +1860,11 @@ __argp_failure (const struct argp_state *state, int status, int errnum,
                {
                  putc_unlocked (':', stream);
                  putc_unlocked (' ', stream);
+#if defined _LIBC || defined HAVE_STRERROR_R
                  fputs (__strerror_r (errnum, buf, sizeof (buf)), stream);
+#else
+                 fputs (strerror (errnum), stream);
+#endif
                }
            }
 
@@ -1829,7 +1875,9 @@ __argp_failure (const struct argp_state *state, int status, int errnum,
 #endif
            putc_unlocked ('\n', stream);
 
+#if _LIBC || (HAVE_FLOCKFILE && HAVE_FUNLOCKFILE)
          __funlockfile (stream);
+#endif
 
          if (status && (!state || !(state->flags & ARGP_NO_EXIT)))
            exit (status);