[PATCH 3/3] ash: exec: -a option for setting zeroth arg
Kaarle Ritvanen
kaarle.ritvanen at datakunkku.fi
Tue Apr 11 21:58:48 UTC 2017
Signed-off-by: Kaarle Ritvanen <kaarle.ritvanen at datakunkku.fi>
---
shell/ash.c | 42 ++++++++++++++++++++++++++++++++----------
1 file changed, 32 insertions(+), 10 deletions(-)
diff --git a/shell/ash.c b/shell/ash.c
index 58ae950..b799b85 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -7744,9 +7744,9 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) char *cmd, char **argv, char **
* have to change the find_command routine as well.
* argv[-1] must exist and be writable! See tryexec() for why.
*/
-static void shellexec(char **, const char *, int) NORETURN;
+static void shellexec(const char *, char **, const char *, int) NORETURN;
static void
-shellexec(char **argv, const char *path, int idx)
+shellexec(const char *cmdname, char **argv, const char *path, int idx)
{
char *cmdpath;
int e;
@@ -7755,12 +7755,15 @@ shellexec(char **argv, const char *path, int idx)
int applet_no = -1; /* used only by FEATURE_SH_STANDALONE */
envp = listvars(VEXPORT, VUNSET, /*end:*/ NULL);
- if (strchr(argv[0], '/') != NULL
+ if (!cmdname)
+ cmdname = argv[0];
+
+ if (strchr(cmdname, '/') != NULL
#if ENABLE_FEATURE_SH_STANDALONE
- || (applet_no = find_applet_by_name(argv[0])) >= 0
+ || (applet_no = find_applet_by_name(cmdname)) >= 0
#endif
) {
- tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) argv[0], argv, envp);
+ tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) cmdname, argv, envp);
if (applet_no >= 0) {
/* We tried execing ourself, but it didn't work.
* Maybe /proc/self/exe doesn't exist?
@@ -7772,7 +7775,7 @@ shellexec(char **argv, const char *path, int idx)
} else {
try_PATH:
e = ENOENT;
- while ((cmdpath = path_advance(&path, argv[0])) != NULL) {
+ while ((cmdpath = path_advance(&path, cmdname)) != NULL) {
if (--idx < 0 && pathopt == NULL) {
tryexec(IF_FEATURE_SH_STANDALONE(-1,) cmdpath, argv, envp);
if (errno != ENOENT && errno != ENOTDIR)
@@ -9353,9 +9356,28 @@ truecmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
}
static int FAST_FUNC
-execcmd(int argc UNUSED_PARAM, char **argv)
+execcmd(int argc, char **argv)
{
- if (argv[1]) {
+ int opt;
+ char *argv0 = NULL;
+ char *cmdname = NULL;
+
+ GETOPT_RESET
+ while ((opt = getopt(argc, argv, "a:")) != -1)
+ switch (opt) {
+ case 'a':
+ argv0 = optarg;
+ break;
+ default:
+ return 1;
+ }
+
+ if (argv[optind]) {
+ if (argv0) {
+ cmdname = argv[optind];
+ argv[optind] = argv0;
+ }
+
iflag = 0; /* exit on error */
mflag = 0;
optschanged();
@@ -9371,7 +9393,7 @@ execcmd(int argc UNUSED_PARAM, char **argv)
/*setsignal(SIGTSTP); - unnecessary because of mflag=0 */
/*setsignal(SIGTTOU); - unnecessary because of mflag=0 */
- shellexec(argv + 1, pathval(), 0);
+ shellexec(cmdname, argv + optind, pathval(), 0);
/* NOTREACHED */
}
return 0;
@@ -9773,7 +9795,7 @@ evalcommand(union node *cmd, int flags)
/* fall through to exec'ing external program */
}
listsetvar(varlist.list, VEXPORT|VSTACK);
- shellexec(argv, path, cmdentry.u.index);
+ shellexec(NULL, argv, path, cmdentry.u.index);
/* NOTREACHED */
} /* default */
case CMDBUILTIN:
--
2.9.3
More information about the busybox
mailing list