diff options
Diffstat (limited to 'src/user/lib')
-rw-r--r-- | src/user/lib/camellia.c | 30 | ||||
-rw-r--r-- | src/user/lib/draw/draw.c | 4 | ||||
-rw-r--r-- | src/user/lib/include/camellia.h | 5 | ||||
-rw-r--r-- | src/user/lib/stdio/file.c | 21 | ||||
-rw-r--r-- | src/user/lib/stdlib.c | 4 | ||||
-rw-r--r-- | src/user/lib/string/strerror.c | 4 | ||||
-rw-r--r-- | src/user/lib/unistd.c | 30 |
7 files changed, 53 insertions, 45 deletions
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; |