From a492c6ec119bd1151a6f2f7b70875ba173e9c036 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sun, 3 Sep 2023 01:30:53 +0200 Subject: libc: split up large .c files, slimming down small binaries a bit --- src/libc/unistd/unistd.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/libc/unistd/unistd.c (limited to 'src/libc/unistd/unistd.c') 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 +#include +#include +#include +#include +#include +#include +#include +#include + +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"); +} -- cgit v1.2.3