From 33ab177a56b6fe751e70123cf1aa80448d9ca253 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 7 Aug 2003 21:11:58 +0000 Subject: [PATCH] Don't assume that struct option is available just because we can find a getopt_long(). This is more or less the same problem as we saw earlier with getaddrinfo() and struct addrinfo, and for the same reason: random user-added libraries might contain the subroutine, but there's no guarantee we will find the matching header files. --- configure | 149 +++++++++++++++++++++++++- configure.in | 16 ++- contrib/pg_autovacuum/pg_autovacuum.h | 2 +- contrib/pg_dumplo/main.c | 8 +- src/bin/pg_dump/pg_dump.c | 8 +- src/bin/pg_dump/pg_dumpall.c | 8 +- src/bin/pg_dump/pg_restore.c | 9 +- src/bin/psql/startup.c | 9 +- src/bin/scripts/common.h | 9 +- src/include/getopt_long.h | 23 +++- src/include/pg_config.h.in | 3 + 11 files changed, 200 insertions(+), 44 deletions(-) diff --git a/configure b/configure index 659c17f60d..ea4876c964 100755 --- a/configure +++ b/configure @@ -10379,6 +10379,67 @@ _ACEOF fi +echo "$as_me:$LINENO: checking for struct option" >&5 +echo $ECHO_N "checking for struct option... $ECHO_C" >&6 +if test "${ac_cv_type_struct_option+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +#line $LINENO "configure" +#include "confdefs.h" +#ifdef HAVE_GETOPT_H +include +#endif + +#ifdef F77_DUMMY_MAIN +# ifdef __cplusplus + extern "C" +# endif + int F77_DUMMY_MAIN() { return 1; } +#endif +int +main () +{ +if ((struct option *) 0) + return 0; +if (sizeof (struct option)) + return 0; + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext +if { (eval echo "$as_me:$LINENO: \"$ac_compile\"") >&5 + (eval $ac_compile) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest.$ac_objext' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_cv_type_struct_option=yes +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +ac_cv_type_struct_option=no +fi +rm -f conftest.$ac_objext conftest.$ac_ext +fi +echo "$as_me:$LINENO: result: $ac_cv_type_struct_option" >&5 +echo "${ECHO_T}$ac_cv_type_struct_option" >&6 +if test $ac_cv_type_struct_option = yes; then + +cat >>confdefs.h <<_ACEOF +#define HAVE_STRUCT_OPTION 1 +_ACEOF + + +fi + + if test "$with_zlib" = yes; then # Check that defines z_streamp (versions before about 1.0.4 # did not). While we could work around the lack of z_streamp, it @@ -11760,8 +11821,7 @@ fi - -for ac_func in crypt fseeko getopt getopt_long getrusage inet_aton random rint srandom strcasecmp strdup strerror strtol strtoul +for ac_func in crypt fseeko getopt getrusage inet_aton random rint srandom strcasecmp strdup strerror strtol strtoul do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` echo "$as_me:$LINENO: checking for $ac_func" >&5 @@ -11926,6 +11986,91 @@ else LIBOBJS="$LIBOBJS getaddrinfo.$ac_objext" fi +# similarly, use system's getopt_long() only if system provides struct option. +if test x"$ac_cv_type_struct_option" = xyes ; then + +for ac_func in getopt_long +do +as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` +echo "$as_me:$LINENO: checking for $ac_func" >&5 +echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6 +if eval "test \"\${$as_ac_var+set}\" = set"; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + cat >conftest.$ac_ext <<_ACEOF +#line $LINENO "configure" +#include "confdefs.h" +/* System header to define __stub macros and hopefully few prototypes, + which can conflict with char $ac_func (); below. */ +#include +/* Override any gcc2 internal prototype to avoid an error. */ +#ifdef __cplusplus +extern "C" +#endif +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char $ac_func (); +char (*f) (); + +#ifdef F77_DUMMY_MAIN +# ifdef __cplusplus + extern "C" +# endif + int F77_DUMMY_MAIN() { return 1; } +#endif +int +main () +{ +/* The GNU C library defines this for functions which it implements + to always fail with ENOSYS. Some functions are actually named + something starting with __ and the normal name is an alias. */ +#if defined (__stub_$ac_func) || defined (__stub___$ac_func) +choke me +#else +f = $ac_func; +#endif + + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5 + (eval $ac_link) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -s conftest$ac_exeext' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + eval "$as_ac_var=yes" +else + echo "$as_me: failed program was:" >&5 +cat conftest.$ac_ext >&5 +eval "$as_ac_var=no" +fi +rm -f conftest.$ac_objext conftest$ac_exeext conftest.$ac_ext +fi +echo "$as_me:$LINENO: result: `eval echo '${'$as_ac_var'}'`" >&5 +echo "${ECHO_T}`eval echo '${'$as_ac_var'}'`" >&6 +if test `eval echo '${'$as_ac_var'}'` = yes; then + cat >>confdefs.h <<_ACEOF +#define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 +_ACEOF + +else + LIBOBJS="$LIBOBJS $ac_func.$ac_objext" +fi +done + + +else + LIBOBJS="$LIBOBJS getopt_long.$ac_objext" +fi + # BSD/OS & NetBSD use a custom fseeko/ftello built on fsetpos/fgetpos # We override the previous test that said fseeko/ftello didn't exist # OS tests are also done in include/c.h and port/fseeko.c diff --git a/configure.in b/configure.in index 0273e4246c..1905613884 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ dnl Process this file with autoconf to produce a configure script. -dnl $Header: /cvsroot/pgsql/configure.in,v 1.277 2003/08/04 22:30:30 pgsql Exp $ +dnl $Header: /cvsroot/pgsql/configure.in,v 1.278 2003/08/07 21:11:57 tgl Exp $ dnl dnl Developers, please strive to achieve this order: dnl @@ -780,6 +780,11 @@ AC_CHECK_TYPES([struct cmsgcred, struct fcred, struct sockcred], [], [], #include #include ]) +AC_CHECK_TYPES([struct option], [], [], +[#ifdef HAVE_GETOPT_H +include +#endif]) + if test "$with_zlib" = yes; then # Check that defines z_streamp (versions before about 1.0.4 # did not). While we could work around the lack of z_streamp, it @@ -875,7 +880,7 @@ else AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break]) fi -AC_REPLACE_FUNCS([crypt fseeko getopt getopt_long getrusage inet_aton random rint srandom strcasecmp strdup strerror strtol strtoul]) +AC_REPLACE_FUNCS([crypt fseeko getopt getrusage inet_aton random rint srandom strcasecmp strdup strerror strtol strtoul]) # system's version of getaddrinfo(), if any, may be used only if we found # a definition for struct addrinfo; see notes in src/include/getaddrinfo.h @@ -885,6 +890,13 @@ else AC_LIBOBJ(getaddrinfo) fi +# similarly, use system's getopt_long() only if system provides struct option. +if test x"$ac_cv_type_struct_option" = xyes ; then + AC_REPLACE_FUNCS([getopt_long]) +else + AC_LIBOBJ(getopt_long) +fi + # BSD/OS & NetBSD use a custom fseeko/ftello built on fsetpos/fgetpos # We override the previous test that said fseeko/ftello didn't exist # OS tests are also done in include/c.h and port/fseeko.c diff --git a/contrib/pg_autovacuum/pg_autovacuum.h b/contrib/pg_autovacuum/pg_autovacuum.h index fbdaf69912..0fe49159dd 100644 --- a/contrib/pg_autovacuum/pg_autovacuum.h +++ b/contrib/pg_autovacuum/pg_autovacuum.h @@ -6,7 +6,7 @@ #include "postgres_fe.h" #include -#ifdef __GLIBC__ +#ifdef HAVE_GETOPT_H #include #endif #include diff --git a/contrib/pg_dumplo/main.c b/contrib/pg_dumplo/main.c index 09a71a586d..1e892f0105 100644 --- a/contrib/pg_dumplo/main.c +++ b/contrib/pg_dumplo/main.c @@ -1,7 +1,7 @@ /* ------------------------------------------------------------------------- * pg_dumplo * - * $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/main.c,v 1.17 2003/08/04 00:43:11 momjian Exp $ + * $Header: /cvsroot/pgsql/contrib/pg_dumplo/Attic/main.c,v 1.18 2003/08/07 21:11:57 tgl Exp $ * * Karel Zak 1999-2000 * ------------------------------------------------------------------------- @@ -11,9 +11,6 @@ #include #include -#ifdef HAVE_GETOPT_H -#include -#endif #include "libpq-fe.h" #include "libpq/libpq-fs.h" @@ -24,8 +21,9 @@ #include "strdup.h" #endif -#ifndef HAVE_GETOPT_LONG #include "getopt_long.h" + +#ifndef HAVE_OPTRESET int optreset; #endif diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index fb21d04cd6..a6ecda10d6 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -12,7 +12,7 @@ * by PostgreSQL * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.342 2003/08/04 02:40:09 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.343 2003/08/07 21:11:58 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -29,9 +29,6 @@ #ifdef ENABLE_NLS #include #endif -#ifdef HAVE_GETOPT_H -#include -#endif #ifdef HAVE_TERMIOS_H #include #endif @@ -40,8 +37,9 @@ #include "strdup.h" #endif -#ifndef HAVE_GETOPT_LONG #include "getopt_long.h" + +#ifndef HAVE_OPTRESET int optreset; #endif diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c index 9be39ecb75..8c83254c81 100644 --- a/src/bin/pg_dump/pg_dumpall.c +++ b/src/bin/pg_dump/pg_dumpall.c @@ -6,7 +6,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.26 2003/08/04 02:40:09 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.27 2003/08/07 21:11:58 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -17,16 +17,14 @@ #ifdef ENABLE_NLS #include #endif -#ifdef HAVE_GETOPT_H -#include -#endif #ifndef HAVE_STRDUP #include "strdup.h" #endif #include -#ifndef HAVE_GETOPT_LONG #include "getopt_long.h" + +#ifndef HAVE_OPTRESET int optreset; #endif diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index 0598c9c451..f9217f2c09 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -34,7 +34,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.49 2003/08/04 00:43:28 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.50 2003/08/07 21:11:58 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -55,12 +55,9 @@ #include -#ifdef HAVE_GETOPT_H -#include -#endif - -#ifndef HAVE_GETOPT_LONG #include "getopt_long.h" + +#ifndef HAVE_OPTRESET int optreset; #endif diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 7dbe42d5a8..afee6d62a5 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -3,7 +3,7 @@ * * Copyright (c) 2000-2003, PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.78 2003/08/04 23:59:40 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.79 2003/08/07 21:11:58 tgl Exp $ */ #include "postgres_fe.h" @@ -17,12 +17,9 @@ #include #endif /* WIN32 */ -#ifdef HAVE_GETOPT_H -#include -#endif - -#ifndef HAVE_GETOPT_LONG #include "getopt_long.h" + +#ifndef HAVE_OPTRESET int optreset; #endif diff --git a/src/bin/scripts/common.h b/src/bin/scripts/common.h index 8a6431e724..f8cba29eb3 100644 --- a/src/bin/scripts/common.h +++ b/src/bin/scripts/common.h @@ -1,15 +1,8 @@ #include "postgres_fe.h" -#ifdef HAVE_GETOPT_H -#include -#endif - -#ifndef HAVE_GETOPT_LONG -#include "getopt_long.h" -#endif - #include "libpq-fe.h" #include "pqexpbuffer.h" +#include "getopt_long.h" #ifndef HAVE_OPTRESET int optreset; diff --git a/src/include/getopt_long.h b/src/include/getopt_long.h index 3651cdeee9..9b155b25da 100644 --- a/src/include/getopt_long.h +++ b/src/include/getopt_long.h @@ -2,11 +2,16 @@ * Portions Copyright (c) 1987, 1993, 1994 * The Regents of the University of California. All rights reserved. * - * Portions Copyright (c) 2003 - * PostgreSQL Global Development Group + * Portions Copyright (c) 2003, PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/include/getopt_long.h,v 1.2 2003/08/04 00:43:29 momjian Exp $ + * $Header: /cvsroot/pgsql/src/include/getopt_long.h,v 1.3 2003/08/07 21:11:58 tgl Exp $ */ +#ifndef GETOPT_LONG_H +#define GETOPT_LONG_H + +#ifdef HAVE_GETOPT_H +#include +#endif /* These are picked up from the system's getopt() facility. */ extern int opterr; @@ -17,6 +22,8 @@ extern char *optarg; /* Some systems have this, otherwise you need to define it somewhere. */ extern int optreset; +#ifndef HAVE_STRUCT_OPTION + struct option { const char *name; @@ -28,6 +35,14 @@ struct option #define no_argument 0 #define required_argument 1 -int getopt_long(int argc, char *const argv[], +#endif + +#ifndef HAVE_GETOPT_LONG + +extern int getopt_long(int argc, char *const argv[], const char *optstring, const struct option * longopts, int *longindex); + +#endif + +#endif /* GETOPT_LONG_H */ diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in index e89cdb8730..379f0f4f99 100644 --- a/src/include/pg_config.h.in +++ b/src/include/pg_config.h.in @@ -417,6 +417,9 @@ /* Define to 1 if the system has the type `struct fcred'. */ #undef HAVE_STRUCT_FCRED +/* Define to 1 if the system has the type `struct option'. */ +#undef HAVE_STRUCT_OPTION + /* Define to 1 if `sa_len' is member of `struct sockaddr'. */ #undef HAVE_STRUCT_SOCKADDR_SA_LEN -- 2.40.0