From 1d26226d9583ad7bee290ca9bba1aa3794e98a53 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 4 Apr 2009 21:55:50 +0000
Subject: [PATCH] Make an attempt at fixing our current Solaris 11 breakage:
 add a configure probe for opterr (exactly like the one for optreset) and have
 getopt.c define the variables only if configure doesn't find them in libc.

---
 configure                  | 59 ++++++++++++++++++++++++++++++++++++++
 configure.in               | 11 ++++++-
 src/include/pg_config.h.in |  3 ++
 src/port/getopt.c          | 19 ++++++++++--
 4 files changed, 88 insertions(+), 4 deletions(-)

diff --git a/configure b/configure
index f20c8a2d30..fc17b1a5fe 100755
--- a/configure
+++ b/configure
@@ -18655,6 +18655,65 @@ fi
 fi
 
 
+{ echo "$as_me:$LINENO: checking for opterr" >&5
+echo $ECHO_N "checking for opterr... $ECHO_C" >&6; }
+if test "${pgac_cv_var_int_opterr+set}" = set; then
+  echo $ECHO_N "(cached) $ECHO_C" >&6
+else
+  cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#include <unistd.h>
+int
+main ()
+{
+extern int opterr; opterr = 1;
+  ;
+  return 0;
+}
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_link") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+	 test -z "$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       } && test -s conftest$ac_exeext &&
+       $as_test_x conftest$ac_exeext; then
+  pgac_cv_var_int_opterr=yes
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+	pgac_cv_var_int_opterr=no
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
+      conftest$ac_exeext conftest.$ac_ext
+fi
+{ echo "$as_me:$LINENO: result: $pgac_cv_var_int_opterr" >&5
+echo "${ECHO_T}$pgac_cv_var_int_opterr" >&6; }
+if test x"$pgac_cv_var_int_opterr" = x"yes"; then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_INT_OPTERR 1
+_ACEOF
+
+fi
+
 { echo "$as_me:$LINENO: checking for optreset" >&5
 echo $ECHO_N "checking for optreset... $ECHO_C" >&6; }
 if test "${pgac_cv_var_int_optreset+set}" = set; then
diff --git a/configure.in b/configure.in
index 33560af351..5a742d8323 100644
--- a/configure.in
+++ b/configure.in
@@ -1,5 +1,5 @@
 dnl Process this file with autoconf to produce a configure script.
-dnl $PostgreSQL: pgsql/configure.in,v 1.592 2009/03/27 19:58:11 tgl Exp $
+dnl $PostgreSQL: pgsql/configure.in,v 1.593 2009/04/04 21:55:50 tgl Exp $
 dnl
 dnl Developers, please strive to achieve this order:
 dnl
@@ -1317,6 +1317,15 @@ AC_CHECK_FUNC(syslog,
               [AC_CHECK_HEADER(syslog.h,
                                [AC_DEFINE(HAVE_SYSLOG, 1, [Define to 1 if you have the syslog interface.])])])
 
+AC_CACHE_CHECK([for opterr], pgac_cv_var_int_opterr,
+[AC_TRY_LINK([#include <unistd.h>],
+  [extern int opterr; opterr = 1;],
+  [pgac_cv_var_int_opterr=yes],
+  [pgac_cv_var_int_opterr=no])])
+if test x"$pgac_cv_var_int_opterr" = x"yes"; then
+  AC_DEFINE(HAVE_INT_OPTERR, 1, [Define to 1 if you have the global variable 'int opterr'.])
+fi
+
 AC_CACHE_CHECK([for optreset], pgac_cv_var_int_optreset,
 [AC_TRY_LINK([#include <unistd.h>],
   [extern int optreset; optreset = 1;],
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index 3cc7c7ce6a..3473227827 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -221,6 +221,9 @@
 /* Define to 1 if you have the <inttypes.h> header file. */
 #undef HAVE_INTTYPES_H
 
+/* Define to 1 if you have the global variable 'int opterr'. */
+#undef HAVE_INT_OPTERR
+
 /* Define to 1 if you have the global variable 'int optreset'. */
 #undef HAVE_INT_OPTRESET
 
diff --git a/src/port/getopt.c b/src/port/getopt.c
index 796a009dfa..2cc2519116 100644
--- a/src/port/getopt.c
+++ b/src/port/getopt.c
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/src/port/getopt.c,v 1.11 2007/03/26 21:44:11 momjian Exp $ */
+/* $PostgreSQL: pgsql/src/port/getopt.c,v 1.12 2009/04/04 21:55:50 tgl Exp $ */
 
 /* This is used by psql under Win32 */
 
@@ -37,12 +37,25 @@ static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
 #endif   /* LIBC_SCCS and not lint */
 
 
+/*
+ * On some versions of Solaris, opterr and friends are defined in core libc
+ * rather than in a separate getopt module.  Define these variables only
+ * if configure found they aren't there by default.  (We assume that testing
+ * opterr is sufficient for all of these except optreset.)
+ */
+#ifndef HAVE_INT_OPTERR
+
 int			opterr = 1,			/* if error message should be printed */
 			optind = 1,			/* index into parent argv vector */
-			optopt,				/* character checked for validity */
-			optreset;			/* reset getopt */
+			optopt;				/* character checked for validity */
 char	   *optarg;				/* argument associated with option */
 
+#endif
+
+#ifndef HAVE_INT_OPTRESET
+int			optreset;			/* reset getopt */
+#endif
+
 #define BADCH	(int)'?'
 #define BADARG	(int)':'
 #define EMSG	""