Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Remove optreset from src/port/ implementations of getopt and getopt_l…

…ong.

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.
  • Loading branch information...
commit 61b53695fbbedb7fa9d394e71bf2affdc494e6b0 1 parent cd1fefa
authored December 16, 2010
5  src/backend/postmaster/postmaster.c
@@ -313,8 +313,7 @@ extern char *optarg;
313 313
 extern int	optind,
314 314
 			opterr;
315 315
 
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
318 317
 extern int	optreset;			/* might not be declared by system headers */
319 318
 #endif
320 319
 
@@ -752,7 +751,7 @@ PostmasterMain(int argc, char *argv[])
752 751
 	 * getopt(3) library so that it will work correctly in subprocesses.
753 752
 	 */
754 753
 	optind = 1;
755  
-#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
  754
+#ifdef HAVE_INT_OPTRESET
756 755
 	optreset = 1;				/* some systems need this too */
757 756
 #endif
758 757
 
5  src/backend/tcop/postgres.c
@@ -78,8 +78,7 @@
78 78
 extern char *optarg;
79 79
 extern int	optind;
80 80
 
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
83 82
 extern int	optreset;			/* might not be declared by system headers */
84 83
 #endif
85 84
 
@@ -3443,7 +3442,7 @@ process_postgres_switches(int argc, char *argv[], GucContext ctx)
3443 3442
 	 * or when this function is called a second time with another array.
3444 3443
 	 */
3445 3444
 	optind = 1;
3446  
-#if defined(HAVE_INT_OPTRESET) || !defined(HAVE_GETOPT)
  3445
+#ifdef HAVE_INT_OPTRESET
3447 3446
 	optreset = 1;				/* some systems need this too */
3448 3447
 #endif
3449 3448
 
1  src/include/getopt_long.h
@@ -18,7 +18,6 @@ extern int	opterr;
18 18
 extern int	optind;
19 19
 extern int	optopt;
20 20
 extern char *optarg;
21  
-extern int	optreset;
22 21
 
23 22
 #ifndef HAVE_STRUCT_OPTION
24 23
 
20  src/port/getopt.c
@@ -41,7 +41,7 @@ static char sccsid[] = "@(#)getopt.c	8.3 (Berkeley) 4/27/95";
41 41
  * On some versions of Solaris, opterr and friends are defined in core libc
42 42
  * rather than in a separate getopt module.  Define these variables only
43 43
  * 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.)
45 45
  */
46 46
 #ifndef HAVE_INT_OPTERR
47 47
 
@@ -57,12 +57,6 @@ extern int	optopt;
57 57
 extern char *optarg;
58 58
 #endif
59 59
 
60  
-#ifndef HAVE_INT_OPTRESET
61  
-int			optreset;			/* reset getopt */
62  
-#else
63  
-extern int	optreset;
64  
-#endif
65  
-
66 60
 #define BADCH	(int)'?'
67 61
 #define BADARG	(int)':'
68 62
 #define EMSG	""
@@ -70,6 +64,12 @@ extern int	optreset;
70 64
 /*
71 65
  * getopt
72 66
  *	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.)
73 73
  */
74 74
 int
75 75
 getopt(nargc, nargv, ostr)
@@ -80,9 +80,8 @@ const char *ostr;
80 80
 	static char *place = EMSG;	/* option letter processing */
81 81
 	char	   *oli;			/* option letter list index */
82 82
 
83  
-	if (optreset || !*place)
  83
+	if (!*place)
84 84
 	{							/* update scanning pointer */
85  
-		optreset = 0;
86 85
 		if (optind >= nargc || *(place = nargv[optind]) != '-')
87 86
 		{
88 87
 			place = EMSG;
@@ -102,7 +101,10 @@ const char *ostr;
102 101
 		 * if the user didn't specify '-' as an option, assume it means -1.
103 102
 		 */
104 103
 		if (optopt == (int) '-')
  104
+		{
  105
+			place = EMSG;
105 106
 			return -1;
  107
+		}
106 108
 		if (!*place)
107 109
 			++optind;
108 110
 		if (opterr && *ostr != ':')
20  src/port/getopt_long.c
@@ -38,17 +38,21 @@
38 38
 
39 39
 #include "getopt_long.h"
40 40
 
41  
-#ifndef HAVE_INT_OPTRESET
42  
-int			optreset;
43  
-
44  
-/* else the "extern" was provided by getopt_long.h */
45  
-#endif
46  
-
47 41
 #define BADCH	'?'
48 42
 #define BADARG	':'
49 43
 #define EMSG	""
50 44
 
51 45
 
  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
+ */
52 56
 int
53 57
 getopt_long(int argc, char *const argv[],
54 58
 			const char *optstring,
@@ -57,10 +61,8 @@ getopt_long(int argc, char *const argv[],
57 61
 	static char *place = EMSG;	/* option letter processing */
58 62
 	char	   *oli;			/* option letter list index */
59 63
 
60  
-	if (optreset || !*place)
  64
+	if (!*place)
61 65
 	{							/* update scanning pointer */
62  
-		optreset = 0;
63  
-
64 66
 		if (optind >= argc)
65 67
 		{
66 68
 			place = EMSG;

0 notes on commit 61b5369

Please sign in to comment.
Something went wrong with that request. Please try again.