From d33281b1ac1efdbfd2a7896510e32214e03b62bd Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Sat, 5 May 2018 12:31:17 +0200 Subject: [PATCH 1/3] Remove @author tag --- websh.c | 1 - 1 file changed, 1 deletion(-) diff --git a/websh.c b/websh.c index 79dbc5b..1d99fde 100644 --- a/websh.c +++ b/websh.c @@ -1,6 +1,5 @@ /** * @file websh.c - * @author Tobias Eidelpes * @date 2018-04-16 * * @brief Formats program output for the web. From 76dcf53c8d81c74821ec13c124333c8da3ee28a1 Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Sat, 5 May 2018 17:57:01 +0200 Subject: [PATCH 2/3] Main logic completed --- websh.c | 115 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 40 deletions(-) diff --git a/websh.c b/websh.c index 1d99fde..489a08d 100644 --- a/websh.c +++ b/websh.c @@ -17,6 +17,7 @@ #include #define MAX_LEN 256 +#define MAX_LINES 1024 static const char *pname; @@ -26,6 +27,7 @@ static int sFlag = 0; static const char *word; static const char *tag; +static char *lines[MAX_LINES] = {NULL}; volatile sig_atomic_t quit = 0; @@ -99,16 +101,42 @@ void parse(int argc, char *argv[]) pname = argv[0]; } -void child2(int fd[], char readbuffer[]) +void child2(int fd[], char *line) { /* CHILD TWO */ + char readbuffer[MAX_LEN]; close(fd[1]); int nbytes = read(fd[0], readbuffer, MAX_LEN); if (nbytes == -1) { perror(pname); exit(EXIT_FAILURE); } - printf("%s", readbuffer); + readbuffer[nbytes-1] = '\0'; + if (eFlag && (strcmp(line, lines[0]) == 0)) { + printf("\n"); + } + if (hFlag) { + char *pch = strstr(line, "\n"); + if (pch != NULL) + strncpy(pch, "\0", 1); + printf("

%s

\n", line); + } + if (sFlag) { + if (strstr(readbuffer, word) != NULL) { + printf("<%s>%s
\n", tag, readbuffer, tag); + } else { + printf("%s
\n", readbuffer); + } + } else { + printf("%s
\n", readbuffer); + } + int i = 0; + while (lines[i] != NULL) { + i++; + } + if (eFlag && (strcmp(line, lines[i-1]) == 0)) { + printf("\n"); + } return; } @@ -138,61 +166,68 @@ int main(int argc, char *argv[]) char *line = NULL; size_t len = 0; ssize_t nread; - char readbuffer[MAX_LEN]; int status; int w; - if (eFlag) - printf("\n"); - int fd[2]; pid_t childpid1; pid_t childpid2; pipe(fd); - while ((nread = getline(&line, &len, stdin)) > 0) { - childpid1 = fork(); - if (childpid1 > 0) { - /* PARENT ONE */ - childpid2 = fork(); - if (childpid2 > 0) { - /* PARENT TWO */ - } else if (childpid2 == 0) { - /* CHILD TWO */ - child2(fd, readbuffer); - } else { - perror(pname); - free(line); - exit(EXIT_FAILURE); - } - do { - w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED); - if (w == -1) { - perror(pname); - exit(EXIT_FAILURE); - } + int count = 0; - 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 { + while ((nread = getline(&line, &len, stdin)) > 0) { + lines[count] = (char *) malloc (len); + strcpy(lines[count], line); + count++; + if (count == 1024) + break; + } + + for (int i = 0; i < count; i++) { + switch (childpid1 = fork()) { + case -1: perror(pname); free(line); exit(EXIT_FAILURE); + case 0: + child1(fd, lines[i]); + exit(EXIT_SUCCESS); + break; + default: + switch (childpid2 = fork()) { + case -1: + perror(pname); + free(line); + exit(EXIT_FAILURE); + case 0: + child2(fd, lines[i]); + exit(EXIT_SUCCESS); + break; + default: + do { + w = waitpid(-1, &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; + } } } free(line); break; } - + return 0; } From 488dd66da0d3aa76f7cf886ac63645c167e55d02 Mon Sep 17 00:00:00 2001 From: Tobias Eidelpes Date: Sun, 6 May 2018 12:21:30 +0200 Subject: [PATCH 3/3] Added header information --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index b61eb72..b888635 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,9 @@ +#--------------------------------------------------------------------# +# @date 2018-05-06 # +# # +# websh - A program to display linux commands in html format # +#--------------------------------------------------------------------# + CC = gcc CFLAGS = -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L -g -c