Use proper variable-length vector for spawn args

This commit is contained in:
Lassi Kortela 2019-10-13 22:58:36 +03:00
parent 34be865a86
commit f408a0bbcd
1 changed files with 13 additions and 16 deletions

View File

@ -51,27 +51,24 @@ value_t builtin_spawn(value_t *args, uint32_t nargs)
int fds[2]; int fds[2];
struct pollfd pollfd; struct pollfd pollfd;
ssize_t nbyte; ssize_t nbyte;
value_t exec_args; value_t unix_argv_obj;
char *exec_argv[16]; struct sv_accum unix_argv;
size_t exec_argv_fill = 0;
// TODO: memory leak: unix_argv not freed on type error
argcount("spawn", nargs, 1); argcount("spawn", nargs, 1);
exec_args = args[0]; unix_argv_obj = args[0];
if (exec_args == FL_NIL) { if (unix_argv_obj == FL_NIL) {
lerror(ArgError, "executable argument list is empty"); lerror(ArgError, "executable argument list is empty");
} }
for (;;) { sv_accum_init(&unix_argv);
if (!iscons(exec_args)) { do {
if (!iscons(unix_argv_obj)) {
lerror(ArgError, "executable arguments not a proper list"); lerror(ArgError, "executable arguments not a proper list");
} }
exec_argv[exec_argv_fill++] = sv_accum_strdup(&unix_argv,
tostring(car_(exec_args), "executable argument"); tostring(car_(unix_argv_obj), "executable argument"));
if ((exec_args = cdr_(exec_args)) == FL_NIL) { unix_argv_obj = cdr_(unix_argv_obj);
break; } while (unix_argv_obj != FL_NIL);
}
}
exec_argv[exec_argv_fill++] = 0;
// TODO: Signal mask for the child process. // TODO: Signal mask for the child process.
if (pipe(fds) == -1) { if (pipe(fds) == -1) {
diesys("cannot create pipe"); diesys("cannot create pipe");
@ -84,7 +81,7 @@ value_t builtin_spawn(value_t *args, uint32_t nargs)
if (!child) { if (!child) {
// We are in the new child process. // We are in the new child process.
close(fds[0]); close(fds[0]);
execvp(exec_argv[0], exec_argv); execvp(unix_argv.vec[0], unix_argv.vec);
exec_errno = errno; exec_errno = errno;
nbyte = write(fds[1], &exec_errno, sizeof(exec_errno)); nbyte = write(fds[1], &exec_errno, sizeof(exec_errno));
if (nbyte == (ssize_t)-1) { if (nbyte == (ssize_t)-1) {