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 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 core/for.c (limited to 'core/for.c') 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; +} -- cgit v1.2.3-70-g09d2