diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2016-10-26 17:54:32 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-10-26 17:54:32 +0200 |
commit | dbef38a74b825c15dcce737fab51c594d93b6d59 (patch) | |
tree | 32a8071cbb79207fa8cd4e2262ab9ba3fa8641dd /shell | |
parent | 35c2a136cd74402932d94ca69bd5f69ca900d83f (diff) |
ash: [VAR] Remove setvarsafe
Upstream commit:
Date: Sat, 6 Oct 2007 21:18:58 +0800
[VAR] Remove setvarsafe
The only user of setvarsafe is getopts. However, we can achieve the same
result by pre-setting the value of shellparam.optind.
function old new delta
getoptscmd 614 515 -99
setvarsafe 147 - -147
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 0/1 up/down: 0/-246) Total: -246 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'shell')
-rw-r--r-- | shell/ash.c | 70 |
1 files changed, 20 insertions, 50 deletions
diff --git a/shell/ash.c b/shell/ash.c index 3effa0c81..2c439602a 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2258,32 +2258,6 @@ setvar0(const char *name, const char *val) setvar(name, val, 0); } -#if ENABLE_ASH_GETOPTS -/* - * Safe version of setvar, returns 1 on success 0 on failure. - */ -static int -setvarsafe(const char *name, const char *val, int flags) -{ - int err; - volatile int saveint; - struct jmploc *volatile savehandler = exception_handler; - struct jmploc jmploc; - - SAVE_INT(saveint); - if (setjmp(jmploc.loc)) - err = 1; - else { - exception_handler = &jmploc; - setvar(name, val, flags); - err = 0; - } - exception_handler = savehandler; - RESTORE_INT(saveint); - return err; -} -#endif - /* * Unset the specified variable. */ @@ -10559,21 +10533,20 @@ getopts(char *optstr, char *optvar, char **optfirst) char *p, *q; char c = '?'; int done = 0; - int err = 0; char sbuf[2]; char **optnext; + int ind = shellparam.optind; + int off = shellparam.optoff; sbuf[1] = '\0'; - optnext = optfirst + shellparam.optind - 1; + shellparam.optind = -1; + optnext = optfirst + ind - 1; - if (shellparam.optind <= 1 - || shellparam.optoff < 0 - || (int)strlen(optnext[-1]) < shellparam.optoff - ) { + if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off) p = NULL; - } else - p = optnext[-1] + shellparam.optoff; + else + p = optnext[-1] + off; if (p == NULL || *p == '\0') { /* Current word is done, advance */ p = *optnext; @@ -10594,7 +10567,7 @@ getopts(char *optstr, char *optvar, char **optfirst) if (optstr[0] == ':') { sbuf[0] = c; /*sbuf[1] = '\0'; - already is */ - err |= setvarsafe("OPTARG", sbuf, 0); + setvar0("OPTARG", sbuf); } else { fprintf(stderr, "Illegal option -%c\n", c); unsetvar("OPTARG"); @@ -10611,7 +10584,7 @@ getopts(char *optstr, char *optvar, char **optfirst) if (optstr[0] == ':') { sbuf[0] = c; /*sbuf[1] = '\0'; - already is */ - err |= setvarsafe("OPTARG", sbuf, 0); + setvar0("OPTARG", sbuf); c = ':'; } else { fprintf(stderr, "No arg for -%c option\n", c); @@ -10623,23 +10596,20 @@ getopts(char *optstr, char *optvar, char **optfirst) if (p == *optnext) optnext++; - err |= setvarsafe("OPTARG", p, 0); + setvar0("OPTARG", p); p = NULL; } else - err |= setvarsafe("OPTARG", nullstr, 0); + setvar0("OPTARG", nullstr); out: - shellparam.optoff = p ? p - *(optnext - 1) : -1; - shellparam.optind = optnext - optfirst + 1; - err |= setvarsafe("OPTIND", itoa(shellparam.optind), VNOFUNC); + ind = optnext - optfirst + 1; + setvar("OPTIND", itoa(ind), VNOFUNC); sbuf[0] = c; /*sbuf[1] = '\0'; - already is */ - err |= setvarsafe(optvar, sbuf, 0); - if (err) { - shellparam.optind = 1; - shellparam.optoff = -1; - flush_stdout_stderr(); - raise_exception(EXERROR); - } + setvar0(optvar, sbuf); + + shellparam.optoff = p ? p - *(optnext - 1) : -1; + shellparam.optind = ind; + return done; } @@ -10658,13 +10628,13 @@ getoptscmd(int argc, char **argv) ash_msg_and_raise_error("usage: getopts optstring var [arg]"); if (argc == 3) { optbase = shellparam.p; - if (shellparam.optind > shellparam.nparam + 1) { + if ((unsigned)shellparam.optind > shellparam.nparam + 1) { shellparam.optind = 1; shellparam.optoff = -1; } } else { optbase = &argv[3]; - if (shellparam.optind > argc - 2) { + if ((unsigned)shellparam.optind > argc - 2) { shellparam.optind = 1; shellparam.optoff = -1; } |