changeset 5428:e6b5cc694a7c

(__getopt_argv_const): New macro, to be used so that we can stop lying to compilers about the constness of argv when we are compiled outside glibc. (getopt, getopt_long, getopt_long_only): Use it. [defined __GETOPT_PREFIX && !defined __need_getopt]: Include <stdlib.h> and <stdio.h>, too.
author Paul Eggert <eggert@cs.ucla.edu>
date Thu, 11 Nov 2004 21:14:57 +0000
parents e314bbc551bf
children 1b54fe747ab9
files lib/getopt_.h
diffstat 1 files changed, 21 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/lib/getopt_.h
+++ b/lib/getopt_.h
@@ -25,13 +25,15 @@
 
 /* Standalone applications should #define __GETOPT_PREFIX to an
    identifier that prefixes the external functions and variables
-   defined in this header.  When this happens, include <unistd.h> (the
-   standard header declaring getopt), so that it will not cause
-   confusion if it is included later.  Then systematically rename
+   defined in this header.  When this happens, include the
+   headers that might declare getopt so that they will not cause
+   confusion if included after this file.  Then systematically rename
    identifiers so that they do not collide with the system functions
    and variables.  Renaming avoids problems with some compilers and
    linkers.  */
 #if defined __GETOPT_PREFIX && !defined __need_getopt
+# include <stdlib.h>
+# include <stdio.h>
 # if HAVE_UNISTD_H
 #  include <unistd.h>
 # endif
@@ -51,6 +53,18 @@
 # define optopt __GETOPT_PREFIX##optopt
 #endif
 
+/* The elements of the ARGV arguments to getopt aren't really const,
+   because we permute them.  For glibc, __getopt_argv_const is const
+   so that prototypes pretend the elements are const, to be compatible
+   with Posix.  However, drop this pretense for standalone
+   applications, since it's not needed there and it's safer not to lie
+   to compilers.  */
+#ifdef __GETOPT_PREFIX
+# 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
@@ -172,15 +186,16 @@
    arguments to the option '\0'.  This behavior is specific to the GNU
    `getopt'.  */
 
-extern int getopt (int ___argc, char *const *___argv, const char *__shortopts)
+extern int getopt (int ___argc, char *__getopt_argv_const *___argv,
+		   const char *__shortopts)
        __THROW;
 
 #ifndef __need_getopt
-extern int getopt_long (int ___argc, char *const *___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 *const *___argv,
+extern int getopt_long_only (int ___argc, char *__getopt_argv_const *___argv,
 			     const char *__shortopts,
 		             const struct option *__longopts, int *__longind)
        __THROW;