summary refs log tree commit diff
path: root/progs/shel
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2025-02-14 20:04:08 +0200
committerWlodekM <[email protected]>2025-02-14 20:04:08 +0200
commit50dd5f54d89c0902fdb8143c4ed91ea613f34b18 (patch)
tree4db84c2e61f286874f18d1d34c8bff9fcf772be2 /progs/shel
parent4f6f9b3af331946c233ca4be471c9e0a424bc17f (diff)
finish basic smolshel functionality
Diffstat (limited to 'progs/shel')
-rw-r--r--progs/shel/shel.c30
1 files changed, 27 insertions, 3 deletions
diff --git a/progs/shel/shel.c b/progs/shel/shel.c
index e3221f4..9eca8c5 100644
--- a/progs/shel/shel.c
+++ b/progs/shel/shel.c
@@ -5,6 +5,14 @@
 #include <stdbool.h>
 #include <string.h>
 
+#define P_ALL 0
+#define P_PID 1
+#define P_PGID 2
+#define P_PIDFD 3
+#if !WEXITED
+#define WEXITED		0x00000004
+#endif
+
 void parseInput(char *input, char *command, char **argv) {
     bool reachedArgv = false;
     char arg[32] = {0};
@@ -42,6 +50,9 @@ void parseInput(char *input, char *command, char **argv) {
     if (reachedArgv) {
         arg[idx] = '\0';
         argv[argvIdx] = strdup(arg);
+    } else {
+        arg[idx] = '\0';
+        strcpy(command, arg);
     }
 }
 
@@ -52,7 +63,20 @@ int main() {
     char *env[32] = {"PATH=/bin"};
     char path[128] = "/";
 
-    strcpy(input, "/bin/echo meow");
-    parseInput(input, command, argv);
-    execve(command, argv, 0);
+    while (!false) {
+        memset(input, 0, sizeof(input));
+        memset(command, 0, sizeof(command));
+        memset(argv, 0, sizeof(argv));
+        write(1, path, strlen(path));
+        write(1, " $ ", 3);
+        int len = read(1, input, sizeof(input));
+        input[len - 1] = '\0';
+        parseInput(input, command, argv);
+        __pid_t forkResult = fork();
+        if (forkResult == 0) {
+            execve(command, argv, 0);
+        } else {
+            waitid(P_ALL, 0, 0, WEXITED);
+        }
+    }
 }
\ No newline at end of file