From 662bbf673a4264cdf552031c8bfac37cc5499dca Mon Sep 17 00:00:00 2001 From: Kranklyboy Date: Mon, 23 Apr 2018 15:33:23 +0200 Subject: [PATCH] Restructuring of main() function --- websh.c | 99 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/websh.c b/websh.c index 3f65cb9..79dbc5b 100644 --- a/websh.c +++ b/websh.c @@ -100,6 +100,31 @@ void parse(int argc, char *argv[]) pname = argv[0]; } +void child2(int fd[], char readbuffer[]) +{ + /* CHILD TWO */ + close(fd[1]); + int nbytes = read(fd[0], readbuffer, MAX_LEN); + if (nbytes == -1) { + perror(pname); + exit(EXIT_FAILURE); + } + printf("%s", readbuffer); + return; +} + +void child1(int fd[], char *line) +{ + /* CHILD ONE */ + close(fd[0]); + dup2(fd[1], STDOUT_FILENO); + execl("/bin/bash", "/bin/bash", "-c", line, (char *) NULL); + fprintf(stderr, "[%s] exec failed\n", pname); + free(line); + exit(EXIT_FAILURE); + return; +} + int main(int argc, char *argv[]) { parse(argc, argv); @@ -126,59 +151,43 @@ int main(int argc, char *argv[]) pid_t childpid2; pipe(fd); - while ((nread = getline(&line, &len, stdin)) != -1) { - switch (childpid1 = fork()) { - case -1: - perror(pname); - free(line); - exit(EXIT_FAILURE); - case 0: - /* CHILD ONE */ - close(fd[0]); - dup2(fd[1], STDOUT_FILENO); - execl("/bin/bash", "/bin/bash", "-c", line, (char *) NULL); - fprintf(stderr, "[%s] exec failed\n", pname); - free(line); - exit(EXIT_FAILURE); - default: + while ((nread = getline(&line, &len, stdin)) > 0) { + childpid1 = fork(); + if (childpid1 > 0) { /* PARENT ONE */ - switch (childpid2 = fork()) { - case -1: + childpid2 = fork(); + if (childpid2 > 0) { + /* PARENT TWO */ + } else if (childpid2 == 0) { + /* CHILD TWO */ + child2(fd, readbuffer); + } else { perror(pname); free(line); exit(EXIT_FAILURE); - case 0: - /* CHILD TWO */ - close(fd[1]); - int nbytes = read(fd[0], readbuffer, MAX_LEN); - if (nbytes == -1) { + } + do { + w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED); + if (w == -1) { perror(pname); - free(line); exit(EXIT_FAILURE); } - printf("%s", readbuffer); - return 0; - default: - /* PARENT TWO */ - do { - w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED); - if (w == -1) { - perror(pname); - exit(EXIT_FAILURE); - } - if (WIFSIGNALED(status)) { - printf("Killed by signal %d\n", WTERMSIG(status)); - } else if (WIFSTOPPED(status)) { - printf("Stopped by signal %d\n", WSTOPSIG(status)); - } else if (WIFCONTINUED(status)) { - printf("Continued\n"); - } - } while (!WIFEXITED(status) && !WIFSIGNALED(status)); - break; - } - - break; + if (WIFSIGNALED(status)) { + printf("Killed by signal %d\n", WTERMSIG(status)); + } else if (WIFSTOPPED(status)) { + printf("Stopped by signal %d\n", WSTOPSIG(status)); + } else if (WIFCONTINUED(status)) { + printf("Continued\n"); + } + } while (!WIFEXITED(status) && !WIFSIGNALED(status)); + } else if (childpid1 == 0) { + /* CHILD ONE */ + child1(fd, line); + } else { + perror(pname); + free(line); + exit(EXIT_FAILURE); } }