Reduce memory requirements for execve()

This commit is contained in:
Justine Tunney 2021-01-29 22:00:10 -08:00
parent eaca5b3e81
commit 21e1023d28
7 changed files with 35 additions and 32 deletions

View file

@ -25,15 +25,16 @@
#include "libc/str/str.h"
int execve$sysv(const char *prog, char *const argv[], char *const envp[]) {
size_t i, n;
char **shargs, bash[PATH_MAX];
size_t i;
char **shargs;
if (__execve$sysv(prog, argv, envp) != -1) return 0;
if (errno != ENOEXEC) return -1;
for (i = 0; argv[i];) ++i;
shargs = alloca((i + 2) * sizeof(char *));
memcpy(shargs + 2, argv + 1, i * sizeof(char *));
shargs[0] = !IsFreebsd() ? _PATH_BSHELL
: firstnonnull(commandv("bash", bash), _PATH_BSHELL);
: firstnonnull(commandv("bash", alloca(PATH_MAX)),
_PATH_BSHELL);
shargs[1] = prog;
return __execve$sysv(shargs[0], shargs, envp);
}