mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
LibC: Add creat(), execvp() resolution, and exec*() environment inheritance.
This commit is contained in:
parent
fb7c7829c2
commit
0c2face7b0
2 changed files with 29 additions and 4 deletions
|
@ -9,8 +9,10 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <Kernel/Syscall.h>
|
#include <Kernel/Syscall.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <AK/AKString.h>
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
|
@ -28,7 +30,7 @@ pid_t fork()
|
||||||
|
|
||||||
int execv(const char* path, char* const argv[])
|
int execv(const char* path, char* const argv[])
|
||||||
{
|
{
|
||||||
return execve(path, argv, nullptr);
|
return execve(path, argv, environ);
|
||||||
}
|
}
|
||||||
|
|
||||||
int execve(const char* filename, char* const argv[], char* const envp[])
|
int execve(const char* filename, char* const argv[], char* const envp[])
|
||||||
|
@ -39,8 +41,25 @@ int execve(const char* filename, char* const argv[], char* const envp[])
|
||||||
|
|
||||||
int execvp(const char* filename, char* const argv[])
|
int execvp(const char* filename, char* const argv[])
|
||||||
{
|
{
|
||||||
// FIXME: This should do some sort of shell-like path resolution!
|
int rc = execve(filename, argv, nullptr);
|
||||||
return execve(filename, argv, nullptr);
|
if (rc < 0 && errno != ENOENT) {
|
||||||
|
printf("execvp failed on first with %s\n", strerror(errno));
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
String path = getenv("PATH");
|
||||||
|
if (path.is_empty())
|
||||||
|
path = "/bin:/usr/bin";
|
||||||
|
auto parts = path.split(':');
|
||||||
|
for (auto& part : parts) {
|
||||||
|
auto candidate = String::format("%s/%s", part.characters(), filename);
|
||||||
|
rc = execve(candidate.characters(), argv, environ);
|
||||||
|
if (rc < 0 && errno != ENOENT) {
|
||||||
|
printf("execvp failed on attempt (%s) with %s\n", candidate.characters(), strerror(errno));
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errno = ENOENT;
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int execl(const char* filename, const char* arg0, ...)
|
int execl(const char* filename, const char* arg0, ...)
|
||||||
|
@ -58,7 +77,7 @@ int execl(const char* filename, const char* arg0, ...)
|
||||||
}
|
}
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
args.append(nullptr);
|
args.append(nullptr);
|
||||||
return execve(filename, (char* const *)args.data(), nullptr);
|
return execve(filename, (char* const *)args.data(), environ);
|
||||||
}
|
}
|
||||||
|
|
||||||
uid_t getuid()
|
uid_t getuid()
|
||||||
|
@ -125,6 +144,11 @@ pid_t getpgrp()
|
||||||
__RETURN_WITH_ERRNO(rc, rc, -1);
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int creat(const char* path, mode_t mode)
|
||||||
|
{
|
||||||
|
return open(path, O_CREAT, mode);
|
||||||
|
}
|
||||||
|
|
||||||
int open(const char* path, int options, ...)
|
int open(const char* path, int options, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
|
|
@ -48,6 +48,7 @@ int setuid(uid_t);
|
||||||
int setgid(gid_t);
|
int setgid(gid_t);
|
||||||
pid_t tcgetpgrp(int fd);
|
pid_t tcgetpgrp(int fd);
|
||||||
int tcsetpgrp(int fd, pid_t pgid);
|
int tcsetpgrp(int fd, pid_t pgid);
|
||||||
|
int creat(const char* path, mode_t);
|
||||||
int open(const char* path, int options, ...);
|
int open(const char* path, int options, ...);
|
||||||
ssize_t read(int fd, void* buf, size_t count);
|
ssize_t read(int fd, void* buf, size_t count);
|
||||||
ssize_t write(int fd, const void* buf, size_t count);
|
ssize_t write(int fd, const void* buf, size_t count);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue