Centralize getopt-related declarations in a new header file pg_getopt.h.
authorTom Lane
Sat, 15 Feb 2014 19:31:30 +0000 (14:31 -0500)
committerTom Lane
Sat, 15 Feb 2014 19:31:30 +0000 (14:31 -0500)
We used to have externs for getopt() and its API variables scattered
all over the place.  Now that we find we're going to need to tweak the
variable declarations for Cygwin, it seems like a good idea to have
just one place to tweak.

In this commit, the variables are declared "#ifndef HAVE_GETOPT_H".
That may or may not work everywhere, but we'll soon find out.

Andres Freund

19 files changed:
contrib/oid2name/oid2name.c
contrib/pg_archivecleanup/pg_archivecleanup.c
contrib/pg_standby/pg_standby.c
contrib/pg_upgrade/option.c
contrib/pgbench/pgbench.c
contrib/vacuumlo/vacuumlo.c
src/backend/bootstrap/bootstrap.c
src/backend/postmaster/postmaster.c
src/backend/tcop/postgres.c
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_restore.c
src/bin/pg_resetxlog/pg_resetxlog.c
src/bin/psql/startup.c
src/include/getopt_long.h
src/include/pg_getopt.h [new file with mode: 0644]
src/include/port.h
src/port/getopt.c
src/test/isolation/isolationtester.c
src/timezone/zic.c

index 67d79346b22e84187dacc7ccb79da5721f14b789..d8565941f90997726efe0dc5e141e6c957be0878 100644 (file)
@@ -9,14 +9,8 @@
  */
 #include "postgres_fe.h"
 
-#include 
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
-
-extern char *optarg;
-
 #include "libpq-fe.h"
+#include "pg_getopt.h"
 
 /* an extensible array to keep track of elements to show */
 typedef struct
index f12331a62ecaef4b8966aa1d24e0847820dece1d..7b5484bb6ac9f2585ba819be92dbdfc3ae6ecb85 100644 (file)
 #include 
 #include 
 #include 
-
-#ifndef WIN32
 #include 
-#include 
-
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
-#else                          /* WIN32 */
-extern int getopt(int argc, char *const argv[], const char *optstring);
-#endif   /* ! WIN32 */
 
-extern char *optarg;
-extern int optind;
+#include "pg_getopt.h"
 
 const char *progname;
 
index 059c820527994ee3409c0d0c10dfd7dbbd516842..144edd8a0728cf97719a86bbe9b666f6a6ea82ac 100644 (file)
 #include 
 #include 
 #include 
-
-#ifdef WIN32
-int            getopt(int argc, char *const argv[], const char *optstring);
-#else
 #include 
-#include 
-
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
-#endif   /* ! WIN32 */
 
-extern char *optarg;
-extern int optind;
+#include "pg_getopt.h"
 
 const char *progname;
 
index 4c08e94ffd02abb7e7597bc05ce11f7e9eb22f37..cd9f66dd117d67490d7e2a2e975e392f98bccddf 100644 (file)
 #include "postgres_fe.h"
 
 #include "miscadmin.h"
+#include "getopt_long.h"
 
 #include "pg_upgrade.h"
 
-#include 
 #include 
 #include 
 #include 
index 16b7ab5ddc7f93d63c5636b3bedae16ee75850a7..a836acfae9896aec6077696919808d83437ffb56 100644 (file)
 #include 
 #include 
 #include 
-
-#ifndef WIN32
 #include 
-#include 
-#endif   /* ! WIN32 */
-
 #ifdef HAVE_SYS_SELECT_H
 #include 
 #endif
@@ -89,9 +84,6 @@ static int    pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start
 static int pthread_join(pthread_t th, void **thread_return);
 #endif
 
-extern char *optarg;
-extern int optind;
-
 
 /********************************************************************
  * some configurable parameters */
index 3981dd6260f41a7ff5d1e3123ae9dc94b3d9b213..c2e5bad438b6184159db5162d73cfd8773a423ea 100644 (file)
 #endif
 
 #include "libpq-fe.h"
+#include "pg_getopt.h"
 
 #define atooid(x)  ((Oid) strtoul((x), NULL, 10))
 
 #define BUFSIZE            1024
 
-extern char *optarg;
-extern int optind,
-           opterr;
-
 enum trivalue
 {
    TRI_DEFAULT,
index ef6311b075b8eb4829872e273f2d013079922cad..c36e71d80660f9fd17d499241f8d5b32cd59d3f8 100644 (file)
@@ -17,9 +17,6 @@
 #include 
 #include 
 #include 
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
 
 #include "access/htup_details.h"
 #include "bootstrap/bootstrap.h"
@@ -29,6 +26,7 @@
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
 #include "nodes/makefuncs.h"
+#include "pg_getopt.h"
 #include "postmaster/bgwriter.h"
 #include "postmaster/startup.h"
 #include "postmaster/walwriter.h"
@@ -46,9 +44,6 @@
 #include "utils/relmapper.h"
 #include "utils/tqual.h"
 
-extern int optind;
-extern char *optarg;
-
 uint32     bootstrap_data_checksum_version = 0;        /* No checksum */
 
 
index 7adc410b43aaefa02777972115b8f27f74de7cca..6bb2a474857ce070a96b8c6e9deb5e36fb2e076e 100644 (file)
 #include 
 #endif
 
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
-
 #ifdef USE_BONJOUR
 #include 
 #endif
 #include "libpq/libpq.h"
 #include "libpq/pqsignal.h"
 #include "miscadmin.h"
+#include "pg_getopt.h"
 #include "pgstat.h"
 #include "postmaster/autovacuum.h"
 #include "postmaster/bgworker_internals.h"
@@ -352,14 +349,6 @@ static volatile bool HaveCrashedWorker = false;
 static unsigned int random_seed = 0;
 static struct timeval random_start_time;
 
-extern char *optarg;
-extern int optind,
-           opterr;
-
-#ifdef HAVE_INT_OPTRESET
-extern int optreset;           /* might not be declared by system headers */
-#endif
-
 #ifdef USE_BONJOUR
 static DNSServiceRef bonjour_sdref = NULL;
 #endif
index b7612b9b5f7c3ac324bc632b5985694c9c40649e..a230d7eda6955e1e57f641afe217e8dc3843faa0 100644 (file)
@@ -32,9 +32,6 @@
 #include 
 #include 
 #endif
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
 
 #ifndef HAVE_GETRUSAGE
 #include "rusagestub.h"
@@ -55,6 +52,7 @@
 #include "pg_trace.h"
 #include "parser/analyze.h"
 #include "parser/parser.h"
+#include "pg_getopt.h"
 #include "postmaster/autovacuum.h"
 #include "postmaster/postmaster.h"
 #include "replication/walsender.h"
 #include "mb/pg_wchar.h"
 
 
-extern char *optarg;
-extern int optind;
-
-#ifdef HAVE_INT_OPTRESET
-extern int optreset;           /* might not be declared by system headers */
-#endif
-
-
 /* ----------------
  *     global variables
  * ----------------
index 458a118dc02d2a8157658d06a81f52cad384943b..2ce8e6dd00f04989026904b8c197bf097bec3d81 100644 (file)
 #include "dumputils.h"
 #include "parallel.h"
 
-extern char *optarg;
-extern int optind,
-           opterr;
-
 
 typedef struct
 {
index bb652538a444a29036817fcef470373381ea4919..df9477b69fa97a02c000d1e50a21435f1a0d0653 100644 (file)
@@ -43,6 +43,7 @@
 #include "pg_backup_utils.h"
 #include "dumputils.h"
 #include "parallel.h"
+#include "getopt_long.h"
 
 #include 
 
 #include 
 #endif
 
-#include 
-
-#include "getopt_long.h"
-
-extern char *optarg;
-extern int optind;
-
 #ifdef ENABLE_NLS
 #include 
 #endif
index 03f2fad3f72ee6931fb4bd4621dcc2e2e6add812..54690af0311300510c399061cd18be0ed38afc28 100644 (file)
@@ -44,9 +44,6 @@
 #include 
 #include 
 #include 
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
 
 #include "access/transam.h"
 #include "access/tuptoaster.h"
@@ -55,9 +52,7 @@
 #include "catalog/catversion.h"
 #include "catalog/pg_control.h"
 #include "common/fe_memutils.h"
-
-extern int optind;
-extern char *optarg;
+#include "pg_getopt.h"
 
 
 static ControlFileData ControlFile;        /* pg_control values */
index 91948014e5a79c17da8307fb2080b50fb047433b..106199273f0b229f8ec6ce7a1265eb3cf3124b45 100644 (file)
@@ -386,8 +386,6 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
    };
 
    int         optindex;
-   extern char *optarg;
-   extern int  optind;
    int         c;
 
    memset(options, 0, sizeof *options);
index 563719b3f7c5e14f5c7960e747b6f3f508af9f9c..e23c21f333cadda7eb6130d8cff66419d3a3fbbd 100644 (file)
@@ -9,15 +9,7 @@
 #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;
-extern int optind;
-extern int optopt;
-extern char *optarg;
+#include "pg_getopt.h"
 
 #ifndef HAVE_STRUCT_OPTION
 
diff --git a/src/include/pg_getopt.h b/src/include/pg_getopt.h
new file mode 100644 (file)
index 0000000..6488004
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Portions Copyright (c) 1987, 1993, 1994
+ * The Regents of the University of California.  All rights reserved.
+ *
+ * Portions Copyright (c) 2003-2014, PostgreSQL Global Development Group
+ *
+ * src/include/pg_getopt.h
+ */
+#ifndef PG_GETOPT_H
+#define PG_GETOPT_H
+
+/* POSIX says getopt() is provided by unistd.h */
+#include 
+
+/* rely on the system's getopt.h if present */
+#ifdef HAVE_GETOPT_H
+#include 
+#endif
+
+/*
+ * If we have , assume it declares these variables, else do that
+ * ourselves.  (We used to just declare them unconditionally, but Cygwin
+ * doesn't like that.)
+ */
+#ifndef HAVE_GETOPT_H
+
+extern char *optarg;
+extern int optind;
+extern int opterr;
+extern int optopt;
+
+#ifdef HAVE_INT_OPTRESET
+extern int optreset;
+#endif
+
+#endif   /* HAVE_GETOPT_H */
+
+#ifndef HAVE_GETOPT
+extern int getopt(int nargc, char *const * nargv, const char *ostr);
+#endif
+
+#endif   /* PG_GETOPT_H */
index f50fbecf0109fcca47a10be182bf5463598d6ba5..aeb7754cb0940b4fb0355ba015f98f3ab40fc681 100644 (file)
@@ -382,10 +382,6 @@ extern int fls(int mask);
 #define ftello(a)      ftell(a)
 #endif
 
-#ifndef HAVE_GETOPT
-extern int getopt(int nargc, char *const * nargv, const char *ostr);
-#endif
-
 #if !defined(HAVE_GETPEEREID) && !defined(WIN32)
 extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
 #endif
index eb50cba1ca440d0de9f9d194dc0c792b0bfb293b..b81fba14b07ddd9ea0c4ca9be0a06454ea5a5bf2 100644 (file)
@@ -32,6 +32,8 @@
 
 #include "c.h"
 
+#include "pg_getopt.h"
+
 #if defined(LIBC_SCCS) && !defined(lint)
 static char sccsid[] = "@(#)getopt.c   8.3 (Berkeley) 4/27/95";
 #endif   /* LIBC_SCCS and not lint */
@@ -49,20 +51,13 @@ int         opterr = 1,         /* if error message should be printed */
            optind = 1,         /* index into parent argv vector */
            optopt;             /* character checked for validity */
 char      *optarg;             /* argument associated with option */
-#else
 
-extern int opterr;
-extern int optind;
-extern int optopt;
-extern char *optarg;
 #endif
 
 #define BADCH  (int)'?'
 #define BADARG (int)':'
 #define EMSG   ""
 
-int            getopt(int nargc, char *const * nargv, const char *ostr);
-
 /*
  * getopt
  * Parse argc/argv argument vector.
index e936c01532f9848c39e37a85c5be43215be91d4f..ad6732aee783ccc3726d9df267d414ebbf171a01 100644 (file)
 #ifdef WIN32
 #include 
 #endif
-
-#ifndef WIN32
 #include 
-#include 
-
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
-#else
-int            getopt(int argc, char *const argv[], const char *optstring);
-#endif   /* ! WIN32 */
-
 #ifdef HAVE_SYS_SELECT_H
 #include 
 #endif
 
 #include "libpq-fe.h"
 #include "pqexpbuffer.h"
+#include "pg_getopt.h"
 
 #include "isolationtester.h"
 
-extern int optind;
-
 #define PREP_WAITING "isolationtester_waiting"
 
 /*
index a399d1f0f194acfcda187091fce7f6fba08aadc5..cda704266add2b2878c9f924464dd484204e46b5 100644 (file)
@@ -8,15 +8,11 @@
 
 #include "postgres_fe.h"
 
-#ifdef HAVE_GETOPT_H
-#include 
-#endif
 #include 
 #include 
 #include 
 
-extern int optind;
-extern char *optarg;
+#include "pg_getopt.h"
 
 #include "private.h"
 #include "pgtz.h"