[PATCH] getopt,argp - reentrant getopt interface, argp fixes
Paul Eggert
eggert@CS.UCLA.EDU
Mon Mar 8 19:33:00 GMT 2004
Marcus Brinkmann <marcus.brinkmann@ruhr-uni-bochum.de> writes:
> New patch attached. Hopefully it is converging :)
I checked it for compatibility with gnulib, and it seems OK.
coreutils builds and tests with it.
I also took the liberty of merging gnulib's changes with yours.
gnulib assumes C89 or better (and has for some time now); this
simplifies the code a bit and pacifies some compilers. The following
proposed patch to glibc assumes that your patch has been installed.
2004-03-08 Paul Eggert <eggert@cs.ucla.edu>
Merge from gnulib. We now assume C89 or better.
* getopt1.c (const): Remove.
* getopt.c (const): Likewise.
* getopt1.c (getopt_long, _getopt_long_r, getopt_long_only,
_getopt_long_only_r, main): Use prototypes, not old-style defns.
* getopt.c (exchange, _getopt_initialize, _getopt_internal_r,
_getopt_internal, getopt, main): Likewise.
* getopt.h (getopt, getopt_long, getopt_long_only): Likewise.
* getopt.c [!defined VMS || !HAVE_STRING_H]:
Include <string.h> regardless. No need for <strings.h>.
[!defined _LIBC]: Include "gettext.h" rather than rolling it ourselves.
(_): Define to gettext always.
(my_index): Remove: all uses changed to strchr.
(strlen): Remove decl.
* getopt.h (struct option.name): Always const char *.
--- libc-old/posix/getopt1.c Mon Mar 8 10:48:24 2004
+++ libc/posix/getopt1.c Mon Mar 8 11:11:49 2004
@@ -29,14 +29,6 @@
#endif
#include "getopt_int.h"
-#if !defined __STDC__ || !__STDC__
-/* This is a separate conditional since some stdc systems
- reject `defined (const)'. */
-#ifndef const
-#define const
-#endif
-#endif
-
#include <stdio.h>
/* Comment out all this code if we are using the GNU C Library, and are not
@@ -69,24 +61,16 @@
#endif
int
-getopt_long (argc, argv, options, long_options, opt_index)
- int argc;
- char *const *argv;
- const char *options;
- const struct option *long_options;
- int *opt_index;
+getopt_long (int argc, char *const *argv, const char *options,
+ const struct option *long_options, int *opt_index)
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
}
int
-_getopt_long_r (argc, argv, options, long_options, opt_index, d)
- int argc;
- char *const *argv;
- const char *options;
- const struct option *long_options;
- int *opt_index;
- struct _getopt_data *d;
+_getopt_long_r (int argc, char *const *argv, const char *options,
+ const struct option *long_options, int *opt_index,
+ struct _getopt_data *d)
{
return _getopt_internal_r (argc, argv, options, long_options, opt_index,
0, d);
@@ -98,24 +82,16 @@ _getopt_long_r (argc, argv, options, lon
instead. */
int
-getopt_long_only (argc, argv, options, long_options, opt_index)
- int argc;
- char *const *argv;
- const char *options;
- const struct option *long_options;
- int *opt_index;
+getopt_long_only (int argc, char *const *argv, const char *options,
+ const struct option *long_options, int *opt_index)
{
return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
}
int
-_getopt_long_only_r (argc, argv, options, long_options, opt_index, d)
- int argc;
- char *const *argv;
- const char *options;
- const struct option *long_options;
- int *opt_index;
- struct _getopt_data *d;
+_getopt_long_only_r (int argc, char *const *argv, const char *options,
+ const struct option *long_options, int *opt_index,
+ struct _getopt_data *d)
{
return _getopt_internal_r (argc, argv, options, long_options, opt_index,
1, d);
@@ -128,9 +104,7 @@ _getopt_long_only_r (argc, argv, options
#include <stdio.h>
int
-main (argc, argv)
- int argc;
- char **argv;
+main (int argc, char **argv)
{
int c;
int digit_optind = 0;
--- libc-old/posix/getopt.c Mon Mar 8 10:48:24 2004
+++ libc/posix/getopt.c Mon Mar 8 11:20:03 2004
@@ -31,14 +31,6 @@
# include <config.h>
#endif
-#if !defined __STDC__ || !__STDC__
-/* This is a separate conditional since some stdc systems
- reject `defined (const)'. */
-# ifndef const
-# define const
-# endif
-#endif
-
#include <stdio.h>
/* Comment out all this code if we are using the GNU C Library, and are not
@@ -69,26 +61,21 @@
# include <unistd.h>
#endif /* GNU C library. */
+#include <string.h>
+
#ifdef VMS
# include <unixlib.h>
-# if HAVE_STRING_H - 0
-# include <string.h>
-# endif
#endif
-#ifndef _
-/* This is for other GNU distributions with internationalized messages. */
-# if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
-# include <libintl.h>
-# ifndef _
-# define _(msgid) gettext (msgid)
-# endif
-# else
-# define _(msgid) (msgid)
-# endif
-# if defined _LIBC && defined USE_IN_LIBIO
-# include <wchar.h>
-# endif
+#ifdef _LIBC
+# include <libintl.h>
+#else
+# include "gettext.h"
+#endif
+#define _(msgid) gettext (msgid)
+
+#if defined _LIBC && defined USE_IN_LIBIO
+# include <wchar.h>
#endif
#ifndef attribute_hidden
@@ -151,20 +138,7 @@ int optopt = '?';
static struct _getopt_data getopt_data;
-#ifdef __GNU_LIBRARY__
-/* We want to avoid inclusion of string.h with non-GNU libraries
- because there are many ways it can cause trouble.
- On some systems, it contains special magic macros that don't work
- in GCC. */
-# include <string.h>
-# define my_index strchr
-#else
-
-# if HAVE_STRING_H
-# include <string.h>
-# else
-# include <strings.h>
-# endif
+#ifndef __GNU_LIBRARY__
/* Avoid depending on library functions or files
whose names are inconsistent. */
@@ -173,32 +147,6 @@ static struct _getopt_data getopt_data;
extern char *getenv ();
#endif
-static char *
-my_index (str, chr)
- const char *str;
- int chr;
-{
- while (*str)
- {
- if (*str == chr)
- return (char *) str;
- str++;
- }
- return 0;
-}
-
-/* If using GCC, we can safely declare strlen this way.
- If not using GCC, it is ok not to declare it. */
-#ifdef __GNUC__
-/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
- That was relevant to code that was here before. */
-# if (!defined __STDC__ || !__STDC__) && !defined strlen
-/* gcc with -traditional declares the built-in strlen to return int,
- and has done so at least since version 2.4.5. -- rms. */
-extern int strlen (const char *);
-# endif /* not __STDC__ */
-#endif /* __GNUC__ */
-
#endif /* not __GNU_LIBRARY__ */
#ifdef _LIBC
@@ -240,14 +188,8 @@ extern char *__getopt_nonoption_flags;
`first_nonopt' and `last_nonopt' are relocated so that they describe
the new indices of the non-options in ARGV after they are moved. */
-#if defined __STDC__ && __STDC__
-static void exchange (char **, struct _getopt_data *);
-#endif
-
static void
-exchange (argv, d)
- char **argv;
- struct _getopt_data *d;
+exchange (char **argv, struct _getopt_data *d)
{
int bottom = d->__first_nonopt;
int middle = d->__last_nonopt;
@@ -327,16 +269,9 @@ exchange (argv, d)
/* Initialize the internal data when the first call is made. */
-#if defined __STDC__ && __STDC__
-static const char *_getopt_initialize (int, char *const *, const char *,
- struct _getopt_data *);
-#endif
static const char *
-_getopt_initialize (argc, argv, optstring, d)
- int argc;
- char *const *argv;
- const char *optstring;
- struct _getopt_data *d;
+_getopt_initialize (int argc, char *const *argv, const char *optstring,
+ struct _getopt_data *d)
{
/* Start processing options with ARGV-element 1 (since ARGV-element 0
is the program name); the sequence of previously skipped
@@ -455,14 +390,9 @@ _getopt_initialize (argc, argv, optstrin
long-named options. */
int
-_getopt_internal_r (argc, argv, optstring, longopts, longind, long_only, d)
- int argc;
- char *const *argv;
- const char *optstring;
- const struct option *longopts;
- int *longind;
- int long_only;
- struct _getopt_data *d;
+_getopt_internal_r (int argc, char *const *argv, const char *optstring,
+ const struct option *longopts, int *longind,
+ int long_only, struct _getopt_data *d)
{
int print_errors = d->opterr;
if (optstring[0] == ':')
@@ -590,7 +520,7 @@ _getopt_internal_r (argc, argv, optstrin
if (longopts != NULL
&& (argv[d->optind][1] == '-'
|| (long_only && (argv[d->optind][2]
- || !my_index (optstring, argv[d->optind][1])))))
+ || !strchr (optstring, argv[d->optind][1])))))
{
char *nameend;
const struct option *p;
@@ -801,7 +731,7 @@ _getopt_internal_r (argc, argv, optstrin
option, then it's an error.
Otherwise interpret it as a short option. */
if (!long_only || argv[d->optind][1] == '-'
- || my_index (optstring, *d->__nextchar) == NULL)
+ || strchr (optstring, *d->__nextchar) == NULL)
{
if (print_errors)
{
@@ -864,7 +794,7 @@ _getopt_internal_r (argc, argv, optstrin
{
char c = *d->__nextchar++;
- char *temp = my_index (optstring, c);
+ char *temp = strchr (optstring, c);
/* Increment `optind' when we start to process its last character. */
if (*d->__nextchar == '\0')
@@ -1225,13 +1155,8 @@ _getopt_internal_r (argc, argv, optstrin
}
int
-_getopt_internal (argc, argv, optstring, longopts, longind, long_only)
- int argc;
- char *const *argv;
- const char *optstring;
- const struct option *longopts;
- int *longind;
- int long_only;
+_getopt_internal (int argc, char *const *argv, const char *optstring,
+ const struct option *longopts, int *longind, int long_only)
{
int result;
@@ -1249,10 +1174,7 @@ _getopt_internal (argc, argv, optstring,
}
int
-getopt (argc, argv, optstring)
- int argc;
- char *const *argv;
- const char *optstring;
+getopt (int argc, char *const *argv, const char *optstring)
{
return _getopt_internal (argc, argv, optstring,
(const struct option *) 0,
@@ -1268,9 +1190,7 @@ getopt (argc, argv, optstring)
the above definition of `getopt'. */
int
-main (argc, argv)
- int argc;
- char **argv;
+main (int argc, char **argv)
{
int c;
int digit_optind = 0;
--- libc-old/posix/getopt.h Mon Mar 8 10:48:24 2004
+++ libc/posix/getopt.h Mon Mar 8 11:14:16 2004
@@ -105,11 +105,7 @@ extern int optopt;
struct option
{
-# if (defined __STDC__ && __STDC__) || defined __cplusplus
const char *name;
-# else
- char *name;
-# endif
/* has_arg can't be an enum because some compilers complain about
type mismatches in all the code that assumes it is an int. */
int has_arg;
@@ -149,18 +145,17 @@ struct option
arguments to the option '\0'. This behavior is specific to the GNU
`getopt'. */
-#if (defined __STDC__ && __STDC__) || defined __cplusplus
-# ifdef __GNU_LIBRARY__
+#ifdef __GNU_LIBRARY__
/* Many other libraries have conflicting prototypes for getopt, with
differences in the consts, in stdlib.h. To avoid compilation
errors, only prototype getopt for the GNU C library. */
extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
__THROW;
-# else /* not __GNU_LIBRARY__ */
+#else /* not __GNU_LIBRARY__ */
extern int getopt ();
-# endif /* __GNU_LIBRARY__ */
+#endif /* __GNU_LIBRARY__ */
-# ifndef __need_getopt
+#ifndef __need_getopt
extern int getopt_long (int ___argc, char *const *___argv,
const char *__shortopts,
const struct option *__longopts, int *__longind)
@@ -170,15 +165,7 @@ extern int getopt_long_only (int ___argc
const struct option *__longopts, int *__longind)
__THROW;
-# endif
-#else /* not __STDC__ */
-extern int getopt ();
-# ifndef __need_getopt
-extern int getopt_long ();
-extern int getopt_long_only ();
-
-# endif
-#endif /* __STDC__ */
+#endif
#ifdef __cplusplus
}
More information about the Libc-alpha
mailing list