]> git.ozlabs.org Git - ccan/commitdiff
pipecmd: add pipecmdarr variant.
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 20 Nov 2015 06:29:09 +0000 (16:59 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 20 Nov 2015 06:35:47 +0000 (17:05 +1030)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ccan/pipecmd/pipecmd.c
ccan/pipecmd/pipecmd.h

index 352677fac1c216240eb38252216f72584793221c..671d39198089b424f00e3f5041a7d79291ec55f8 100644 (file)
@@ -24,6 +24,20 @@ static char **gather_args(const char *arg0, va_list ap)
 }
 
 pid_t pipecmdv(int *fd_fromchild, int *fd_tochild, const char *cmd, va_list ap)
 }
 
 pid_t pipecmdv(int *fd_fromchild, int *fd_tochild, const char *cmd, va_list ap)
+{
+       char **arr = gather_args(cmd, ap);
+       pid_t ret;
+
+       if (!arr) {
+               errno = ENOMEM;
+               return -1;
+       }
+       ret = pipecmdarr(fd_fromchild, fd_tochild, arr);
+       free_noerr(arr);
+       return ret;
+}
+
+pid_t pipecmdarr(int *fd_fromchild, int *fd_tochild, char *const *arr)
 {
        int tochild[2], fromchild[2], execfail[2];
        pid_t childpid;
 {
        int tochild[2], fromchild[2], execfail[2];
        pid_t childpid;
@@ -57,8 +71,6 @@ pid_t pipecmdv(int *fd_fromchild, int *fd_tochild, const char *cmd, va_list ap)
                goto close_execfail_fail;
 
        if (childpid == 0) {
                goto close_execfail_fail;
 
        if (childpid == 0) {
-               char **args = gather_args(cmd, ap);
-
                if (fd_tochild)
                        close(tochild[1]);
                if (fd_fromchild)
                if (fd_tochild)
                        close(tochild[1]);
                if (fd_fromchild)
@@ -66,23 +78,20 @@ pid_t pipecmdv(int *fd_fromchild, int *fd_tochild, const char *cmd, va_list ap)
                close(execfail[0]);
 
                // Child runs command.
                close(execfail[0]);
 
                // Child runs command.
-               if (!args)
-                       err = ENOMEM;
-               else {
-                       if (tochild[0] != STDIN_FILENO) {
-                               if (dup2(tochild[0], STDIN_FILENO) == -1)
-                                       goto child_errno_fail;
-                               close(tochild[0]);
-                       }
-                       if (fromchild[1] != STDOUT_FILENO) {
-                               if (dup2(fromchild[1], STDOUT_FILENO) == -1)
-                                       goto child_errno_fail;
-                               close(fromchild[1]);
-                       }
-                       execvp(cmd, args);
-               child_errno_fail:
-                       err = errno;
+               if (tochild[0] != STDIN_FILENO) {
+                       if (dup2(tochild[0], STDIN_FILENO) == -1)
+                               goto child_errno_fail;
+                       close(tochild[0]);
                }
                }
+               if (fromchild[1] != STDOUT_FILENO) {
+                       if (dup2(fromchild[1], STDOUT_FILENO) == -1)
+                               goto child_errno_fail;
+                       close(fromchild[1]);
+               }
+               execvp(arr[0], arr);
+
+       child_errno_fail:
+               err = errno;
                write(execfail[1], &err, sizeof(err));
                exit(127);
        }
                write(execfail[1], &err, sizeof(err));
                exit(127);
        }
index 3adfb541370b18e761e56333876783abd51b0a0c..87e3dcf8193ddf17985f0c33d372be34928566f6 100644 (file)
@@ -27,4 +27,12 @@ pid_t pipecmd(int *infd, int *outfd, const char *cmd, ...);
  * @ap: argument list for arguments.
  */
 pid_t pipecmdv(int *infd, int *outfd, const char *cmd, va_list ap);
  * @ap: argument list for arguments.
  */
 pid_t pipecmdv(int *infd, int *outfd, const char *cmd, va_list ap);
+
+/**
+ * pipecmdarr - run a command, optionally connect pipes (char arry version)
+ * @infd: input fd to write to child (if non-NULL)
+ * @outfd: output fd to read from child (if non-NULL)
+ * @arr: NULL-terminated array for arguments (first is program to run).
+ */
+pid_t pipecmdarr(int *infd, int *outfd, char *const *arr);
 #endif /* CCAN_PIPECMD_H */
 #endif /* CCAN_PIPECMD_H */