[defined __GETOPT_PREFIX && !defined __need_getopt]:
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 11 Nov 2004 20:24:58 +0000 (20:24 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 11 Nov 2004 20:24:58 +0000 (20:24 +0000)
Include <unistd.h> if available.
Then rename getopt to __GETOPT_PREFIX##getopt, and so forth for
the other external symbols.
(getopt) [!defined __GNU_LIBRARY]: Use prototype, not old-style
declaration, since the above renaming now works around collisions.

lib/getopt_.h

index c61768c691bfc6e0f010b8f5bdf7ccaec385774d..e6437ed3b18422563b60483f18c3d55c11fa924e 100644 (file)
 # define _GETOPT_H 1
 #endif
 
+/* 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
+   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
+# if HAVE_UNISTD_H
+#  include <unistd.h>
+# endif
+# undef getopt
+# undef getopt_long
+# undef getopt_long_only
+# undef optarg
+# undef opterr
+# undef optind
+# undef optopt
+# define getopt __GETOPT_PREFIX##getopt
+# define getopt_long __GETOPT_PREFIX##getopt_long
+# define getopt_long_only __GETOPT_PREFIX##getopt_long_only
+# define optarg __GETOPT_PREFIX##optarg
+# define opterr __GETOPT_PREFIX##opterr
+# define optind __GETOPT_PREFIX##optind
+# define optopt __GETOPT_PREFIX##optopt
+#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
@@ -144,15 +172,8 @@ struct option
    arguments to the option '\0'.  This behavior is specific to the GNU
    `getopt'.  */
 
-#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__ */
-extern int getopt ();
-#endif /* __GNU_LIBRARY__ */
 
 #ifndef __need_getopt
 extern int getopt_long (int ___argc, char *const *___argv,