From 6b47ff2690b473e86adf6ded723619b546047d7e Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Thu, 21 May 2020 12:28:02 +0200 Subject: memory should be fixed? i think --- 02_exercise/shell.c | 55 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/02_exercise/shell.c b/02_exercise/shell.c index 5a8d48f..c960680 100644 --- a/02_exercise/shell.c +++ b/02_exercise/shell.c @@ -10,13 +10,14 @@ void print_prompt(); -int parse_line(char const *line, char **exe, char ***parts, size_t *part_count); +int parse_line(char const *line, char ***parts, size_t *part_count); // returns the return code of the executed program int exec_command(const char *path, char *const argv[], unsigned timeout); int main(int argc, char* argv[]) { - while (true) { + bool done = false; + while (!done) { char *line = NULL; size_t cap = 0; size_t length = 0; @@ -29,35 +30,48 @@ int main(int argc, char* argv[]) { line[length - 1] = '\0'; // cut the line feed - char *exe = NULL; char **arguments = NULL; size_t argument_count; - parse_line(line, &exe, &arguments, &argument_count); + parse_line(line, &arguments, &argument_count); - if (strcmp(exe, "cd") == 0) { - printf("Changing dirs \n"); - chdir("beispiele"); - } else if (strcmp(exe, "exit") == 0) { - exit(0); + if (strcmp(arguments[0], "cd") == 0) { + if (arrayLen(arguments) != 3) { + fprintf(stderr, "usage: cd "); + goto clean; + } + int ret = chdir(arguments[1]); + if (ret) + printf("[%i] ", ret); + } else if (strcmp(arguments[0], "exit") == 0) { + done = true; } else { - exec_command(exe, arguments, 0); + int ret = exec_command(arguments[0], arguments, 0); + if (ret) + printf("[%i] ", ret); } + + clean: free((void *)line); - // no need to free exe, since it is part of arguments any way + while (arrayLen(arguments) > 0) { + char *tmp = arrayPop(arguments); + if (tmp) + arrayRelease(tmp); + } arrayRelease(arguments); } } -int parse_line(char const *line, char **exe, char ***parts, size_t *part_count) { +int parse_line(char const *line, char ***parts, size_t *part_count) { char *part; char **local_parts; - if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) { + arrayInit(part); + arrayInit(local_parts); +/* if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) { fprintf(stderr, "Failed to prepare new part array"); exit(-1); - } + }*/ - bool found_exe = false; char c; int i = 0; while (true) { @@ -65,16 +79,11 @@ int parse_line(char const *line, char **exe, char ***parts, size_t *part_count) if (c == ' ' || c == '\0') { arrayPush(part) = '\0'; arrayPush(local_parts) = part; - if (!found_exe) { - // the first is the executable - *exe = malloc(arrayLen(part) * sizeof(char)); - strcpy(*exe, part); - found_exe = true; - } - arrayInit(part); if (c == '\0') { arrayPush(local_parts) = NULL; break; + } else { + arrayInit(part); } } else { arrayPush(part) = c; @@ -110,7 +119,7 @@ int exec_command(const char *path, char *const argv[], unsigned timeout) { dup2(pipefd[1], 1); // includes close(1); close(pipefd[1]); execvp(path, argv); - fprintf(stderr, "no program\n"); + fprintf(stderr, "could not execute \"%s\"\n", path); exit(-1); } -- cgit v1.2.3-54-g00ecf