From 40d578c09654ba6720ffd6e365e0d411cfa38613 Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Fri, 22 May 2020 00:24:51 +0200 Subject: Somewhat working pipes --- 02_exercise/process.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) (limited to '02_exercise/process.c') diff --git a/02_exercise/process.c b/02_exercise/process.c index 9bc85be..00f8762 100644 --- a/02_exercise/process.c +++ b/02_exercise/process.c @@ -44,17 +44,15 @@ int parse_command(char const *line, char const *end, process *p) { arrayPush(local_parts) = part; } - arrayInit(part); - arrayPush(part) = '\0'; - arrayPush(local_parts) = part; - p->argc = arrayLen(local_parts); + arrayPush(local_parts) = NULL; + p->argc = arrayLen(local_parts) - 1; p->argv = local_parts; return 0; } int parse_line(char const *const line, process **processes) { - //Splits the line at | and then parses the commands + // Splits the line at | and then parses the commands int ret_code = 0; if (arrayInit(*processes) != 0) { @@ -78,24 +76,24 @@ int parse_line(char const *const line, process **processes) { cursor = end + 1; } 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) { + for (size_t i = 0; i < p_len - 1; ++i) { int fds[2]; - if(pipe(fds) != 0) { + if (pipe(fds) != 0) { perror("Failed to create pipe"); return -1; } - (*processes)[i].out_fd=fds[1]; - (*processes)[i+1].in_fd=fds[0]; - + (*processes)[i].out_fd = fds[1]; + (*processes)[i + 1].in_fd = fds[0]; } // Setting all processes to non blocking when - if(strcmp(last->argv[last->argc-2], "&")== 0) { + if (strcmp(last->argv[last->argc - 1], "&") == 0) { arrayPop(last->argv); last->argc = last->argc - 1; + last->argv[last->argc] = NULL; for (size_t i = 0; i < p_len; ++i) { (*processes)[i].blocking = false; @@ -132,8 +130,8 @@ int exec_command(process p, unsigned timeout) { return WEXITSTATUS(status); } -int free_processes(process ** pr) { - process * processes = * pr; +int free_processes(process **pr) { + process *processes = *pr; while (!arrayIsEmpty(processes)) { process p = arrayPop(processes); while (!arrayIsEmpty(p.argv)) { -- cgit v1.2.3-54-g00ecf