diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-15 15:44:41 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2017-08-15 15:44:41 +0200 |
commit | 9832bbaba966f0e52e183f10cd93fad7f8f643fe (patch) | |
tree | e79e43916984e0d1a575c30ae2dd9a3893331316 | |
parent | 4476c703015d026dfd8057a28010c33943aa2a9c (diff) | |
download | busybox-9832bbaba966f0e52e183f10cd93fad7f8f643fe.tar.gz |
ash: unset OPTARG if getopts exits 1, support OPTERR=0 behavior
function old new delta
getoptscmd 522 547 +25
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | shell/ash.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/shell/ash.c b/shell/ash.c index 58999fac5..703802ff5 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -10948,6 +10948,7 @@ getopts(char *optstr, char *optvar, char **optfirst) p = *optnext; if (p == NULL || *p != '-' || *++p == '\0') { atend: + unsetvar("OPTARG"); p = NULL; done = 1; goto out; @@ -10960,7 +10961,11 @@ getopts(char *optstr, char *optvar, char **optfirst) c = *p++; for (q = optstr; *q != c;) { if (*q == '\0') { - if (optstr[0] == ':') { + /* OPTERR is a bashism */ + const char *cp = lookupvar("OPTERR"); + if ((cp && LONE_CHAR(cp, '0')) + || (optstr[0] == ':') + ) { sbuf[0] = c; /*sbuf[1] = '\0'; - already is */ setvar0("OPTARG", sbuf); @@ -10977,7 +10982,11 @@ getopts(char *optstr, char *optvar, char **optfirst) if (*++q == ':') { if (*p == '\0' && (p = *optnext) == NULL) { - if (optstr[0] == ':') { + /* OPTERR is a bashism */ + const char *cp = lookupvar("OPTERR"); + if ((cp && LONE_CHAR(cp, '0')) + || (optstr[0] == ':') + ) { sbuf[0] = c; /*sbuf[1] = '\0'; - already is */ setvar0("OPTARG", sbuf); |