Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 60ff2fd

Browse files
committed
Centralize getopt-related declarations in a new header file pg_getopt.h.
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
1 parent 32be1c8 commit 60ff2fd

File tree

19 files changed

+57
-130
lines changed

19 files changed

+57
-130
lines changed

contrib/oid2name/oid2name.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,8 @@
99
*/
1010
#include "postgres_fe.h"
1111

12-
#include <unistd.h>
13-
#ifdef HAVE_GETOPT_H
14-
#include <getopt.h>
15-
#endif
16-
17-
extern char *optarg;
18-
1912
#include "libpq-fe.h"
13+
#include "pg_getopt.h"
2014

2115
/* an extensible array to keep track of elements to show */
2216
typedef struct

contrib/pg_archivecleanup/pg_archivecleanup.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,9 @@
1717
#include <sys/stat.h>
1818
#include <fcntl.h>
1919
#include <signal.h>
20-
21-
#ifndef WIN32
2220
#include <sys/time.h>
23-
#include <unistd.h>
24-
25-
#ifdef HAVE_GETOPT_H
26-
#include <getopt.h>
27-
#endif
28-
#else /* WIN32 */
29-
extern int getopt(int argc, char *const argv[], const char *optstring);
30-
#endif /* ! WIN32 */
3121

32-
extern char *optarg;
33-
extern int optind;
22+
#include "pg_getopt.h"
3423

3524
const char *progname;
3625

contrib/pg_standby/pg_standby.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,9 @@
2828
#include <sys/stat.h>
2929
#include <fcntl.h>
3030
#include <signal.h>
31-
32-
#ifdef WIN32
33-
int getopt(int argc, char *const argv[], const char *optstring);
34-
#else
3531
#include <sys/time.h>
36-
#include <unistd.h>
37-
38-
#ifdef HAVE_GETOPT_H
39-
#include <getopt.h>
40-
#endif
41-
#endif /* ! WIN32 */
4232

43-
extern char *optarg;
44-
extern int optind;
33+
#include "pg_getopt.h"
4534

4635
const char *progname;
4736

contrib/pg_upgrade/option.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include "postgres_fe.h"
1111

1212
#include "miscadmin.h"
13+
#include "getopt_long.h"
1314

1415
#include "pg_upgrade.h"
1516

16-
#include <getopt_long.h>
1717
#include <time.h>
1818
#include <sys/types.h>
1919
#include <sys/stat.h>

contrib/pgbench/pgbench.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@
4040
#include <ctype.h>
4141
#include <math.h>
4242
#include <signal.h>
43-
44-
#ifndef WIN32
4543
#include <sys/time.h>
46-
#include <unistd.h>
47-
#endif /* ! WIN32 */
48-
4944
#ifdef HAVE_SYS_SELECT_H
5045
#include <sys/select.h>
5146
#endif
@@ -89,9 +84,6 @@ static int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start
8984
static int pthread_join(pthread_t th, void **thread_return);
9085
#endif
9186

92-
extern char *optarg;
93-
extern int optind;
94-
9587

9688
/********************************************************************
9789
* some configurable parameters */

contrib/vacuumlo/vacuumlo.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@
2222
#endif
2323

2424
#include "libpq-fe.h"
25+
#include "pg_getopt.h"
2526

2627
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
2728

2829
#define BUFSIZE 1024
2930

30-
extern char *optarg;
31-
extern int optind,
32-
opterr;
33-
3431
enum trivalue
3532
{
3633
TRI_DEFAULT,

src/backend/bootstrap/bootstrap.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
#include <time.h>
1818
#include <unistd.h>
1919
#include <signal.h>
20-
#ifdef HAVE_GETOPT_H
21-
#include <getopt.h>
22-
#endif
2320

2421
#include "access/htup_details.h"
2522
#include "bootstrap/bootstrap.h"
@@ -29,6 +26,7 @@
2926
#include "libpq/pqsignal.h"
3027
#include "miscadmin.h"
3128
#include "nodes/makefuncs.h"
29+
#include "pg_getopt.h"
3230
#include "postmaster/bgwriter.h"
3331
#include "postmaster/startup.h"
3432
#include "postmaster/walwriter.h"
@@ -46,9 +44,6 @@
4644
#include "utils/relmapper.h"
4745
#include "utils/tqual.h"
4846

49-
extern int optind;
50-
extern char *optarg;
51-
5247
uint32 bootstrap_data_checksum_version = 0; /* No checksum */
5348

5449

src/backend/postmaster/postmaster.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@
8383
#include <sys/select.h>
8484
#endif
8585

86-
#ifdef HAVE_GETOPT_H
87-
#include <getopt.h>
88-
#endif
89-
9086
#ifdef USE_BONJOUR
9187
#include <dns_sd.h>
9288
#endif
@@ -101,6 +97,7 @@
10197
#include "libpq/libpq.h"
10298
#include "libpq/pqsignal.h"
10399
#include "miscadmin.h"
100+
#include "pg_getopt.h"
104101
#include "pgstat.h"
105102
#include "postmaster/autovacuum.h"
106103
#include "postmaster/bgworker_internals.h"
@@ -352,14 +349,6 @@ static volatile bool HaveCrashedWorker = false;
352349
static unsigned int random_seed = 0;
353350
static struct timeval random_start_time;
354351

355-
extern char *optarg;
356-
extern int optind,
357-
opterr;
358-
359-
#ifdef HAVE_INT_OPTRESET
360-
extern int optreset; /* might not be declared by system headers */
361-
#endif
362-
363352
#ifdef USE_BONJOUR
364353
static DNSServiceRef bonjour_sdref = NULL;
365354
#endif

src/backend/tcop/postgres.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
#include <sys/time.h>
3333
#include <sys/resource.h>
3434
#endif
35-
#ifdef HAVE_GETOPT_H
36-
#include <getopt.h>
37-
#endif
3835

3936
#ifndef HAVE_GETRUSAGE
4037
#include "rusagestub.h"
@@ -55,6 +52,7 @@
5552
#include "pg_trace.h"
5653
#include "parser/analyze.h"
5754
#include "parser/parser.h"
55+
#include "pg_getopt.h"
5856
#include "postmaster/autovacuum.h"
5957
#include "postmaster/postmaster.h"
6058
#include "replication/walsender.h"
@@ -77,14 +75,6 @@
7775
#include "mb/pg_wchar.h"
7876

7977

80-
extern char *optarg;
81-
extern int optind;
82-
83-
#ifdef HAVE_INT_OPTRESET
84-
extern int optreset; /* might not be declared by system headers */
85-
#endif
86-
87-
8878
/* ----------------
8979
* global variables
9080
* ----------------

src/bin/pg_dump/pg_dump.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@
6363
#include "dumputils.h"
6464
#include "parallel.h"
6565

66-
extern char *optarg;
67-
extern int optind,
68-
opterr;
69-
7066

7167
typedef struct
7268
{

src/bin/pg_dump/pg_restore.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,14 @@
4343
#include "pg_backup_utils.h"
4444
#include "dumputils.h"
4545
#include "parallel.h"
46+
#include "getopt_long.h"
4647

4748
#include <ctype.h>
4849

4950
#ifdef HAVE_TERMIOS_H
5051
#include <termios.h>
5152
#endif
5253

53-
#include <unistd.h>
54-
55-
#include "getopt_long.h"
56-
57-
extern char *optarg;
58-
extern int optind;
59-
6054
#ifdef ENABLE_NLS
6155
#include <locale.h>
6256
#endif

src/bin/pg_resetxlog/pg_resetxlog.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@
4444
#include <sys/time.h>
4545
#include <time.h>
4646
#include <unistd.h>
47-
#ifdef HAVE_GETOPT_H
48-
#include <getopt.h>
49-
#endif
5047

5148
#include "access/transam.h"
5249
#include "access/tuptoaster.h"
@@ -55,9 +52,7 @@
5552
#include "catalog/catversion.h"
5653
#include "catalog/pg_control.h"
5754
#include "common/fe_memutils.h"
58-
59-
extern int optind;
60-
extern char *optarg;
55+
#include "pg_getopt.h"
6156

6257

6358
static ControlFileData ControlFile; /* pg_control values */

src/bin/psql/startup.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,6 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
386386
};
387387

388388
int optindex;
389-
extern char *optarg;
390-
extern int optind;
391389
int c;
392390

393391
memset(options, 0, sizeof *options);

src/include/getopt_long.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@
99
#ifndef GETOPT_LONG_H
1010
#define GETOPT_LONG_H
1111

12-
#ifdef HAVE_GETOPT_H
13-
#include <getopt.h>
14-
#endif
15-
16-
/* These are picked up from the system's getopt() facility. */
17-
extern int opterr;
18-
extern int optind;
19-
extern int optopt;
20-
extern char *optarg;
12+
#include "pg_getopt.h"
2113

2214
#ifndef HAVE_STRUCT_OPTION
2315

src/include/pg_getopt.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Portions Copyright (c) 1987, 1993, 1994
3+
* The Regents of the University of California. All rights reserved.
4+
*
5+
* Portions Copyright (c) 2003-2014, PostgreSQL Global Development Group
6+
*
7+
* src/include/pg_getopt.h
8+
*/
9+
#ifndef PG_GETOPT_H
10+
#define PG_GETOPT_H
11+
12+
/* POSIX says getopt() is provided by unistd.h */
13+
#include <unistd.h>
14+
15+
/* rely on the system's getopt.h if present */
16+
#ifdef HAVE_GETOPT_H
17+
#include <getopt.h>
18+
#endif
19+
20+
/*
21+
* If we have <getopt.h>, assume it declares these variables, else do that
22+
* ourselves. (We used to just declare them unconditionally, but Cygwin
23+
* doesn't like that.)
24+
*/
25+
#ifndef HAVE_GETOPT_H
26+
27+
extern char *optarg;
28+
extern int optind;
29+
extern int opterr;
30+
extern int optopt;
31+
32+
#ifdef HAVE_INT_OPTRESET
33+
extern int optreset;
34+
#endif
35+
36+
#endif /* HAVE_GETOPT_H */
37+
38+
#ifndef HAVE_GETOPT
39+
extern int getopt(int nargc, char *const * nargv, const char *ostr);
40+
#endif
41+
42+
#endif /* PG_GETOPT_H */

src/include/port.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,6 @@ extern int fls(int mask);
382382
#define ftello(a) ftell(a)
383383
#endif
384384

385-
#ifndef HAVE_GETOPT
386-
extern int getopt(int nargc, char *const * nargv, const char *ostr);
387-
#endif
388-
389385
#if !defined(HAVE_GETPEEREID) && !defined(WIN32)
390386
extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
391387
#endif

src/port/getopt.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include "c.h"
3434

35+
#include "pg_getopt.h"
36+
3537
#if defined(LIBC_SCCS) && !defined(lint)
3638
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
3739
#endif /* LIBC_SCCS and not lint */
@@ -49,20 +51,13 @@ int opterr = 1, /* if error message should be printed */
4951
optind = 1, /* index into parent argv vector */
5052
optopt; /* character checked for validity */
5153
char *optarg; /* argument associated with option */
52-
#else
5354

54-
extern int opterr;
55-
extern int optind;
56-
extern int optopt;
57-
extern char *optarg;
5855
#endif
5956

6057
#define BADCH (int)'?'
6158
#define BADARG (int)':'
6259
#define EMSG ""
6360

64-
int getopt(int nargc, char *const * nargv, const char *ostr);
65-
6661
/*
6762
* getopt
6863
* Parse argc/argv argument vector.

0 commit comments

Comments
 (0)