From e1520589de17d08c4bde37f1198f18d012a1f377 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sat, 6 Jun 2020 14:06:00 +0200 Subject: basic shell works - sometimes goes out of sync (running current command only after issueing the next) --- 03_exercise/srv/shell.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to '03_exercise/srv/shell.c') diff --git a/03_exercise/srv/shell.c b/03_exercise/srv/shell.c index a32fc5f..74586c7 100644 --- a/03_exercise/srv/shell.c +++ b/03_exercise/srv/shell.c @@ -11,7 +11,7 @@ #include "prompt_utils.h" #include "builtins.h" -#define BUF_SIZE 256 +#define BUF_SIZE 2048 process *processes; @@ -55,19 +55,19 @@ int shell(int in_fd) { bool done = false; while (!done) { char line[BUF_SIZE]; + __ssize_t length; - for (int i = 0; i < BUF_SIZE; ++i) { - read(in_fd, line + i, 1); - if (line[i] == '\n') { - line[i] = 0; - break; - } + if ((length = read(in_fd, line, BUF_SIZE)) < 0) { + fprintf(stderr, "Failed to read from STDIN"); + fflush(stderr); + exit(-1); } if (strspn(line, " \n\t") == strlen(line)) { // skip empty lines - empty being just spaces or tabs continue; } + line[length - 1] = '\0'; // cut the line feed processes = NULL; parse_line(line, &processes); @@ -92,6 +92,8 @@ int shell(int in_fd) { if (ret) printf("[%i] ", ret); + else + printf("%s\n", get_current_dir_name()); } else if (strcmp(processes[0].argv[0], "exit") == 0) { done = true; @@ -115,6 +117,8 @@ int shell(int in_fd) { free_processes(&processes); } + printf("Disconnecting..."); + free((void *) original_wd); return 0; -- cgit v1.2.3-54-g00ecf