summary refs log tree commit diff
path: root/progs
diff options
context:
space:
mode:
authorWlodekM <[email protected]>2025-02-15 12:48:17 +0200
committerWlodekM <[email protected]>2025-02-15 12:48:17 +0200
commit82ee985d93484f8ac1cabd688e33a35cb2ebe926 (patch)
tree507a32348564cb5565b44bb02128986cd71b55bd /progs
parent545c5f85c38de8ddb8c026bf3b76658c8d8f3d6b (diff)
cd
Diffstat (limited to 'progs')
-rw-r--r--progs/shel/shel.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/progs/shel/shel.c b/progs/shel/shel.c
index 9eca8c5..7e8c310 100644
--- a/progs/shel/shel.c
+++ b/progs/shel/shel.c
@@ -2,8 +2,11 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <sys/wait.h>
+#include <fcntl.h>
+#include <sys/stat.h>
 #include <stdbool.h>
 #include <string.h>
+#include <stdlib.h>
 
 #define P_ALL 0
 #define P_PID 1
@@ -56,6 +59,12 @@ void parseInput(char *input, char *command, char **argv) {
     }
 }
 
+void joinPath(char *result, const char *base, const char *rel) {
+    strcpy(result, base);
+    strcat(result, "/");
+    strcat(result, rel);
+}
+
 int main() {
     char input[255] = {0};
     char command[128] = {0};
@@ -72,6 +81,30 @@ int main() {
         int len = read(1, input, sizeof(input));
         input[len - 1] = '\0';
         parseInput(input, command, argv);
+
+        if (!strcmp(command, "cd")) {
+            char newpath[128] = {0};
+            char newresolvedpath[128] = {0};
+            if (argv[1][0] == '/') {
+                strcpy(newresolvedpath, argv[1]);
+            } else {
+                joinPath(newpath, path, argv[1]);
+                realpath(newpath, newresolvedpath);
+            }
+            if (!access(newresolvedpath, F_OK)) {
+                struct stat statResult;
+                stat(newresolvedpath, &statResult);
+                if (S_ISDIR(statResult.st_mode)) {
+                    strcpy(path, newresolvedpath);
+                } else {
+                    write(1, "uuuuh das not a dir\n", 21);
+                }
+            } else {
+                write(1, "uuuuh das not a dir\n", 21);
+            }
+            continue;
+        }
+
         __pid_t forkResult = fork();
         if (forkResult == 0) {
             execve(command, argv, 0);