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>
This commit is contained in:
Lukas Fleischer 2012-05-16 15:55:40 +02:00
parent 2951cf8320
commit ce13b70c5a
4 changed files with 45 additions and 6 deletions

View File

@ -930,7 +930,7 @@ void str_toupper (char *);
void file_close (FILE *, const char *);
void psleep (unsigned);
int fork_exec (int *, int *, const char *, const char *const *);
int shell_exec (int *, int *, const char *);
int shell_exec (int *, int *, const char *, const char *const *);
int child_wait (int *, int *, int);
void press_any_key (void);
void print_apoint (const char *, long, struct apoint *);

View File

@ -1180,6 +1180,7 @@ void
day_pipe_item (void)
{
char cmd[BUFSIZ] = "";
char const *arg[] = { cmd, NULL };
int pout;
int pid;
FILE *fpout;
@ -1196,7 +1197,7 @@ day_pipe_item (void)
return;
wins_prepare_external ();
if ((pid = shell_exec (NULL, &pout, cmd)))
if ((pid = shell_exec (NULL, &pout, *arg, arg)))
{
fpout = fdopen (pout, "w");

View File

@ -469,6 +469,7 @@ void
todo_pipe_item (void)
{
char cmd[BUFSIZ] = "";
char const *arg[] = { cmd, NULL };
int pout;
int pid;
FILE *fpout;
@ -479,7 +480,7 @@ todo_pipe_item (void)
return;
wins_prepare_external ();
if ((pid = shell_exec (NULL, &pout, cmd)))
if ((pid = shell_exec (NULL, &pout, *arg, arg)))
{
fpout = fdopen (pout, "w");

View File

@ -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. */