Skip to content

Commit 7745116

Browse files
committed
Remove optreset from src/port/ implementations of getopt and getopt_long.
We don't actually need optreset, because we can easily fix the code to ensure that it's cleanly restartable after having completed a scan over the argv array; which is the only case we need to restart in. Getting rid of it avoids a class of interactions with the system libraries and allows reversion of my change of yesterday in postmaster.c and postgres.c. Back-patch to 8.4. Before that the getopt code was a bit different anyway.
1 parent 770bddc commit 7745116

File tree

5 files changed

+26
-25
lines changed

5 files changed

+26
-25
lines changed

src/backend/postmaster/postmaster.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ extern char *optarg;
312312
extern int optind,
313313
opterr;
314314

315-
/* If not HAVE_GETOPT, we are using src/port/getopt.c, which has optreset */
316-
#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
315+
#ifdef HAVE_INT_OPTRESET
317316
extern int optreset; /* might not be declared by system headers */
318317
#endif
319318

@@ -751,7 +750,7 @@ PostmasterMain(int argc, char *argv[])
751750
* getopt(3) library so that it will work correctly in subprocesses.
752751
*/
753752
optind = 1;
754-
#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
753+
#ifdef HAVE_INT_OPTRESET
755754
optreset = 1; /* some systems need this too */
756755
#endif
757756

src/backend/tcop/postgres.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@
7777
extern char *optarg;
7878
extern int optind;
7979

80-
/* If not HAVE_GETOPT, we are using src/port/getopt.c, which has optreset */
81-
#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
80+
#ifdef HAVE_INT_OPTRESET
8281
extern int optreset; /* might not be declared by system headers */
8382
#endif
8483

@@ -3440,7 +3439,7 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
34403439
* or when this function is called a second time with another array.
34413440
*/
34423441
optind = 1;
3443-
#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
3442+
#ifdef HAVE_INT_OPTRESET
34443443
optreset = 1; /* some systems need this too */
34453444
#endif
34463445

src/include/getopt_long.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extern int opterr;
1818
extern int optind;
1919
extern int optopt;
2020
extern char *optarg;
21-
extern int optreset;
2221

2322
#ifndef HAVE_STRUCT_OPTION
2423

src/port/getopt.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
4141
* On some versions of Solaris, opterr and friends are defined in core libc
4242
* rather than in a separate getopt module. Define these variables only
4343
* if configure found they aren't there by default. (We assume that testing
44-
* opterr is sufficient for all of these except optreset.)
44+
* opterr is sufficient for all of these.)
4545
*/
4646
#ifndef HAVE_INT_OPTERR
4747

@@ -57,19 +57,19 @@ extern int optopt;
5757
extern char *optarg;
5858
#endif
5959

60-
#ifndef HAVE_INT_OPTRESET
61-
int optreset; /* reset getopt */
62-
#else
63-
extern int optreset;
64-
#endif
65-
6660
#define BADCH (int)'?'
6761
#define BADARG (int)':'
6862
#define EMSG ""
6963

7064
/*
7165
* getopt
7266
* Parse argc/argv argument vector.
67+
*
68+
* This implementation does not use optreset. Instead, we guarantee that
69+
* it can be restarted on a new argv array after a previous call returned -1,
70+
* if the caller resets optind to 1 before the first call of the new series.
71+
* (Internally, this means we must be sure to reset "place" to EMSG before
72+
* returning -1.)
7373
*/
7474
int
7575
getopt(nargc, nargv, ostr)
@@ -80,9 +80,8 @@ const char *ostr;
8080
static char *place = EMSG; /* option letter processing */
8181
char *oli; /* option letter list index */
8282

83-
if (optreset || !*place)
83+
if (!*place)
8484
{ /* update scanning pointer */
85-
optreset = 0;
8685
if (optind >= nargc || *(place = nargv[optind]) != '-')
8786
{
8887
place = EMSG;
@@ -102,7 +101,10 @@ const char *ostr;
102101
* if the user didn't specify '-' as an option, assume it means -1.
103102
*/
104103
if (optopt == (int) '-')
104+
{
105+
place = EMSG;
105106
return -1;
107+
}
106108
if (!*place)
107109
++optind;
108110
if (opterr && *ostr != ':')

src/port/getopt_long.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@
3838

3939
#include "getopt_long.h"
4040

41-
#ifndef HAVE_INT_OPTRESET
42-
int optreset;
43-
44-
/* else the "extern" was provided by getopt_long.h */
45-
#endif
46-
4741
#define BADCH '?'
4842
#define BADARG ':'
4943
#define EMSG ""
5044

5145

46+
/*
47+
* getopt_long
48+
* Parse argc/argv argument vector, with long options.
49+
*
50+
* This implementation does not use optreset. Instead, we guarantee that
51+
* it can be restarted on a new argv array after a previous call returned -1,
52+
* if the caller resets optind to 1 before the first call of the new series.
53+
* (Internally, this means we must be sure to reset "place" to EMSG before
54+
* returning -1.)
55+
*/
5256
int
5357
getopt_long(int argc, char *const argv[],
5458
const char *optstring,
@@ -57,10 +61,8 @@ getopt_long(int argc, char *const argv[],
5761
static char *place = EMSG; /* option letter processing */
5862
char *oli; /* option letter list index */
5963

60-
if (optreset || !*place)
64+
if (!*place)
6165
{ /* update scanning pointer */
62-
optreset = 0;
63-
6466
if (optind >= argc)
6567
{
6668
place = EMSG;

0 commit comments

Comments
 (0)