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 | |
parent | 35c2a136cd74402932d94ca69bd5f69ca900d83f (diff) | |
download | busybox-w32-dbef38a74b825c15dcce737fab51c594d93b6d59.tar.gz busybox-w32-dbef38a74b825c15dcce737fab51c594d93b6d59.tar.bz2 busybox-w32-dbef38a74b825c15dcce737fab51c594d93b6d59.zip |
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>
-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) | |||
2258 | setvar(name, val, 0); | 2258 | setvar(name, val, 0); |
2259 | } | 2259 | } |
2260 | 2260 | ||
2261 | #if ENABLE_ASH_GETOPTS | ||
2262 | /* | ||
2263 | * Safe version of setvar, returns 1 on success 0 on failure. | ||
2264 | */ | ||
2265 | static int | ||
2266 | setvarsafe(const char *name, const char *val, int flags) | ||
2267 | { | ||
2268 | int err; | ||
2269 | volatile int saveint; | ||
2270 | struct jmploc *volatile savehandler = exception_handler; | ||
2271 | struct jmploc jmploc; | ||
2272 | |||
2273 | SAVE_INT(saveint); | ||
2274 | if (setjmp(jmploc.loc)) | ||
2275 | err = 1; | ||
2276 | else { | ||
2277 | exception_handler = &jmploc; | ||
2278 | setvar(name, val, flags); | ||
2279 | err = 0; | ||
2280 | } | ||
2281 | exception_handler = savehandler; | ||
2282 | RESTORE_INT(saveint); | ||
2283 | return err; | ||
2284 | } | ||
2285 | #endif | ||
2286 | |||
2287 | /* | 2261 | /* |
2288 | * Unset the specified variable. | 2262 | * Unset the specified variable. |
2289 | */ | 2263 | */ |
@@ -10559,21 +10533,20 @@ getopts(char *optstr, char *optvar, char **optfirst) | |||
10559 | char *p, *q; | 10533 | char *p, *q; |
10560 | char c = '?'; | 10534 | char c = '?'; |
10561 | int done = 0; | 10535 | int done = 0; |
10562 | int err = 0; | ||
10563 | char sbuf[2]; | 10536 | char sbuf[2]; |
10564 | char **optnext; | 10537 | char **optnext; |
10538 | int ind = shellparam.optind; | ||
10539 | int off = shellparam.optoff; | ||
10565 | 10540 | ||
10566 | sbuf[1] = '\0'; | 10541 | sbuf[1] = '\0'; |
10567 | 10542 | ||
10568 | optnext = optfirst + shellparam.optind - 1; | 10543 | shellparam.optind = -1; |
10544 | optnext = optfirst + ind - 1; | ||
10569 | 10545 | ||
10570 | if (shellparam.optind <= 1 | 10546 | if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off) |
10571 | || shellparam.optoff < 0 | ||
10572 | || (int)strlen(optnext[-1]) < shellparam.optoff | ||
10573 | ) { | ||
10574 | p = NULL; | 10547 | p = NULL; |
10575 | } else | 10548 | else |
10576 | p = optnext[-1] + shellparam.optoff; | 10549 | p = optnext[-1] + off; |
10577 | if (p == NULL || *p == '\0') { | 10550 | if (p == NULL || *p == '\0') { |
10578 | /* Current word is done, advance */ | 10551 | /* Current word is done, advance */ |
10579 | p = *optnext; | 10552 | p = *optnext; |
@@ -10594,7 +10567,7 @@ getopts(char *optstr, char *optvar, char **optfirst) | |||
10594 | if (optstr[0] == ':') { | 10567 | if (optstr[0] == ':') { |
10595 | sbuf[0] = c; | 10568 | sbuf[0] = c; |
10596 | /*sbuf[1] = '\0'; - already is */ | 10569 | /*sbuf[1] = '\0'; - already is */ |
10597 | err |= setvarsafe("OPTARG", sbuf, 0); | 10570 | setvar0("OPTARG", sbuf); |
10598 | } else { | 10571 | } else { |
10599 | fprintf(stderr, "Illegal option -%c\n", c); | 10572 | fprintf(stderr, "Illegal option -%c\n", c); |
10600 | unsetvar("OPTARG"); | 10573 | unsetvar("OPTARG"); |
@@ -10611,7 +10584,7 @@ getopts(char *optstr, char *optvar, char **optfirst) | |||
10611 | if (optstr[0] == ':') { | 10584 | if (optstr[0] == ':') { |
10612 | sbuf[0] = c; | 10585 | sbuf[0] = c; |
10613 | /*sbuf[1] = '\0'; - already is */ | 10586 | /*sbuf[1] = '\0'; - already is */ |
10614 | err |= setvarsafe("OPTARG", sbuf, 0); | 10587 | setvar0("OPTARG", sbuf); |
10615 | c = ':'; | 10588 | c = ':'; |
10616 | } else { | 10589 | } else { |
10617 | fprintf(stderr, "No arg for -%c option\n", c); | 10590 | fprintf(stderr, "No arg for -%c option\n", c); |
@@ -10623,23 +10596,20 @@ getopts(char *optstr, char *optvar, char **optfirst) | |||
10623 | 10596 | ||
10624 | if (p == *optnext) | 10597 | if (p == *optnext) |
10625 | optnext++; | 10598 | optnext++; |
10626 | err |= setvarsafe("OPTARG", p, 0); | 10599 | setvar0("OPTARG", p); |
10627 | p = NULL; | 10600 | p = NULL; |
10628 | } else | 10601 | } else |
10629 | err |= setvarsafe("OPTARG", nullstr, 0); | 10602 | setvar0("OPTARG", nullstr); |
10630 | out: | 10603 | out: |
10631 | shellparam.optoff = p ? p - *(optnext - 1) : -1; | 10604 | ind = optnext - optfirst + 1; |
10632 | shellparam.optind = optnext - optfirst + 1; | 10605 | setvar("OPTIND", itoa(ind), VNOFUNC); |
10633 | err |= setvarsafe("OPTIND", itoa(shellparam.optind), VNOFUNC); | ||
10634 | sbuf[0] = c; | 10606 | sbuf[0] = c; |
10635 | /*sbuf[1] = '\0'; - already is */ | 10607 | /*sbuf[1] = '\0'; - already is */ |
10636 | err |= setvarsafe(optvar, sbuf, 0); | 10608 | setvar0(optvar, sbuf); |
10637 | if (err) { | 10609 | |
10638 | shellparam.optind = 1; | 10610 | shellparam.optoff = p ? p - *(optnext - 1) : -1; |
10639 | shellparam.optoff = -1; | 10611 | shellparam.optind = ind; |
10640 | flush_stdout_stderr(); | 10612 | |
10641 | raise_exception(EXERROR); | ||
10642 | } | ||
10643 | return done; | 10613 | return done; |
10644 | } | 10614 | } |
10645 | 10615 | ||
@@ -10658,13 +10628,13 @@ getoptscmd(int argc, char **argv) | |||
10658 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); | 10628 | ash_msg_and_raise_error("usage: getopts optstring var [arg]"); |
10659 | if (argc == 3) { | 10629 | if (argc == 3) { |
10660 | optbase = shellparam.p; | 10630 | optbase = shellparam.p; |
10661 | if (shellparam.optind > shellparam.nparam + 1) { | 10631 | if ((unsigned)shellparam.optind > shellparam.nparam + 1) { |
10662 | shellparam.optind = 1; | 10632 | shellparam.optind = 1; |
10663 | shellparam.optoff = -1; | 10633 | shellparam.optoff = -1; |
10664 | } | 10634 | } |
10665 | } else { | 10635 | } else { |
10666 | optbase = &argv[3]; | 10636 | optbase = &argv[3]; |
10667 | if (shellparam.optind > argc - 2) { | 10637 | if ((unsigned)shellparam.optind > argc - 2) { |
10668 | shellparam.optind = 1; | 10638 | shellparam.optind = 1; |
10669 | shellparam.optoff = -1; | 10639 | shellparam.optoff = -1; |
10670 | } | 10640 | } |