ash: align --login code with dash

Upstream commit:

    Date: Sun, 13 Jul 2008 22:34:50 +0800
    [OPTIONS] Added support for -l

    This patch adds support for the -l option (login shell) as required
    by the LSB.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

It's a bit bigger, but gets rid of one global variable

function                                             old     new   delta
options                                              554     576     +22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-07-31 19:43:47 +02:00
parent 78b1b1b07a
commit ec05df13b0

View File

@ -328,7 +328,6 @@ struct globals_misc {
#define EXERROR 1 /* a generic error */ #define EXERROR 1 /* a generic error */
#define EXEXIT 4 /* exit the shell */ #define EXEXIT 4 /* exit the shell */
smallint isloginsh;
char nullstr[1]; /* zero length string */ char nullstr[1]; /* zero length string */
char optlist[NOPTS]; char optlist[NOPTS];
@ -397,7 +396,6 @@ extern struct globals_misc *const ash_ptr_to_globals_misc;
#define pending_int (G_misc.pending_int ) #define pending_int (G_misc.pending_int )
#define got_sigchld (G_misc.got_sigchld ) #define got_sigchld (G_misc.got_sigchld )
#define pending_sig (G_misc.pending_sig ) #define pending_sig (G_misc.pending_sig )
#define isloginsh (G_misc.isloginsh )
#define nullstr (G_misc.nullstr ) #define nullstr (G_misc.nullstr )
#define optlist (G_misc.optlist ) #define optlist (G_misc.optlist )
#define sigmode (G_misc.sigmode ) #define sigmode (G_misc.sigmode )
@ -10721,7 +10719,7 @@ setoption(int flag, int val)
/* NOTREACHED */ /* NOTREACHED */
} }
static int static int
options(int cmdline) options(int cmdline, int *login_sh)
{ {
char *p; char *p;
int val; int val;
@ -10762,11 +10760,14 @@ options(int cmdline)
if (*argptr) if (*argptr)
argptr++; argptr++;
} else if (cmdline && (c == 'l')) { /* -l or +l == --login */ } else if (cmdline && (c == 'l')) { /* -l or +l == --login */
isloginsh = 1; if (login_sh)
*login_sh = 1;
/* bash does not accept +-login, we also won't */ /* bash does not accept +-login, we also won't */
} else if (cmdline && val && (c == '-')) { /* long options */ } else if (cmdline && val && (c == '-')) { /* long options */
if (strcmp(p, "login") == 0) if (strcmp(p, "login") == 0) {
isloginsh = 1; if (login_sh)
*login_sh = 1;
}
break; break;
} else { } else {
setoption(c, val); setoption(c, val);
@ -10850,7 +10851,7 @@ setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
return showvars(nullstr, 0, VUNSET); return showvars(nullstr, 0, VUNSET);
INT_OFF; INT_OFF;
retval = options(/*cmdline:*/ 0); retval = options(/*cmdline:*/ 0, NULL);
if (retval == 0) { /* if no parse error... */ if (retval == 0) { /* if no parse error... */
optschanged(); optschanged();
if (*argptr != NULL) { if (*argptr != NULL) {
@ -13602,21 +13603,23 @@ init(void)
/* /*
* Process the shell command line arguments. * Process the shell command line arguments.
*/ */
static void static int
procargs(char **argv) procargs(char **argv)
{ {
int i; int i;
const char *xminusc; const char *xminusc;
char **xargv; char **xargv;
int login_sh;
xargv = argv; xargv = argv;
login_sh = xargv[0] && xargv[0][0] == '-';
arg0 = xargv[0]; arg0 = xargv[0];
/* if (xargv[0]) - mmm, this is always true! */ /* if (xargv[0]) - mmm, this is always true! */
xargv++; xargv++;
for (i = 0; i < NOPTS; i++) for (i = 0; i < NOPTS; i++)
optlist[i] = 2; optlist[i] = 2;
argptr = xargv; argptr = xargv;
if (options(/*cmdline:*/ 1)) { if (options(/*cmdline:*/ 1, &login_sh)) {
/* it already printed err message */ /* it already printed err message */
raise_exception(EXERROR); raise_exception(EXERROR);
} }
@ -13660,6 +13663,8 @@ procargs(char **argv)
xargv++; xargv++;
} }
optschanged(); optschanged();
return login_sh;
} }
/* /*
@ -13720,6 +13725,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
volatile smallint state; volatile smallint state;
struct jmploc jmploc; struct jmploc jmploc;
struct stackmark smark; struct stackmark smark;
int login_sh;
/* Initialize global data */ /* Initialize global data */
INIT_G_misc(); INIT_G_misc();
@ -13768,15 +13774,13 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
init(); init();
setstackmark(&smark); setstackmark(&smark);
procargs(argv); login_sh = procargs(argv);
#if DEBUG #if DEBUG
TRACE(("Shell args: ")); TRACE(("Shell args: "));
trace_puts_args(argv); trace_puts_args(argv);
#endif #endif
if (argv[0] && argv[0][0] == '-') if (login_sh) {
isloginsh = 1;
if (isloginsh) {
const char *hp; const char *hp;
state = 1; state = 1;