From 00d4b0f7cf6742a66f2bac262d5a3b3a9d0cc275 Mon Sep 17 00:00:00 2001 From: Bert Date: Thu, 3 Mar 2011 10:52:16 +0100 Subject: Use "/bin/sh -c" for external commands --- main.c | 72 ++++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 35 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e3a1fa2..d736d12 100644 --- a/main.c +++ b/main.c @@ -273,57 +273,59 @@ void read_dir_rec(const char *dirname) { free(dirnames); } -int run_command(const char **cmdline, Bool reload) { - int argc, i; - const char **argv; +int run_command(const char *cline, Bool reload) { + int fncnt, fnlen; + char *cn, *cmdline; + const char *co, *fname; pid_t pid; - int error, ret, status; + int ret, status; - if (!cmdline) + if (!cline || !*cline) return 0; - argc = 1; - while (cmdline[argc-1]) - ++argc; + fncnt = 0; + co = cline - 1; + while ((co = strchr(co + 1, '#'))) + ++fncnt; - if (argc < 2) + if (!fncnt) return 0; - argv = (const char**) s_malloc(argc * sizeof(const char*)); - error = ret = 0; - - for (i = 0; i < argc; ++i) { - if (cmdline[i] != FILENAME) - argv[i] = cmdline[i]; - else - argv[i] = filenames[mode == MODE_NORMAL ? fileidx : tns.sel]; + ret = 0; + fname = filenames[mode == MODE_NORMAL ? fileidx : tns.sel]; + fnlen = strlen(fname); + cn = cmdline = (char*) s_malloc((strlen(cline) + fncnt * (fnlen + 2)) * + sizeof(char)); + + /* replace all '#' with filename */ + for (co = cline; *co; ++co) { + if (*co == '#') { + *cn++ = '"'; + strcpy(cn, fname); + cn += fnlen; + *cn++ = '"'; + } else { + *cn++ = *co; + } } + *cn = '\0'; if ((pid = fork()) == 0) { - execvp(argv[0], (char **const) argv); - warn("could not exec %s", argv[0]); + execlp("/bin/sh", "/bin/sh", "-c", cmdline, NULL); + warn("could not exec: /bin/sh"); exit(1); - } else if (pid < 0 && !options->quiet) { - warn("could not fork. command line was:"); - error = 1; + } else if (pid < 0) { + warn("could not fork. command line was: %s", cmdline); } else if (reload) { waitpid(pid, &status, 0); - if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { + if (WIFEXITED(status) && WEXITSTATUS(status) == 0) ret = 1; - } else if (!options->quiet) { - warn("child exited with non-zero return value: %d. command line was:", - WEXITSTATUS(status)); - error = 1; - } + else + warn("child exited with non-zero return value: %d. command line was: %s", + WEXITSTATUS(status), cmdline); } - if (error) { - for (i = 0; i < argc && argv[i]; ++i) - fprintf(stderr, "%s%s", i > 0 ? " " : "", argv[i]); - fprintf(stderr, "\n"); - } - - free(argv); + free(cmdline); return ret; } -- cgit v1.2.3