summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordzwdz2022-07-26 23:04:49 +0200
committerdzwdz2022-07-26 23:04:49 +0200
commit57152f6a03d857118fa82e0a28657d869f4c7110 (patch)
tree3b3143810a65cf5e5b1d1b9612aee157e33076a2
parentd7767be24bdcc0101c2ba08c03600b9f3c608663 (diff)
user/libc: execv()
-rw-r--r--src/shared/include/camellia/errno.h1
-rw-r--r--src/user/app/init/shell.c13
-rw-r--r--src/user/lib/include/unistd.h2
-rw-r--r--src/user/lib/stdlib.c16
4 files changed, 25 insertions, 7 deletions
diff --git a/src/shared/include/camellia/errno.h b/src/shared/include/camellia/errno.h
index a269fa3..0c3afb3 100644
--- a/src/shared/include/camellia/errno.h
+++ b/src/shared/include/camellia/errno.h
@@ -2,3 +2,4 @@
#define EFAULT 2
#define EBADF 3 /* Invalid file descriptor. */
+#define EINVAL 4
diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c
index 048b713..a6b9965 100644
--- a/src/user/app/init/shell.c
+++ b/src/user/app/init/shell.c
@@ -1,12 +1,12 @@
#include "shell.h"
#include "tests/tests.h"
#include <camellia/syscalls.h>
+#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <user/lib/elfload.h>
static bool isspace(char c) {
return c == ' ' || c == '\t' || c == '\n';
@@ -202,14 +202,13 @@ void shell_loop(void) {
memcpy(binname + 5, cmd, cmdlen + 1);
}
- FILE *file = fopen(binname, "r");
- if (!file) {
- printf("unknown command: %s\n", cmd);
+ execv(binname, NULL);
+ if (errno == EINVAL) {
+ printf("%s isn't a valid executable\n", cmd);
} else {
- elf_execf(file);
- fclose(file);
- printf("couldn't execute %s\n", binname);
+ printf("unknown command: %s\n", cmd);
}
+
if (binname != cmd)
free(binname);
}
diff --git a/src/user/lib/include/unistd.h b/src/user/lib/include/unistd.h
index b825e45..4f367ca 100644
--- a/src/user/lib/include/unistd.h
+++ b/src/user/lib/include/unistd.h
@@ -4,3 +4,5 @@
int fork(void);
int close(handle_t h);
_Noreturn void exit(int);
+
+int execv(const char *path, char *const argv[]);
diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c
index 068635b..179bd8f 100644
--- a/src/user/lib/stdlib.c
+++ b/src/user/lib/stdlib.c
@@ -1,5 +1,8 @@
#include <camellia/syscalls.h>
+#include <errno.h>
+#include <stdio.h>
#include <unistd.h>
+#include <user/lib/elfload.h>
int errno = 0;
@@ -14,3 +17,16 @@ int close(handle_t h) {
_Noreturn void exit(int c) {
_syscall_exit(c);
}
+
+int execv(const char *path, char *const argv[]) {
+ (void)argv;
+
+ FILE *file = fopen(path, "r");
+ if (!file)
+ return -1;
+
+ elf_execf(file);
+ fclose(file);
+ errno = EINVAL;
+ return -1;
+}