From fa455f597bf54b10ac3769d5270960ce12c80929 Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Thu, 10 May 2018 13:10:22 +0200 Subject: [PATCH] Comments for clarity --- websh.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/websh.c b/websh.c index 069b5c0..1ca1998 100644 --- a/websh.c +++ b/websh.c @@ -146,7 +146,7 @@ static void parse(int argc, char *argv[]) } } - + pname = argv[0]; } @@ -159,7 +159,6 @@ static void parse(int argc, char *argv[]) */ static void child2(int fd[], char *line) { - /* CHILD TWO */ char readbuffer[MAX_LEN]; close(fd[1]); int nbytes = read(fd[0], readbuffer, MAX_LEN); @@ -168,16 +167,18 @@ static void child2(int fd[], char *line) exit(EXIT_FAILURE); } readbuffer[nbytes-1] = '\0'; - if (eFlag && (strcmp(line, lines[0]) == 0)) { + if (eFlag && (strcmp(line, lines[0]) == 0)) printf("\n"); - } + if (hFlag) { + /* remove \n from input string */ char *pch = strstr(line, "\n"); if (pch != NULL) strncpy(pch, "\0", 1); printf("

%s

\n", line); } if (sFlag) { + /* try to find word in command and set in tag */ if (strstr(readbuffer, word) != NULL) { printf("<%s>%s
\n", tag, readbuffer, tag); } else { @@ -190,6 +191,8 @@ static void child2(int fd[], char *line) while (lines[i] != NULL) { i++; } + + /* if last line, print closing tags */ if (eFlag && (strcmp(line, lines[i-1]) == 0)) { printf("\n"); } @@ -206,7 +209,6 @@ static void child2(int fd[], char *line) */ static 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); @@ -252,10 +254,11 @@ int main(int argc, char *argv[]) lines[count] = (char *) malloc (len); strcpy(lines[count], line); count++; + /* only read a maximum of 1024 lines */ if (count == 1024) break; } - + for (int i = 0; i < count; i++) { switch (childpid1 = fork()) { case -1: @@ -263,6 +266,7 @@ int main(int argc, char *argv[]) free(line); exit(EXIT_FAILURE); case 0: + /* CHILD ONE */ child1(fd, lines[i]); exit(EXIT_SUCCESS); break; @@ -273,10 +277,12 @@ int main(int argc, char *argv[]) free(line); exit(EXIT_FAILURE); case 0: + /* CHILD TWO */ child2(fd, lines[i]); exit(EXIT_SUCCESS); break; default: + /* PARENT */ do { w = waitpid(-1, &status, WUNTRACED | WCONTINUED); if (w == -1) {