1 #include "git-compat-util.h"
2 #include "spawn-pipe.h"
6 /* cmd specifies the command to invoke.
7 * argv specifies its arguments; argv[0] will be replaced by the basename of cmd.
8 * env specifies the environment.
9 * pin and pout specify pipes; the read end of pin is made the standard input
10 * of the spawned process, and the write end of pout is mad the standard output.
11 * The respective unused ends of the pipes are closed both in the parent
12 * process as well as in the child process.
13 * Anyone of pin or pout can be NULL, or any one of the ends can be -1 to
14 * indicate that no processing shall occur.
16 int spawnvpe_pipe(const char *cmd
, const char **argv
, const char **env
,
17 int pin
[], int pout
[])
19 const char *cmd_basename
= strrchr(cmd
, '/');
23 int s0
= -1, s1
= -1, argc
;
25 extern void quote_argv(const char **dst
, const char **src
);
28 cmd_basename
= strrchr(cmd
, '\\');
35 argv
[0] = cmd_basename
;
40 die("unable to fork");
63 if (pin
&& pin
[0] >= 0)
65 if (pout
&& pout
[1] >= 1)
83 for (argc
= 0; argv
[argc
];) argc
++;
84 qargv
= xmalloc((argc
+2)*sizeof(char*));
85 quote_argv(qargv
, argv
);
87 pid
= spawnvpe(_P_NOWAIT
, cmd
, qargv
, env
);
89 free(qargv
); /* TODO: quoted args should be freed, too */
104 const char **copy_environ()
106 return copy_env(environ
);
109 const char **copy_env(const char **env
)
113 for (s
= env
; *s
; s
++)
115 s
= xmalloc(n
*sizeof(const char *));
116 memcpy(s
, env
, n
*sizeof(const char *));
120 void env_unsetenv(const char **env
, const char *name
)
127 for (src
= dst
= 0; env
[src
]; ++src
) {
129 enln
= strlen(env
[src
]);
131 /* might match, and can test for '=' safely */
132 if (0 == strncmp (env
[src
], name
, nmln
)
133 && '=' == env
[src
][nmln
])
134 /* matches, so skip */