bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-gnulib] getopt fix to prevent it from lying to the compiler about c


From: Paul Eggert
Subject: [Bug-gnulib] getopt fix to prevent it from lying to the compiler about const
Date: Tue, 16 Nov 2004 17:59:33 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

Paul Eggert <address@hidden> writes:

> I installed this:

On further thought that change was too ambitious, since it would cause
libc to change incompatibility and so the libc maintainers would never
buy it back.  I installed this further patch to help address that
problem.  This doesn't change gnulib behavior, only libc (assuming
libc buys these changes).

2004-11-16  Paul Eggert  <address@hidden>

        Further getopt changes to make it more likely that glibc will
        buy the changes back.
        * getopt.c (POSIXLY_CORRECT): New constant.
        (getopt): Use it, so to preserve glibc semantic
        * getopt1.c (getopt_long, getopt_long_only): Arg is char * const *
        when compiling for libc.
        * getopt_.h (__getopt_argv_const): Bring it back.
        (getopt_long, getopt_long_only): Use it.

Index: getopt.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getopt.c,v
retrieving revision 1.45
diff -p -u -r1.45 getopt.c
--- getopt.c    17 Nov 2004 01:13:54 -0000      1.45
+++ getopt.c    17 Nov 2004 01:47:37 -0000
@@ -1154,10 +1154,19 @@ _getopt_internal (int argc, char **argv,
   return result;
 }
 
+/* glibc gets a LSB-compliant getopt.
+   Standalone applications get a POSIX-compliant getopt.  */
+#if _LIBC
+enum { POSIXLY_CORRECT = 0 };
+#else
+enum { POSIXLY_CORRECT = 1 };
+#endif
+
 int
 getopt (int argc, char *const *argv, const char *optstring)
 {
-  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0, 1);
+  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
+                          POSIXLY_CORRECT);
 }
 
 
Index: getopt1.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getopt1.c,v
retrieving revision 1.19
diff -p -u -r1.19 getopt1.c
--- getopt1.c   17 Nov 2004 01:13:54 -0000      1.19
+++ getopt1.c   17 Nov 2004 01:47:37 -0000
@@ -41,10 +41,11 @@
 #endif
 
 int
-getopt_long (int argc, char **argv, const char *options,
+getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
             const struct option *long_options, int *opt_index)
 {
-  return _getopt_internal (argc, argv, options, long_options, opt_index, 0, 0);
+  return _getopt_internal (argc, (char **) argv, options, long_options,
+                          opt_index, 0, 0);
 }
 
 int
@@ -62,10 +63,12 @@ _getopt_long_r (int argc, char **argv, c
    instead.  */
 
 int
-getopt_long_only (int argc, char **argv, const char *options,
+getopt_long_only (int argc, char *__getopt_argv_const *argv,
+                 const char *options,
                  const struct option *long_options, int *opt_index)
 {
-  return _getopt_internal (argc, argv, options, long_options, opt_index, 1, 0);
+  return _getopt_internal (argc, (char **) argv, options, long_options,
+                          opt_index, 1, 0);
 }
 
 int
Index: getopt_.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/getopt_.h,v
retrieving revision 1.4
diff -p -u -r1.4 getopt_.h
--- getopt_.h   17 Nov 2004 01:13:54 -0000      1.4
+++ getopt_.h   17 Nov 2004 01:47:37 -0000
@@ -53,6 +53,17 @@
 # define optopt __GETOPT_PREFIX##optopt
 #endif
 
+/* Standalone applications get correct prototypes for getopt_long and
+   getopt_long_only; they declare "char **argv".  libc uses prototypes
+   with "char *const *argv" that are incorrect because getopt_long and
+   getopt_long_only can permute argv; this is required for backward
+   compatibility (e.g., for LSB 2.0.1).  */
+#if defined __GETOPT_PREFIX && !defined __need_getopt
+# define __getopt_argv_const /* empty */
+#else
+# define __getopt_argv_const const
+#endif
+
 /* If __GNU_LIBRARY__ is not already defined, either we are being used
    standalone, or this is the first header included in the source file.
    If we are being used with glibc, we need to include <features.h>, but
@@ -178,11 +189,11 @@ extern int getopt (int ___argc, char *co
        __THROW;
 
 #ifndef __need_getopt
-extern int getopt_long (int ___argc, char **___argv,
+extern int getopt_long (int ___argc, char *__getopt_argv_const *___argv,
                        const char *__shortopts,
                        const struct option *__longopts, int *__longind)
        __THROW;
-extern int getopt_long_only (int ___argc, char **___argv,
+extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
                             const char *__shortopts,
                             const struct option *__longopts, int *__longind)
        __THROW;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]