libc: Add execlp
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-04-25 20:25:51 +02:00
parent 89fd57dea4
commit 188a97cf54
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 27 additions and 6 deletions

View File

@ -64,6 +64,9 @@ extern "C"
/* Replace the current process with another one. On success, does not return. */ /* Replace the current process with another one. On success, does not return. */
int execl(const char* path, const char* arg, ...); 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. */ /* Replace the current process with another one. On success, does not return. */
int execle(const char* path, const char* arg, ...); int execle(const char* path, const char* arg, ...);

View File

@ -58,7 +58,8 @@ static Result<int> try_execvpe(const char* name, char* const* argv, char* const*
return -1; return -1;
} }
static Result<int> try_execl(const char* path, const char* arg, bool use_environ, va_list ap) static Result<int> try_execl(const char* path, const char* arg, bool use_envp, va_list ap,
int (*exec_function)(const char*, char* const*, char* const*))
{ {
Vector<char*> args; Vector<char*> args;
TRY(args.try_append(const_cast<char*>(arg))); TRY(args.try_append(const_cast<char*>(arg)));
@ -70,14 +71,14 @@ static Result<int> try_execl(const char* path, const char* arg, bool use_environ
if (!str) break; if (!str) break;
} }
if (use_environ) if (use_envp)
{ {
char* const* envp = va_arg(ap, char* const*); 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" extern "C"
@ -186,7 +187,24 @@ extern "C"
va_list ap; va_list ap;
va_start(ap, arg); 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); va_end(ap);
@ -203,7 +221,7 @@ extern "C"
va_list ap; va_list ap;
va_start(ap, arg); va_start(ap, arg);
auto rc = try_execl(path, arg, true, ap); auto rc = try_execl(path, arg, true, ap, execve);
va_end(ap); va_end(ap);