aboutsummaryrefslogtreecommitdiff
path: root/core/for.c
diff options
context:
space:
mode:
Diffstat (limited to 'core/for.c')
-rw-r--r--core/for.c45
1 files changed, 45 insertions, 0 deletions
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;
+}