From bed31a7634701b2c19ef8eef3ccb3039c03b1cda Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sun, 24 May 2020 12:58:25 +0200 Subject: minor changes --- 02_exercise/process.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) (limited to '02_exercise/process.c') diff --git a/02_exercise/process.c b/02_exercise/process.c index 8dc331a..8f6589d 100644 --- a/02_exercise/process.c +++ b/02_exercise/process.c @@ -8,9 +8,10 @@ #include "process.h" // given a substring of a line tries to parse out as much information as possible -int parse_command(char const *line, char const *end, Process *p) { +int parse_command(char const *line, char const *end, process *p) { char *part; char **local_parts; + if (arrayInit(part) != 0 || arrayInit(local_parts) != 0) { fprintf(stderr, "Failed to prepare new part / parts array whilst parsing line"); return -1; @@ -18,19 +19,13 @@ int parse_command(char const *line, char const *end, Process *p) { for (size_t i = 0; line + i < end; ++i) { char c = line[i]; - if (c == ' ' || c == '\0') { + if (c == '\t' || c == ' ' || c == '\0') { if (arrayLen(part) == 0) continue; arrayPush(part) = '\0'; arrayPush(local_parts) = part; - - if (c == '\0') { - arrayPush(local_parts) = NULL; - break; - } else { - arrayInit(part); - } + arrayInit(part); } else { arrayPush(part) = c; } @@ -49,7 +44,7 @@ int parse_command(char const *line, char const *end, Process *p) { return 0; } -int parse_line(char const *const line, Process **processes) { +int parse_line(char const *const line, process **processes) { // splits the line at | and then parses the commands int ret_code; @@ -61,7 +56,7 @@ int parse_line(char const *const line, Process **processes) { bool done = false; char const *cursor = line; while (!done) { - Process p = {.in_fd = 0, .out_fd = 0, .pid = 0, .blocking = true}; + process p = {.argv = NULL, .argc = 0, .in_fd = 0, .out_fd = 0, .pid = 0, .blocking = true}; char const *end = strchr(cursor, '|'); if (end == NULL) { @@ -78,7 +73,7 @@ int parse_line(char const *const line, Process **processes) { } size_t p_len = arrayLen(*processes); - Process *last = *processes + (p_len - 1); + process *last = *processes + (p_len - 1); // linking up all processes as we currently only have pipes for multiple commands for (size_t i = 0; i < p_len - 1; ++i) { @@ -101,10 +96,11 @@ int parse_line(char const *const line, Process **processes) { (*processes)[i].blocking = false; } } + return ret_code; } -int exec_command(Process p) { +int exec_command(process p) { int status; if (!p.blocking) { @@ -118,10 +114,12 @@ int exec_command(Process p) { dup2(p.in_fd, 0); close(p.in_fd); } + if (p.out_fd != 0) { dup2(p.out_fd, 1); close(p.out_fd); } + execvp(p.argv[0], p.argv); fprintf(stderr, "command not found: \"%s\"\n", p.argv[0]); fflush(stderr); @@ -157,14 +155,16 @@ int exec_command(Process p) { return WEXITSTATUS(status); } -void free_processes(Process **pr) { - Process *processes = *pr; +void free_processes(process **pr) { + process *processes = *pr; while (!arrayIsEmpty(processes)) { - Process p = arrayPop(processes); + process p = arrayPop(processes); while (!arrayIsEmpty(p.argv)) { - char *tmp = arrayPop(p.argv); - if (tmp) + char *tmp = arrayTop(p.argv); + if (tmp) { arrayRelease(tmp); + } + arrayPop(p.argv) = NULL; } arrayRelease(p.argv); } -- cgit v1.2.3-54-g00ecf