diff options
author | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-13 19:55:28 +0200 |
---|---|---|
committer | Stefan Zabka <zabkaste@hu-berlin.de> | 2020-05-13 19:55:28 +0200 |
commit | 0cc3a2176bd396a35f44fcc28ec916b41f019e16 (patch) | |
tree | 0c90b0ef0f2fbd26b380809d1b9c3f9d445116f8 | |
parent | 97a8aad5493bab671e56eb25539b81e31d58c11a (diff) | |
download | betriebssysteme-0cc3a2176bd396a35f44fcc28ec916b41f019e16.tar.gz betriebssysteme-0cc3a2176bd396a35f44fcc28ec916b41f019e16.zip |
Made a shell that can exit
-rw-r--r-- | 02_exercise/Makefile | 2 | ||||
-rw-r--r-- | 02_exercise/shell.c | 38 |
2 files changed, 35 insertions, 5 deletions
diff --git a/02_exercise/Makefile b/02_exercise/Makefile index c43966f..5d1b360 100644 --- a/02_exercise/Makefile +++ b/02_exercise/Makefile @@ -6,7 +6,7 @@ SRC = $(wildcard *.c) OBJ = $(SRC:%.c=%.o) PCK = lab-2.zip -CFLAGS = -std=gnu11 -c -g -Os -Wall -Werror -MMD -MP +CFLAGS = -std=gnu11 -c -g -Os -Wall -Werror -MMD -MP -D=_GNU_SOURCE DEP = $(OBJ:%.o=%.d) -include $(DEP) diff --git a/02_exercise/shell.c b/02_exercise/shell.c index 0cea1f6..88b17fd 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -1,16 +1,46 @@ -#include <stdio.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> #include <fcntl.h> #include <signal.h> +#include <sys/stat.h> #include <sys/types.h> #include <sys/wait.h> -#include <sys/stat.h> +#include <unistd.h> #include "array.h" +#include "stdbool.h" + +void print_prompt(char const *const original_wd, char const *current_wd); int main(void) { - return 0; + + char const *const original_wd = get_current_dir_name(); + char const *current_wd = get_current_dir_name(); + + while (true) { + char *command = NULL; + size_t cap = 0; + size_t length = 0; + print_prompt(original_wd, current_wd); + if ((length = getline(&command, &cap, stdin)) < 0) { + fprintf(stderr, "Failed to read from STDIN"); + } + if (strcmp(command, "exit\n") == 0) { + exit(0); + } + free((void *)command); + } + // Gotta cast to void otherwise we discard qualifiers + free((void *)original_wd); + free((void *)current_wd); } + +void print_prompt(char const *const original_wd, char const *current_wd) { + + int result = strcmp(original_wd, current_wd); + if (result == 0) { + printf("./ > "); + } +}
\ No newline at end of file |