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>
This commit is contained in:
Denys Vlasenko 2016-10-26 17:54:32 +02:00
parent 35c2a136cd
commit dbef38a74b

View File

@ -2258,32 +2258,6 @@ setvar0(const char *name, const char *val)
setvar(name, val, 0); 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. * Unset the specified variable.
*/ */
@ -10559,21 +10533,20 @@ getopts(char *optstr, char *optvar, char **optfirst)
char *p, *q; char *p, *q;
char c = '?'; char c = '?';
int done = 0; int done = 0;
int err = 0;
char sbuf[2]; char sbuf[2];
char **optnext; char **optnext;
int ind = shellparam.optind;
int off = shellparam.optoff;
sbuf[1] = '\0'; sbuf[1] = '\0';
optnext = optfirst + shellparam.optind - 1; shellparam.optind = -1;
optnext = optfirst + ind - 1;
if (shellparam.optind <= 1 if (ind <= 1 || off < 0 || (int)strlen(optnext[-1]) < off)
|| shellparam.optoff < 0
|| (int)strlen(optnext[-1]) < shellparam.optoff
) {
p = NULL; p = NULL;
} else else
p = optnext[-1] + shellparam.optoff; p = optnext[-1] + off;
if (p == NULL || *p == '\0') { if (p == NULL || *p == '\0') {
/* Current word is done, advance */ /* Current word is done, advance */
p = *optnext; p = *optnext;
@ -10594,7 +10567,7 @@ getopts(char *optstr, char *optvar, char **optfirst)
if (optstr[0] == ':') { if (optstr[0] == ':') {
sbuf[0] = c; sbuf[0] = c;
/*sbuf[1] = '\0'; - already is */ /*sbuf[1] = '\0'; - already is */
err |= setvarsafe("OPTARG", sbuf, 0); setvar0("OPTARG", sbuf);
} else { } else {
fprintf(stderr, "Illegal option -%c\n", c); fprintf(stderr, "Illegal option -%c\n", c);
unsetvar("OPTARG"); unsetvar("OPTARG");
@ -10611,7 +10584,7 @@ getopts(char *optstr, char *optvar, char **optfirst)
if (optstr[0] == ':') { if (optstr[0] == ':') {
sbuf[0] = c; sbuf[0] = c;
/*sbuf[1] = '\0'; - already is */ /*sbuf[1] = '\0'; - already is */
err |= setvarsafe("OPTARG", sbuf, 0); setvar0("OPTARG", sbuf);
c = ':'; c = ':';
} else { } else {
fprintf(stderr, "No arg for -%c option\n", c); fprintf(stderr, "No arg for -%c option\n", c);
@ -10623,23 +10596,20 @@ getopts(char *optstr, char *optvar, char **optfirst)
if (p == *optnext) if (p == *optnext)
optnext++; optnext++;
err |= setvarsafe("OPTARG", p, 0); setvar0("OPTARG", p);
p = NULL; p = NULL;
} else } else
err |= setvarsafe("OPTARG", nullstr, 0); setvar0("OPTARG", nullstr);
out: out:
shellparam.optoff = p ? p - *(optnext - 1) : -1; ind = optnext - optfirst + 1;
shellparam.optind = optnext - optfirst + 1; setvar("OPTIND", itoa(ind), VNOFUNC);
err |= setvarsafe("OPTIND", itoa(shellparam.optind), VNOFUNC);
sbuf[0] = c; sbuf[0] = c;
/*sbuf[1] = '\0'; - already is */ /*sbuf[1] = '\0'; - already is */
err |= setvarsafe(optvar, sbuf, 0); setvar0(optvar, sbuf);
if (err) {
shellparam.optind = 1; shellparam.optoff = p ? p - *(optnext - 1) : -1;
shellparam.optoff = -1; shellparam.optind = ind;
flush_stdout_stderr();
raise_exception(EXERROR);
}
return done; return done;
} }
@ -10658,13 +10628,13 @@ getoptscmd(int argc, char **argv)
ash_msg_and_raise_error("usage: getopts optstring var [arg]"); ash_msg_and_raise_error("usage: getopts optstring var [arg]");
if (argc == 3) { if (argc == 3) {
optbase = shellparam.p; optbase = shellparam.p;
if (shellparam.optind > shellparam.nparam + 1) { if ((unsigned)shellparam.optind > shellparam.nparam + 1) {
shellparam.optind = 1; shellparam.optind = 1;
shellparam.optoff = -1; shellparam.optoff = -1;
} }
} else { } else {
optbase = &argv[3]; optbase = &argv[3];
if (shellparam.optind > argc - 2) { if ((unsigned)shellparam.optind > argc - 2) {
shellparam.optind = 1; shellparam.optind = 1;
shellparam.optoff = -1; shellparam.optoff = -1;
} }