[git commit] ash: [BUILTIN] Treat OPTIND=0 in the same way as OPTIND=1

Denys Vlasenko vda.linux at googlemail.com
Wed Oct 26 00:03:37 UTC 2016


commit: https://git.busybox.net/busybox/commit/?id=a513bf3c3ce0756c991b21c0ca271e24fedcdb51
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Upstream commit:

    Date: Sat, 6 Oct 2007 18:59:31 +0800
    [BUILTIN] Treat OPTIND=0 in the same way as OPTIND=1

    Previously setting OPTIND to 0 would cause subsequent getopts calls to fail.
    This patch makes dash reset the getopts parameters the same way as OPTIND=1.

    Both behaviours are allowed by POSIX but other common shells do tolerate this
    case.

function                                             old     new   delta
getoptsreset                                          24      30      +6
getoptscmd                                           632     614     -18

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 shell/ash.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/shell/ash.c b/shell/ash.c
index e30d7fe..647fc81 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -2019,7 +2019,7 @@ extern struct globals_var *const ash_ptr_to_globals_var;
 static void FAST_FUNC
 getoptsreset(const char *value)
 {
-	shellparam.optind = number(value);
+	shellparam.optind = number(value) ?: 1;
 	shellparam.optoff = -1;
 }
 #endif
@@ -10571,8 +10571,6 @@ getopts(char *optstr, char *optvar, char **optfirst, int *param_optind, int *opt
 
 	sbuf[1] = '\0';
 
-	if (*param_optind < 1)
-		return 1;
 	optnext = optfirst + *param_optind - 1;
 
 	if (*param_optind <= 1 || *optoff < 0 || (int)strlen(optnext[-1]) < *optoff)


More information about the busybox-cvs mailing list