libc: Add execvp() and execvpe()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-07 15:39:10 +02:00
parent fe348d56c0
commit 9bb1720cca
Signed by: apio
GPG Key ID: B8A7D06E42258954
3 changed files with 49 additions and 46 deletions

View File

@ -31,50 +31,6 @@ static Result<Vector<char*>> split_command_into_argv(const char* cmd)
return result;
}
static Result<String> join_path(const char* str1, const char* str2)
{
StringBuilder sb;
TRY(sb.add(StringView { str1 }));
TRY(sb.add(StringView { str2 }));
return sb.string();
}
static int execute_in_path(const char* dir, char** argv)
{
String path;
bool ok = join_path(dir, argv[0]).try_move_value_or_error(path, errno);
if (!ok) return -1;
int err = errno;
int status = execv(path.chars(), argv);
if (errno == ENOENT)
{
errno = err;
return 0;
}
return status;
}
static int sh_execvp(char** argv)
{
if (strchr(argv[0], '/'))
{
// Relative paths do not need path resolution.
return execv(argv[0], argv);
}
if (execute_in_path("/bin/", argv) != 0) return -1;
if (execute_in_path("/sbin/", argv) != 0) return -1;
if (execute_in_path("/usr/bin/", argv) != 0) return -1;
if (execute_in_path("/usr/local/bin/", argv) != 0) return -1;
errno = ENOENT;
return -1;
}
int main()
{
while (1)
@ -105,7 +61,7 @@ int main()
}
if (argv[0] == NULL) return 0;
sh_execvp(argv.data());
execvp(argv[0], argv.data());
perror(argv[0]);
return 1;
}

View File

@ -37,7 +37,11 @@ extern "C"
/* Replace the current process with another one. On success, does not return. */
int execve(const char* path, char* const* argv, char* const* envp);
int execvp(const char*, char* const*);
/* Replace the current process with another one. On success, does not return. */
int execvp(const char* name, char* const* argv);
/* Replace the current process with another one. On success, does not return. */
int execvpe(const char* name, char* const* argv, char* const* envp);
/* Call the operating system kernel for a specific service. */
long syscall(long num, ...);

View File

@ -1,9 +1,11 @@
#include <luna/StringBuilder.h>
#include <luna/Vector.h>
#include <bits/errno-return.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <unistd.h>
@ -40,6 +42,47 @@ extern "C"
__errno_return(rc, int);
}
int execvpe(const char* name, char* const* argv, char* const* envp)
{
if (strchr(name, '/')) return execve(name, argv, envp);
char* path = getenv("PATH");
if (!path) path = const_cast<char*>("/bin:/sbin");
Vector<String> paths;
bool ok = StringView { path }.split(":").try_move_value_or_error(paths, errno);
if (!ok) return -1;
for (const auto& dir : paths)
{
// FIXME: Check for errors.
StringBuilder sb;
sb.add(dir);
sb.add('/');
sb.add(StringView { name });
String file;
ok = sb.string().try_move_value_or_error(file, errno);
if (!ok) return -1;
int err = errno;
execve(file.chars(), argv, envp);
if (errno != ENOENT && errno != EACCES) return -1;
// FIXME: POSIX says that if errno == ENOEXEC, we should run /bin/sh <path>.
if (err == EACCES) errno = err;
}
return -1;
}
int execvp(const char* name, char* const* argv)
{
return execvpe(name, argv, environ);
}
int execl(const char* path, const char* arg, ...)
{
va_list ap;