Skip to content

Commit 0c2face

Browse files
committed
LibC: Add creat(), execvp() resolution, and exec*() environment inheritance.
1 parent fb7c782 commit 0c2face

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

LibC/unistd.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#include <stdlib.h>
1010
#include <sys/ioctl.h>
1111
#include <sys/types.h>
12+
#include <fcntl.h>
1213
#include <Kernel/Syscall.h>
1314
#include <AK/Vector.h>
15+
#include <AK/AKString.h>
1416

1517
extern "C" {
1618

@@ -28,7 +30,7 @@ pid_t fork()
2830

2931
int execv(const char* path, char* const argv[])
3032
{
31-
return execve(path, argv, nullptr);
33+
return execve(path, argv, environ);
3234
}
3335

3436
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[])
3941

4042
int execvp(const char* filename, char* const argv[])
4143
{
42-
// FIXME: This should do some sort of shell-like path resolution!
43-
return execve(filename, argv, nullptr);
44+
int rc = execve(filename, argv, nullptr);
45+
if (rc < 0 && errno != ENOENT) {
46+
printf("execvp failed on first with %s\n", strerror(errno));
47+
return rc;
48+
}
49+
String path = getenv("PATH");
50+
if (path.is_empty())
51+
path = "/bin:/usr/bin";
52+
auto parts = path.split(':');
53+
for (auto& part : parts) {
54+
auto candidate = String::format("%s/%s", part.characters(), filename);
55+
rc = execve(candidate.characters(), argv, environ);
56+
if (rc < 0 && errno != ENOENT) {
57+
printf("execvp failed on attempt (%s) with %s\n", candidate.characters(), strerror(errno));
58+
return rc;
59+
}
60+
}
61+
errno = ENOENT;
62+
return -1;
4463
}
4564

4665
int execl(const char* filename, const char* arg0, ...)
@@ -58,7 +77,7 @@ int execl(const char* filename, const char* arg0, ...)
5877
}
5978
va_end(ap);
6079
args.append(nullptr);
61-
return execve(filename, (char* const *)args.data(), nullptr);
80+
return execve(filename, (char* const *)args.data(), environ);
6281
}
6382

6483
uid_t getuid()
@@ -125,6 +144,11 @@ pid_t getpgrp()
125144
__RETURN_WITH_ERRNO(rc, rc, -1);
126145
}
127146

147+
int creat(const char* path, mode_t mode)
148+
{
149+
return open(path, O_CREAT, mode);
150+
}
151+
128152
int open(const char* path, int options, ...)
129153
{
130154
va_list ap;

LibC/unistd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ int setuid(uid_t);
4848
int setgid(gid_t);
4949
pid_t tcgetpgrp(int fd);
5050
int tcsetpgrp(int fd, pid_t pgid);
51+
int creat(const char* path, mode_t);
5152
int open(const char* path, int options, ...);
5253
ssize_t read(int fd, void* buf, size_t count);
5354
ssize_t write(int fd, const void* buf, size_t count);

0 commit comments

Comments
 (0)