diff options
Diffstat (limited to 'progs')
-rw-r--r-- | progs/shel/shel.c | 33 |
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); |