diff options
author | dzwdz | 2023-09-03 01:30:53 +0200 |
---|---|---|
committer | dzwdz | 2023-09-03 01:30:53 +0200 |
commit | a492c6ec119bd1151a6f2f7b70875ba173e9c036 (patch) | |
tree | a96f9d4ff4b120ecd58f8f41ab26b5a28fd368c0 /src/libc/unistd/unistd.c | |
parent | fd80c0b227336f4650d6b54d82469feb017aeded (diff) |
libc: split up large .c files, slimming down small binaries a bit
Diffstat (limited to 'src/libc/unistd/unistd.c')
-rw-r--r-- | src/libc/unistd/unistd.c | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/libc/unistd/unistd.c b/src/libc/unistd/unistd.c new file mode 100644 index 0000000..102e72a --- /dev/null +++ b/src/libc/unistd/unistd.c @@ -0,0 +1,85 @@ +#include <bits/panic.h> +#include <camellia.h> +#include <camellia/syscalls.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <elfload.h> + +int unlink(const char *path) { + hid_t h = camellia_open(path, OPEN_WRITE); + if (h < 0) return errno = -h, -1; + long ret = _sys_remove(h); + if (ret < 0) return errno = -ret, -1; + return 0; +} + +int rmdir(const char *path) { + (void)path; + __libc_panic("unimplemented"); +} + +int execv(const char *path, char *const argv[]) { + return execve(path, argv, NULL); +} + +int execvp(const char *path, char *const argv[]) { + // TODO execvp + return execve(path, argv, NULL); +} + +int execvpe(const char *path, char *const argv[], char *const envp[]) { + if (path[0] != '/') { + char *exp = malloc(strlen(path) + 6); + int ret; + strcpy(exp, "/bin/"); + strcat(exp, path); + ret = execve(exp, argv, envp); + free(exp); + return ret; + } + return execve(path, argv, envp); +} + +int execve(const char *path, char *const argv[], char *const envp[]) { + FILE *file = fopen(path, "e"); + char hdr[4] = {0}; + if (!file) + return -1; + + fread(hdr, 1, 4, file); + fseek(file, 0, SEEK_SET); + + if (!memcmp("\x7f""ELF", hdr, 4)) { + elf_execf(file, (void*)argv, (void*)envp); + fclose(file); + } else if (!memcmp("#!", hdr, 2)) { + char buf[256]; + fseek(file, 2, SEEK_SET); + if (fgets(buf, sizeof buf, file)) { + const char *argv [] = {buf, path, NULL}; + char *endl = strchr(buf, '\n'); + if (endl) *endl = '\0'; + execve(buf, (void*)argv, envp); + } + } + + errno = EINVAL; + return -1; +} + +pid_t getpgrp(void) { + __libc_panic("unimplemented"); +} + +int getgroups(int size, gid_t list[]) { + (void)size; (void)list; + __libc_panic("unimplemented"); +} + +int dup(int oldfd) { + (void)oldfd; + __libc_panic("unimplemented"); +} |