LibC: Implement basic execvp

This commit is contained in:
Bananymous 2023-12-06 18:14:00 +02:00
parent 76049b2e13
commit 0dc168a8c0
1 changed files with 42 additions and 0 deletions

View File

@ -180,6 +180,48 @@ int execve(const char* pathname, char* const argv[], char* const envp[])
return syscall(SYS_EXEC, pathname, argv, envp);
}
int execvp(const char* file, char* const argv[])
{
char buffer[1024];
const char* pathname = file;
// do path resolution if file doesn't contain /
if (strchr(file, '/') == nullptr)
{
const char* cur = getenv("PATH");
if (!cur)
{
errno = ENOENT;
return -1;
}
while (*cur)
{
const char* end = strchrnul(cur, ':');
size_t len = end - cur;
ASSERT(strlen(file) + 1 + len < sizeof(buffer));
strncpy(buffer, cur, len);
strcat(buffer, "/");
strcat(buffer, file);
struct stat st;
if (stat(buffer, &st) == 0)
{
pathname = buffer;
break;
}
cur = end;
if (*cur)
cur++;
}
}
return execve(pathname, argv, environ);
}
pid_t fork(void)
{
return syscall(SYS_FORK);