X-Git-Url: https://gitweb.dragonflybsd.org/dragonfly.git/blobdiff_plain/7295dc4694612795b7574965e1527509404c9c0f..1bf9817349b98799b92b1e764d4d0855695ce4ea:/bin/sh/options.c diff --git a/bin/sh/options.c b/bin/sh/options.c index d4d880ce4c..e720284ffb 100644 --- a/bin/sh/options.c +++ b/bin/sh/options.c @@ -34,8 +34,7 @@ * SUCH DAMAGE. * * @(#)options.c 8.2 (Berkeley) 5/4/95 - * $FreeBSD: src/bin/sh/options.c,v 1.25 2006/04/09 12:20:42 stefanf Exp $ - * $DragonFly: src/bin/sh/options.c,v 1.7 2007/01/14 04:41:32 pavalos Exp $ + * $FreeBSD: src/bin/sh/options.c,v 1.35 2011/04/25 10:14:29 jilles Exp $ */ #include @@ -64,15 +63,15 @@ char *arg0; /* value of $0 */ struct shparam shellparam; /* current positional parameters */ char **argptr; /* argument list for builtin commands */ const char *shoptarg; /* set by nextopt (like getopt) */ -const char *optptr; /* used by nextopt */ +char *nextopt_optptr; /* used by nextopt */ char *minusc; /* argument to -c option */ -STATIC void options(int); -STATIC void minus_o(char *, int); -STATIC void setoption(int, int); -STATIC int getopts(char *, char *, char **, char ***, char **); +static void options(int); +static void minus_o(char *, int); +static void setoption(int, int); +static int getopts(char *, char *, char **, char ***, char **); /* @@ -93,8 +92,11 @@ procargs(int argc, char **argv) options(1); if (*argptr == NULL && minusc == NULL) sflag = 1; - if (iflag == 2 && sflag == 1 && isatty(0) && isatty(1)) + if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) { iflag = 1; + if (Eflag == 2) + Eflag = 1; + } if (mflag == 2) mflag = iflag; /* turn on tabcomplete in an interactive shell by default */ @@ -137,7 +139,7 @@ optschanged(void) * to the argument list; we advance it past the options. */ -STATIC void +static void options(int cmdline) { char *kp, *p; @@ -197,13 +199,8 @@ options(int cmdline) minus_o(*argptr, val); if (*argptr) argptr++; - } else { - if (c == 'p' && !val && privileged) { - setuid(getuid()); - setgid(getgid()); - } + } else setoption(c, val); - } } } return; @@ -246,7 +243,7 @@ end_options2: } } -STATIC void +static void minus_o(char *name, int val) { int i; @@ -260,21 +257,16 @@ minus_o(char *name, int val) optlist[i].val ? "on" : "off"); } else { /* Output suitable for re-input to shell. */ - for (i = 0; i < NOPTS; i++) { - if (i % 6 == 0) - out1str(i == 0 ? "set" : "\nset"); - out1fmt(" %co %s", optlist[i].val ? '-' : '+', - optlist[i].name); - } - out1c('\n'); + for (i = 0; i < NOPTS; i++) + out1fmt("%s %co %s%s", + i % 6 == 0 ? "set" : "", + optlist[i].val ? '-' : '+', + optlist[i].name, + i % 6 == 5 || i == NOPTS - 1 ? "\n" : ""); } } else { for (i = 0; i < NOPTS; i++) if (equal(name, optlist[i].name)) { - if (!val && privileged && equal(name, "privileged")) { - setuid(getuid()); - setgid(getgid()); - } setoption(optlist[i].letter, val); return; } @@ -283,11 +275,17 @@ minus_o(char *name, int val) } -STATIC void +static void setoption(int flag, int val) { int i; + if (flag == 'p' && !val && privileged) { + if (setgid(getgid()) == -1) + error("setgid"); + if (setuid(getuid()) == -1) + error("setuid"); + } for (i = 0; i < NOPTS; i++) if (optlist[i].letter == flag) { optlist[i].val = val; @@ -304,21 +302,6 @@ setoption(int flag, int val) } - -#ifdef mkinit -INCLUDE "options.h" - -SHELLPROC { - int i; - - for (i = 0; i < NOPTS; i++) - optlist[i].val = 0; - optschanged(); - -} -#endif - - /* * Set the shell parameters. */ @@ -340,6 +323,7 @@ setparam(char **argv) shellparam.malloc = 1; shellparam.nparam = nparam; shellparam.p = newparam; + shellparam.reset = 1; shellparam.optnext = NULL; } @@ -385,7 +369,7 @@ shiftcmd(int argc, char **argv) } ap2 = shellparam.p; while ((*ap2++ = *ap1++) != NULL); - shellparam.optnext = NULL; + shellparam.reset = 1; INTON; return 0; } @@ -416,7 +400,6 @@ void getoptsreset(const char *value) { if (number(value) == 1) { - shellparam.optnext = NULL; shellparam.reset = 1; } } @@ -450,7 +433,7 @@ getoptscmd(int argc, char **argv) &shellparam.optptr); } -STATIC int +static int getopts(char *optstr, char *optvar, char **optfirst, char ***optnext, char **optp) { @@ -558,10 +541,11 @@ out: int nextopt(const char *optstring) { - const char *p, *q; + char *p; + const char *q; char c; - if ((p = optptr) == NULL || *p == '\0') { + if ((p = nextopt_optptr) == NULL || *p == '\0') { p = *argptr; if (p == NULL || *p != '-' || *++p == '\0') return '\0'; @@ -582,6 +566,6 @@ nextopt(const char *optstring) shoptarg = p; p = NULL; } - optptr = p; + nextopt_optptr = p; return c; }