From c2b500909fc82385b38f52c79e46132e88f4ab15 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20P=C3=ADsa=C5=99?= Date: Sat, 13 Nov 2010 21:30:27 +0100 Subject: [PATCH] Make execute_system_command() generic --- src/shigofumi.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/shigofumi.c b/src/shigofumi.c index 8010630..406ea33 100644 --- a/src/shigofumi.c +++ b/src/shigofumi.c @@ -2891,18 +2891,25 @@ static int shi_save_document(int argc, const char **argv) { } -static int execute_system_command(const char *argv0, const char *argv1) { - pid_t pid; +/* Execute program specified as NULL terminated array of arguments. argv[0] is + * subject of PATH search variable look-up. The program is executed directly, + * it's not a shell command. */ +static int execute_system_command(char *const argv[]) { pid_t pid; + + if (!argv || !argv[0]) return -1; + pid = fork(); if (pid == -1) { /* Could not fork */ - fprintf(stderr, _("Could not fork to execute: %s %s\n"), argv0, argv1); + fprintf(stderr, _("Could not fork\n")); return -1; } else if (pid == 0) { /* Child */ - execlp(argv0, argv0, argv1, (char *) NULL); - fprintf(stderr, _("Could not execute: %s %s: %s\n"), argv0, argv1, - strerror(errno)); + execvp(argv[0], argv); + fprintf(stderr, _("Could not execute:")); + for (char *const *arg = argv; *arg; arg++) + fprintf(stderr, " %s", *arg); + fprintf(stderr, _(": %s\n"), strerror(errno)); return -1; } else { /* Wait for the command */ @@ -2941,6 +2948,7 @@ static int shi_open_document(int argc, const char **argv) { const struct isds_document *document; char filename[10] = "shiXXXXXX"; int fd; + char **command = NULL; int retval = 0; if (!argv || !argv[1] || !*argv[1] || argc > 3) { @@ -2968,8 +2976,20 @@ static int shi_open_document(int argc, const char **argv) { /* Open the file with external utility */ if (!retval) { + /* Construct command arguments to execute */ + command = malloc(3 * sizeof(*command)); + if (!command) { + fprintf(stderr, _("Error: Not enough memory\n")); + return -1; + } + command[0] = "xdg-open"; + command[1] = filename; + command[2] = NULL; + /* XXX: Do not use system(3) as we cannot escape uknown shell */ - retval = execute_system_command("xdg-open", filename); + retval = execute_system_command(command); + + free(command); } /* Remove the file */ -- 2.11.4.GIT