From 6f7a5b7e289a9877b30ffc3ad101f7912757e0e3 Mon Sep 17 00:00:00 2001 From: jilles Date: Tue, 19 Jan 2016 22:41:26 +0000 Subject: sh: Simplify some code related to positional parameters. --- bin/sh/options.c | 29 +++++++++++++---------------- bin/sh/options.h | 1 - 2 files changed, 13 insertions(+), 17 deletions(-) (limited to 'bin') diff --git a/bin/sh/options.c b/bin/sh/options.c index 606ccfb..70a09c3 100644 --- a/bin/sh/options.c +++ b/bin/sh/options.c @@ -74,6 +74,7 @@ static void options(int); static void minus_o(char *, int); static void setoption(int, int); static void setoptionbyindex(int, int); +static void setparam(int, char **); static int getopts(char *, char *, char **, char ***, char **); @@ -224,7 +225,7 @@ end_options1: end_options2: if (!cmdline) { if (*argptr == NULL) - setparam(argptr); + setparam(0, argptr); return; } @@ -318,22 +319,20 @@ setoption(int flag, int val) * Set the shell parameters. */ -void -setparam(char **argv) +static void +setparam(int argc, char **argv) { char **newparam; char **ap; - int nparam; - for (nparam = 0 ; argv[nparam] ; nparam++); - ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); + ap = newparam = ckmalloc((argc + 1) * sizeof *ap); while (*argv) { *ap++ = savestr(*argv++); } *ap = NULL; freeparam(&shellparam); shellparam.malloc = 1; - shellparam.nparam = nparam; + shellparam.nparam = argc; shellparam.p = newparam; shellparam.optp = NULL; shellparam.reset = 1; @@ -371,8 +370,7 @@ freeparam(struct shparam *param) int shiftcmd(int argc, char **argv) { - int n; - char **ap1, **ap2; + int i, n; n = 1; if (argc > 1) @@ -381,12 +379,11 @@ shiftcmd(int argc, char **argv) return 1; INTOFF; shellparam.nparam -= n; - for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { - if (shellparam.malloc) - ckfree(*ap1); - } - ap2 = shellparam.p; - while ((*ap2++ = *ap1++) != NULL); + if (shellparam.malloc) + for (i = 0; i < n; i++) + ckfree(shellparam.p[i]); + memmove(shellparam.p, shellparam.p + n, + (shellparam.nparam + 1) * sizeof(shellparam.p[0])); shellparam.reset = 1; INTON; return 0; @@ -407,7 +404,7 @@ setcmd(int argc, char **argv) options(0); optschanged(); if (*argptr != NULL) { - setparam(argptr); + setparam(argc - (argptr - argv), argptr); } INTON; return 0; diff --git a/bin/sh/options.h b/bin/sh/options.h index d997e6f..8c83acf 100644 --- a/bin/sh/options.h +++ b/bin/sh/options.h @@ -108,7 +108,6 @@ extern char *nextopt_optptr; /* used by nextopt */ void procargs(int, char **); void optschanged(void); -void setparam(char **); void freeparam(struct shparam *); int nextopt(const char *); void getoptsreset(const char *); -- cgit v1.1