From 7a98e998c5cda484611d19854649ab8535f503d4 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Tue, 14 Feb 2023 17:09:59 +0100 Subject: add simplet init system --- core/for.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ core/init | 5 ----- core/mount.c | 2 +- core/unmount.c | 12 ++++++++++++ 4 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 core/for.c delete mode 100755 core/init create mode 100644 core/unmount.c (limited to 'core') diff --git a/core/for.c b/core/for.c new file mode 100644 index 0000000..d576bf8 --- /dev/null +++ b/core/for.c @@ -0,0 +1,45 @@ +#include "../lib/exec/exec.h" +#include "../lib/sys/fork.h" +#include "../lib/sys/wait4.h" +#include "../lib/io/io.h" +#include "../lib/sys/types.h" +#include "../lib/cstr/cstr.h" + +char line[BUFSIZ]; + + +void replace_args(char **argv, char *rep) +{ + for (; *argv; ++argv) { + if (cstr_compare(*argv, "%") == 0) { + *argv = rep; + } + } +} + + +int main(int argc, char **argv) +{ + if (argc <= 1) { + wff(STDERR_FD, "for command [args...] % [args...]") + return -1; + } + + i64 size; + int pid; + + while ((size = get_next_line_from_fd(STDIN_FD, line)) >= 0) { + + pid = fork(); + if (pid == 0) { + replace_args(argv + 1, line); + exec((const char**)(argv + 1)); + wff(STDERR_FD, "command not found: %s", argv[1]); + return -1; + } + + wait4(pid, 0, 0); + } + + return 0; +} diff --git a/core/init b/core/init deleted file mode 100755 index 5802b36..0000000 --- a/core/init +++ /dev/null @@ -1,5 +0,0 @@ -#!/system/core/smash -/system/core/clear -/system/core/read /system/config/banner | /system/core/mdv -/system/core/env -f /system/config/env /system/core/smash -/system/core/powerctl poweroff diff --git a/core/mount.c b/core/mount.c index a7a4527..3bbf52b 100644 --- a/core/mount.c +++ b/core/mount.c @@ -12,7 +12,7 @@ int main(int argc, char **argv) int err; if ((err = mount(argv[2], argv[3], argv[1], 0, 0))) { - wff(STDERR_FD, "error: %s", errstr[-err]); + wff(STDERR_FD, "error: %s\n", errstr[-err]); return err; } diff --git a/core/unmount.c b/core/unmount.c new file mode 100644 index 0000000..81103d9 --- /dev/null +++ b/core/unmount.c @@ -0,0 +1,12 @@ +#include "../lib/io/io.h" +#include "../lib/sys/unmount.h" + +int main(int argc, char **argv) +{ + if (argc != 2) { + wff(STDERR_FD, "unmount \n"); + return -1; + } + + return unmount(argv[1], 9); +} -- cgit v1.2.3-70-g09d2