aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-01-28 22:57:10 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-01-28 22:57:10 +0000
commit9772816570f0a63ac301f1885292b064e23f5310 (patch)
treea8e9c2947cd4dbc718989d508dfc742840a16e81
parent73032ca04be49c096f745f0873d67d9c831642bd (diff)
downloadbusybox-w32-9772816570f0a63ac301f1885292b064e23f5310.tar.gz
busybox-w32-9772816570f0a63ac301f1885292b064e23f5310.tar.bz2
busybox-w32-9772816570f0a63ac301f1885292b064e23f5310.zip
*: move getopt reset code to better place(s)
-rw-r--r--libbb/appletlib.c1
-rw-r--r--libbb/getopt32.c27
-rw-r--r--libbb/vfork_daemon_rexec.c20
-rw-r--r--util-linux/getopt.c9
4 files changed, 31 insertions, 26 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 80adff5e7..993618e98 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -638,7 +638,6 @@ void run_applet_no_and_exit(int applet_no, char **argv)
638 argc++; 638 argc++;
639 639
640 /* Reinit some shared global data */ 640 /* Reinit some shared global data */
641 optind = 1;
642 xfunc_error_retval = EXIT_FAILURE; 641 xfunc_error_retval = EXIT_FAILURE;
643 642
644 applet_name = APPLET_NAME(applet_no); 643 applet_name = APPLET_NAME(applet_no);
diff --git a/libbb/getopt32.c b/libbb/getopt32.c
index 2452eb0a5..80d5d2822 100644
--- a/libbb/getopt32.c
+++ b/libbb/getopt32.c
@@ -473,11 +473,30 @@ getopt32(char **argv, const char *applet_opts, ...)
473 } 473 }
474 } 474 }
475 475
476 /* In case getopt32 was already called, reinit some state */ 476 /* In case getopt32 was already called:
477 * reset the libc getopt() function, which keeps internal state.
478 *
479 * BSD-derived getopt() functions require that optind be set to 1 in
480 * order to reset getopt() state. This used to be generally accepted
481 * way of resetting getopt(). However, glibc's getopt()
482 * has additional getopt() state beyond optind, and requires that
483 * optind be set to zero to reset its state. So the unfortunate state of
484 * affairs is that BSD-derived versions of getopt() misbehave if
485 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
486 * will core dump if optind is set 1 in order to reset getopt().
487 *
488 * More modern versions of BSD require that optreset be set to 1 in
489 * order to reset getopt(). Sigh. Standards, anyone?
490 */
491#ifdef __GLIBC__
492 optind = 0;
493#else /* BSD style */
477 optind = 1; 494 optind = 1;
478 /* optarg = NULL; opterr = 0; optopt = 0; ?? */ 495 /* optreset = 1; */
496#endif
497 /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
479 498
480 /* Note: just "getopt() <= 0" will not work good for 499 /* Note: just "getopt() <= 0" will not work well for
481 * "fake" short options, like this one: 500 * "fake" short options, like this one:
482 * wget $'-\203' "Test: test" http://kernel.org/ 501 * wget $'-\203' "Test: test" http://kernel.org/
483 * (supposed to act as --header, but doesn't) */ 502 * (supposed to act as --header, but doesn't) */
@@ -487,7 +506,7 @@ getopt32(char **argv, const char *applet_opts, ...)
487#else 506#else
488 while ((c = getopt(argc, argv, applet_opts)) != -1) { 507 while ((c = getopt(argc, argv, applet_opts)) != -1) {
489#endif 508#endif
490 c &= 0xff; /* fight libc's sign extends */ 509 c &= 0xff; /* fight libc's sign extension */
491 loop_arg_is_opt: 510 loop_arg_is_opt:
492 for (on_off = complementary; on_off->opt != c; on_off++) { 511 for (on_off = complementary; on_off->opt != c; on_off++) {
493 /* c==0 if long opt have non NULL flag */ 512 /* c==0 if long opt have non NULL flag */
diff --git a/libbb/vfork_daemon_rexec.c b/libbb/vfork_daemon_rexec.c
index 3a386c5c9..98339c930 100644
--- a/libbb/vfork_daemon_rexec.c
+++ b/libbb/vfork_daemon_rexec.c
@@ -137,26 +137,6 @@ int run_nofork_applet_prime(struct nofork_save_area *old, int applet_no, char **
137 * die_sleep and longjmp here instead. */ 137 * die_sleep and longjmp here instead. */
138 die_sleep = -1; 138 die_sleep = -1;
139 139
140 /* Reset the libc getopt() function, which keeps internal state.
141 *
142 * BSD-derived getopt() functions require that optind be reset to 1 in
143 * order to reset getopt() state. This used to be generally accepted
144 * way of resetting getopt(). However, glibc's getopt()
145 * has additional getopt() state beyond optind, and requires that
146 * optind be set zero to reset its state. So the unfortunate state of
147 * affairs is that BSD-derived versions of getopt() misbehave if
148 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
149 * will core ump if optind is set 1 in order to reset getopt().
150 *
151 * More modern versions of BSD require that optreset be set to 1 in
152 * order to reset getopt(). Sigh. Standards, anyone?
153 */
154#ifdef __GLIBC__
155 optind = 0;
156#else /* BSD style */
157 optind = 1;
158 /* optreset = 1; */
159#endif
160 /* option_mask32 = 0; - not needed */ 140 /* option_mask32 = 0; - not needed */
161 141
162 argc = 1; 142 argc = 1;
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index 061750e77..ee6c14393 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -155,7 +155,14 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru
155 155
156 if (quiet_errors) /* No error reporting from getopt(3) */ 156 if (quiet_errors) /* No error reporting from getopt(3) */
157 opterr = 0; 157 opterr = 0;
158 optind = 0; /* Reset getopt(3) */ 158
159 /* Reset getopt(3) (see libbb/getopt32.c for long rant) */
160#ifdef __GLIBC__
161 optind = 0;
162#else /* BSD style */
163 optind = 1;
164 /* optreset = 1; */
165#endif
159 166
160 while (1) { 167 while (1) {
161 opt = 168 opt =