Skip to content

Commit 61b5369

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 cd1fefa commit 61b5369

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
@@ -313,8 +313,7 @@ extern char *optarg;
313313
extern int optind,
314314
opterr;
315315

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

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

src/backend/tcop/postgres.c

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

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

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

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy