[PATCH] Resetting getopt

Shaun Jackman sjackman at gmail.com
Sun Jul 23 18:28:19 UTC 2006


Ping!

On 7/7/06, Shaun Jackman <sjackman at gmail.com> wrote:
> On 7/6/06, Rob Landley <rob at landley.net> wrote:
> > On Thursday 06 July 2006 7:49 pm, Shaun Jackman wrote:
> > > There seems to be a bug in every applet -- mostly shells -- that
> > > attempt to reset getopt. They all set `optind = 1;', but the getopt
> > > implementations I've read (uClibc and newlib) use the test `if (optind
> > > == 0)' to reset the getopt internals. Specifically, the static pointer
> > > that iterates through short options needs resetting. I'd recommended
> > > changing all the occurrences of `optind = 1;' to `optind = 0;'. Any
> > > thoughts of any complications this could cause? The only one I can
> > > think of is a possibly errant applet that depends on optind == 1
> > > *before* getopt is called. All will be fine after getopt is called for
> > > the first time.
> >
> > Resetting it to 0 should be fine.  Any applet that depends on the value of
> > optind before calling getopt is buggy.
>
> As discussed above. Please apply.
>
> Cheers,
> Shaun
>
> 2006-07-07  Shaun Jackman  <sjackman at gmail.com>
>
>         * archival/gzip.c: Set optind to 0 before calling getopt.
>         * shell/hush.c: Ditto.
>         * shell/lash.c: Ditto.
>         * shell/msh.c: Ditto.
>
> --- archival/gzip.c     672bf03d09e8a91b2e710ca4b94ad2e1a333a774
> +++ archival/gzip.c     61c04f49b251c814533549686870048e7708f3fd
> @@ -1153,7 +1153,7 @@
>                         break;
>  #ifdef CONFIG_GUNZIP
>                 case 'd':
> -                       optind = 1;
> +                       optind = 0;
>                         return gunzip_main(argc, argv);
>  #endif
>                 default:
> ============================================================
> --- shell/hush.c        5eb0f818c6816893a800208274529061be8cfdd1
> +++ shell/hush.c        0e1bc4709222683ff33a1dd747956812d4ccb68a
> @@ -1108,7 +1108,7 @@
>
>                         /* Count argc for use in a second... */
>                         for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
> -                       optind = 1;
> +                       optind = 0;
>                         debug_printf("running applet %s\n", name);
>                         run_applet_by_name(name, argc_l, child->argv);
>                 }
> ============================================================
> --- shell/lash.c        d8a568319f0f16f25eaa53770b926f927760e891
> +++ shell/lash.c        6280109425b1028a3daf73149bba5d4ef9615da2
> @@ -1207,7 +1207,7 @@
>                 int argc_l;
>
>                 for(argc_l=0; *argv_l; argv_l++, argc_l++);
> -               optind = 1;
> +               optind = 0;
>                 run_applet_by_name(child->argv[0], argc_l, child->argv);
>         }
>
> ============================================================
> --- shell/msh.c 6641c546644723bb90fe10772688fa1ec94d76d2
> +++ shell/msh.c 7beff8013493adbebb7e7480037fb62dff408232
> @@ -3169,7 +3169,7 @@
>  #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
>         char *name = c;
>
> -       optind = 1;
> +       optind = 0;
>         if (find_applet_by_name(name)) {
>                 /* We have to exec here since we vforked.  Running
>                  * run_applet_by_name() won't work and bad things
-------------- next part --------------
2006-07-07  Shaun Jackman  <sjackman at gmail.com>

	* archival/gzip.c: Set optind to 0 before calling getopt.
	* shell/hush.c: Ditto.
	* shell/lash.c: Ditto.
	* shell/msh.c: Ditto.
	* shell/ash.c: Ditto.

--- archival/gzip.c	672bf03d09e8a91b2e710ca4b94ad2e1a333a774
+++ archival/gzip.c	61c04f49b251c814533549686870048e7708f3fd
@@ -1153,7 +1153,7 @@
 			break;
 #ifdef CONFIG_GUNZIP
 		case 'd':
-			optind = 1;
+			optind = 0;
 			return gunzip_main(argc, argv);
 #endif
 		default:
============================================================
--- shell/hush.c	5eb0f818c6816893a800208274529061be8cfdd1
+++ shell/hush.c	0e1bc4709222683ff33a1dd747956812d4ccb68a
@@ -1108,7 +1108,7 @@
 
 			/* Count argc for use in a second... */
 			for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
-			optind = 1;
+			optind = 0;
 			debug_printf("running applet %s\n", name);
 			run_applet_by_name(name, argc_l, child->argv);
 		}
============================================================
--- shell/lash.c	d8a568319f0f16f25eaa53770b926f927760e891
+++ shell/lash.c	6280109425b1028a3daf73149bba5d4ef9615da2
@@ -1207,7 +1207,7 @@
 		int argc_l;
 
 		for(argc_l=0; *argv_l; argv_l++, argc_l++);
-		optind = 1;
+		optind = 0;
 		run_applet_by_name(child->argv[0], argc_l, child->argv);
 	}
 
============================================================
--- shell/msh.c	6641c546644723bb90fe10772688fa1ec94d76d2
+++ shell/msh.c	7beff8013493adbebb7e7480037fb62dff408232
@@ -3169,7 +3169,7 @@
 #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
 	char *name = c;
 
-	optind = 1;
+	optind = 0;
 	if (find_applet_by_name(name)) {
 		/* We have to exec here since we vforked.  Running
 		 * run_applet_by_name() won't work and bad things


More information about the busybox mailing list