summaryrefslogtreecommitdiff
path: root/src/user
diff options
context:
space:
mode:
authordzwdz2022-10-04 13:59:21 +0200
committerdzwdz2022-10-04 13:59:21 +0200
commite83dca9817614d0dc77ce1e5dc13eed44b61eb2f (patch)
tree71bcb994d9cb4199cb002d39c06769f38be1ece2 /src/user
parent710e9b2b5c16f74f66420c66d12455ad518d42c7 (diff)
user/libc: camellia_open, takes cwd into account
Diffstat (limited to 'src/user')
-rw-r--r--src/user/app/drawmouse/drawmouse.c4
-rw-r--r--src/user/app/iochk/iochk.c4
-rw-r--r--src/user/app/netdog/nd.c4
-rw-r--r--src/user/app/netstack/netstack.c4
-rw-r--r--src/user/app/shell/builtins.c4
-rw-r--r--src/user/lib/camellia.c30
-rw-r--r--src/user/lib/draw/draw.c4
-rw-r--r--src/user/lib/include/camellia.h5
-rw-r--r--src/user/lib/stdio/file.c21
-rw-r--r--src/user/lib/stdlib.c4
-rw-r--r--src/user/lib/string/strerror.c4
-rw-r--r--src/user/lib/unistd.c30
12 files changed, 63 insertions, 55 deletions
diff --git a/src/user/app/drawmouse/drawmouse.c b/src/user/app/drawmouse/drawmouse.c
index 6ddfc79..6e2a881 100644
--- a/src/user/app/drawmouse/drawmouse.c
+++ b/src/user/app/drawmouse/drawmouse.c
@@ -1,4 +1,4 @@
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <shared/container/ring.h>
#include <stdbool.h>
@@ -50,7 +50,7 @@ struct packet {
int main(void) {
char buf[64];
- handle_t fd = _syscall_open("/kdev/ps2/mouse", 15, OPEN_READ);
+ handle_t fd = camellia_open("/kdev/ps2/mouse", OPEN_READ);
if (fd < 0) {
eprintf("couldn't open mouse");
return 1;
diff --git a/src/user/app/iochk/iochk.c b/src/user/app/iochk/iochk.c
index 92975be..e4b0346 100644
--- a/src/user/app/iochk/iochk.c
+++ b/src/user/app/iochk/iochk.c
@@ -1,5 +1,5 @@
#include <assert.h>
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <stdbool.h>
#include <stdio.h>
@@ -85,7 +85,7 @@ int main(int argc, char **argv) {
for (; optind < argc; optind++) {
const char *path = argv[optind];
verbosef("checking %s...\n", path);
- handle_t h = _syscall_open(path, strlen(path), OPEN_READ);
+ handle_t h = camellia_open(path, OPEN_READ);
if (h < 0) {
eprintf("couldn't open %s", path);
continue;
diff --git a/src/user/app/netdog/nd.c b/src/user/app/netdog/nd.c
index 8c16bb0..93e1d3a 100644
--- a/src/user/app/netdog/nd.c
+++ b/src/user/app/netdog/nd.c
@@ -1,4 +1,4 @@
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <stdio.h>
#include <string.h>
@@ -35,7 +35,7 @@ int main(int argc, char **argv) {
return 1;
}
- conn = _syscall_open(argv[1], strlen(argv[1]), OPEN_RW);
+ conn = camellia_open(argv[1], OPEN_RW);
if (conn < 0) {
eprintf("couldn't open '%s', err %u", argv[1], -conn);
return -conn;
diff --git a/src/user/app/netstack/netstack.c b/src/user/app/netstack/netstack.c
index c0934fd..852cd37 100644
--- a/src/user/app/netstack/netstack.c
+++ b/src/user/app/netstack/netstack.c
@@ -1,6 +1,6 @@
#include "proto.h"
#include "util.h"
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <stdbool.h>
#include <stddef.h>
@@ -34,7 +34,7 @@ int main(int argc, char **argv) {
eprintf("usage: netstack iface ip gateway");
return 1;
}
- state.raw_h = _syscall_open(argv[1], strlen(argv[1]), OPEN_RW);
+ state.raw_h = camellia_open(argv[1], OPEN_RW);
if (state.raw_h < 0) {
eprintf("couldn't open %s", argv[1]);
return 1;
diff --git a/src/user/app/shell/builtins.c b/src/user/app/shell/builtins.c
index a23e501..6368633 100644
--- a/src/user/app/shell/builtins.c
+++ b/src/user/app/shell/builtins.c
@@ -1,6 +1,6 @@
#include "builtins.h"
#include "shell.h"
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/path.h>
#include <stdbool.h>
#include <stdio.h>
@@ -61,7 +61,7 @@ void cmd_getsize(int argc, char **argv) {
}
for (int i = 1; i < argc; i++) {
- handle_t h = _syscall_open(argv[i], strlen(argv[i]), OPEN_READ);
+ handle_t h = camellia_open(argv[i], OPEN_READ);
if (h < 0) {
eprintf("error opening %s", argv[i]);
continue;
diff --git a/src/user/lib/camellia.c b/src/user/lib/camellia.c
new file mode 100644
index 0000000..1aa8402
--- /dev/null
+++ b/src/user/lib/camellia.c
@@ -0,0 +1,30 @@
+#include <camellia.h>
+#include <camellia/syscalls.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+handle_t camellia_open(const char *path, int flags) {
+ handle_t ret;
+ char *buf;
+ size_t len;
+
+ if (path == NULL)
+ return errno = EINVAL, -EINVAL;
+ if (flags & OPEN_CREATE)
+ flags |= OPEN_WRITE;
+
+ len = absolutepath(NULL, path, 0);
+ buf = malloc(len);
+ if (!buf)
+ return -errno;
+ absolutepath(buf, path, len);
+ ret = _syscall_open(buf, strlen(buf), flags);
+ free(buf);
+
+ if (ret < 0)
+ errno = -ret;
+
+ return ret;
+}
diff --git a/src/user/lib/draw/draw.c b/src/user/lib/draw/draw.c
index 943b8ba..95a8921 100644
--- a/src/user/lib/draw/draw.c
+++ b/src/user/lib/draw/draw.c
@@ -1,4 +1,4 @@
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <errno.h>
#include <stdio.h>
@@ -33,7 +33,7 @@ int fb_setup(struct framebuf *fb, const char *base) {
/* assumes the read went correctly */
fclose(f);
- fb->fd = _syscall_open(path, strlen(path), OPEN_RW);
+ fb->fd = camellia_open(path, OPEN_RW);
if (fb->fd < 0) return fb->fd;
fb->width = strtol(spec, &spec, 0);
diff --git a/src/user/lib/include/camellia.h b/src/user/lib/include/camellia.h
new file mode 100644
index 0000000..f9b9f00
--- /dev/null
+++ b/src/user/lib/include/camellia.h
@@ -0,0 +1,5 @@
+#pragma once
+#include <camellia/flags.h>
+#include <camellia/types.h>
+
+handle_t camellia_open(const char *path, int flags);
diff --git a/src/user/lib/stdio/file.c b/src/user/lib/stdio/file.c
index f3120d7..531d44b 100644
--- a/src/user/lib/stdio/file.c
+++ b/src/user/lib/stdio/file.c
@@ -1,5 +1,5 @@
#include "file.h"
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <errno.h>
#include <stdio.h>
@@ -20,7 +20,6 @@ FILE *fopen(const char *path, const char *mode) {
FILE *f;
handle_t h;
int flags = 0;
- char *tmppath = NULL;
if (!path) {
errno = 1;
return NULL;
@@ -30,18 +29,10 @@ FILE *fopen(const char *path, const char *mode) {
if (!strcmp(path, "stdin")) return file_clone(stdin, mode);
if (!strcmp(path, "stdout")) return file_clone(stdout, mode);
if (!strcmp(path, "stderr")) return file_clone(stderr, mode);
- errno = 1;
+ errno = ENOENT;
return NULL;
}
- if (path && path[0] != '/') {
- size_t len = absolutepath(NULL, path, 0);
- tmppath = malloc(len);
- if (!tmppath) return NULL;
- absolutepath(tmppath, path, len);
- path = tmppath;
- }
-
if (strchr(mode, 'e')) {
/* camellia extension: open as executable */
flags |= OPEN_EXEC;
@@ -53,12 +44,8 @@ FILE *fopen(const char *path, const char *mode) {
flags |= OPEN_WRITE | OPEN_CREATE;
}
- h = _syscall_open(path, strlen(path), flags);
- if (tmppath) free(tmppath);
- if (h < 0) {
- errno = -h;
- return NULL;
- }
+ h = camellia_open(path, flags);
+ if (h < 0) return NULL;
if (mode[0] == 'w')
_syscall_write(h, NULL, 0, 0, WRITE_TRUNCATE);
diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c
index c1ee217..ffa9f15 100644
--- a/src/user/lib/stdlib.c
+++ b/src/user/lib/stdlib.c
@@ -1,4 +1,4 @@
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/syscalls.h>
#include <errno.h>
#include <string.h>
@@ -10,7 +10,7 @@ _Noreturn void abort(void) {
int mkstemp(char *template) {
// TODO randomize template
- handle_t h = _syscall_open(template, strlen(template), OPEN_CREATE | OPEN_RW);
+ handle_t h = camellia_open(template, OPEN_CREATE | OPEN_RW);
if (h < 0) {
errno = -h;
return -1;
diff --git a/src/user/lib/string/strerror.c b/src/user/lib/string/strerror.c
index 1f5fc29..c838299 100644
--- a/src/user/lib/string/strerror.c
+++ b/src/user/lib/string/strerror.c
@@ -7,5 +7,7 @@ static const char *errstr[] = {
};
char *strerror(int n) {
- return (char*)(errstr[n] ? errstr[n] : "unknown error");
+ if (0 <= n && n * sizeof(*errstr) < sizeof(errstr) && errstr[n])
+ return (char*)errstr[n];
+ return "unknown error";
}
diff --git a/src/user/lib/unistd.c b/src/user/lib/unistd.c
index 04a060d..01aa94f 100644
--- a/src/user/lib/unistd.c
+++ b/src/user/lib/unistd.c
@@ -1,4 +1,4 @@
-#include <camellia/flags.h>
+#include <camellia.h>
#include <camellia/path.h>
#include <camellia/syscalls.h>
#include <errno.h>
@@ -24,24 +24,11 @@ _Noreturn void exit(int c) {
_Noreturn void _exit(int c) { exit(c); };
int unlink(const char *path) {
- size_t len = strlen(path) + 1;
- char *abspath = malloc(len);
- if (!abspath) return -1;
-
- size_t abslen = absolutepath(abspath, path, len);
- if (abslen == 0) { errno = EINVAL; goto err; }
-
- // TODO take cwd into account
- handle_t h = _syscall_open(abspath, abslen - 1, OPEN_WRITE);
- if (h < 0) { errno = -h; goto err; }
-
+ handle_t h = camellia_open(path, OPEN_WRITE);
+ if (h < 0) return errno = -h, -1;
long ret = _syscall_remove(h);
- if (ret < 0) { errno = -ret; goto err; }
-
+ if (ret < 0) return errno = -ret, -1;
return 0;
-err:
- free(abspath);
- return -1;
}
// TODO isatty
@@ -117,12 +104,9 @@ int chdir(const char *path) {
}
/* check if exists */
- h = _syscall_open(cwd2, strlen(cwd2), OPEN_READ);
- if (h < 0) {
- errno = ENOENT;
- return -1;
- }
- _syscall_close(h);
+ h = camellia_open(cwd2, OPEN_READ);
+ if (h < 0) return errno = ENOENT, -1;
+ close(h);
tmp = cwd;
cwd = cwd2;