LibC: add execl

This commit is contained in:
Bananymous 2023-05-31 22:36:26 +03:00
parent 02f0239016
commit 0acab11620
2 changed files with 33 additions and 5 deletions

View File

@ -147,6 +147,38 @@ long syscall(long syscall, ...)
return ret; return ret;
} }
int execl(const char* pathname, const char* arg0, ...)
{
if (arg0 == nullptr)
{
char* temp = nullptr;
return execv(pathname, &temp);
}
va_list ap;
va_start(ap, arg0);
int argc = 1;
while (va_arg(ap, const char*))
argc++;
va_end(ap);
char** argv = (char**)malloc(sizeof(char*) * (argc + 1));
if (argv == nullptr)
{
errno = ENOMEM;
return -1;
}
va_start(ap, arg0);
argv[0] = (char*)arg0;
for (int i = 1; i < argc; i++)
argv[i] = va_arg(ap, char*);
argv[argc] = nullptr;
va_end(ap);
return execv(pathname, argv);
}
int execv(const char* pathname, char* const argv[]) int execv(const char* pathname, char* const argv[])
{ {
return syscall(SYS_EXEC, pathname, argv, nullptr); return syscall(SYS_EXEC, pathname, argv, nullptr);

View File

@ -12,11 +12,7 @@ int main()
if (fork() == 0) if (fork() == 0)
{ {
char* argv[3]; execl("/usr/bin/cat", "/usr/bin/cat", "/usr/include/kernel/kprint.h", NULL);
argv[0] = (char*)malloc(100); strcpy(argv[0], "/usr/bin/cat");
argv[1] = (char*)malloc(100); strcpy(argv[1], "/usr/include/kernel/kprint.h");
argv[2] = NULL;
execv("/usr/bin/cat", (char**)argv);
ERROR("execl"); ERROR("execl");
return 0; return 0;
} }