summaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorLukas Fleischer <calcurse@cryptocrack.de>2012-05-16 15:55:40 +0200
committerLukas Fleischer <calcurse@cryptocrack.de>2012-05-16 19:31:47 +0200
commitce13b70c5a0fae3788dc4d551fff24dd5c001649 (patch)
treef5b5960a32a9f5bcc127100daeeba1e3f62bd79e /src/utils.c
parent2951cf832034053d02fee97cc8e4a1f5748578c8 (diff)
downloadcalcurse-ce13b70c5a0fae3788dc4d551fff24dd5c001649.zip
Allow passing additional parameters to shell_exec()
This allows for specifying multiple command line parameters to be passed on to the command in a way similar to fork_exec(). This is useful if we want to wrap editor or pager invocations in a shell. Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c43
1 files changed, 40 insertions, 3 deletions
diff --git a/src/utils.c b/src/utils.c
index d9f3737..36abb30 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1015,10 +1015,47 @@ fork_exec (int *pfdin, int *pfdout, const char *path, const char *const *arg)
/* Execute an external program in a shell. */
int
-shell_exec (int *pfdin, int *pfdout, const char *cmd)
+shell_exec (int *pfdin, int *pfdout, const char *path, const char *const *arg)
{
- const char *arg[] = { "/bin/sh", "-c", cmd, NULL };
- return fork_exec (pfdin, pfdout, *arg, arg);
+ int argc, i;
+ const char **narg;
+ char *arg0 = NULL;
+ int ret;
+
+ for (argc = 0; arg[argc]; argc++)
+ ;
+
+ if (argc < 1)
+ return -1;
+
+ narg = mem_calloc (argc + 4, sizeof (const char *));
+
+ narg[0] = "sh";
+ narg[1] = "-c";
+
+ if (argc > 1)
+ {
+ arg0 = mem_malloc (strlen (path) + 6);
+ sprintf (arg0, "%s \"$@\"", path);
+ narg[2] = arg0;
+
+ for (i = 0; i < argc; i++)
+ narg[i + 3] = arg[i];
+ narg[argc + 3] = NULL;
+ }
+ else
+ {
+ narg[2] = path;
+ narg[3] = NULL;
+ }
+
+ ret = fork_exec (pfdin, pfdout, *narg, narg);
+
+ if (arg0)
+ mem_free (arg0);
+ mem_free (narg);
+
+ return ret;
}
/* Wait for a child process to terminate. */