From 188a97cf54d99080fecf3871fd338fde395f044b Mon Sep 17 00:00:00 2001 From: apio Date: Tue, 25 Apr 2023 20:25:51 +0200 Subject: [PATCH] libc: Add execlp --- libc/include/unistd.h | 3 +++ libc/src/unistd.cpp | 30 ++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/libc/include/unistd.h b/libc/include/unistd.h index b3145c73..efa78d3d 100644 --- a/libc/include/unistd.h +++ b/libc/include/unistd.h @@ -64,6 +64,9 @@ extern "C" /* Replace the current process with another one. On success, does not return. */ int execl(const char* path, const char* arg, ...); + /* Replace the current process with another one. On success, does not return. */ + int execlp(const char* path, const char* arg, ...); + /* Replace the current process with another one. On success, does not return. */ int execle(const char* path, const char* arg, ...); diff --git a/libc/src/unistd.cpp b/libc/src/unistd.cpp index d192ec90..e5eef964 100644 --- a/libc/src/unistd.cpp +++ b/libc/src/unistd.cpp @@ -58,7 +58,8 @@ static Result try_execvpe(const char* name, char* const* argv, char* const* return -1; } -static Result try_execl(const char* path, const char* arg, bool use_environ, va_list ap) +static Result try_execl(const char* path, const char* arg, bool use_envp, va_list ap, + int (*exec_function)(const char*, char* const*, char* const*)) { Vector args; TRY(args.try_append(const_cast(arg))); @@ -70,14 +71,14 @@ static Result try_execl(const char* path, const char* arg, bool use_environ if (!str) break; } - if (use_environ) + if (use_envp) { char* const* envp = va_arg(ap, char* const*); - return execve(path, args.data(), envp); + return exec_function(path, args.data(), envp); } - return execv(path, args.data()); + return exec_function(path, args.data(), environ); } extern "C" @@ -186,7 +187,24 @@ extern "C" va_list ap; va_start(ap, arg); - auto rc = try_execl(path, arg, false, ap); + auto rc = try_execl(path, arg, false, ap, execve); + + va_end(ap); + + if (rc.has_error()) + { + errno = rc.error(); + return -1; + } + return rc.value(); + } + + int execlp(const char* path, const char* arg, ...) + { + va_list ap; + va_start(ap, arg); + + auto rc = try_execl(path, arg, false, ap, execvpe); va_end(ap); @@ -203,7 +221,7 @@ extern "C" va_list ap; va_start(ap, arg); - auto rc = try_execl(path, arg, true, ap); + auto rc = try_execl(path, arg, true, ap, execve); va_end(ap);