Comments for clarity

This commit is contained in:
Tobias Eidelpes 2018-05-10 13:10:22 +02:00
parent b41bdbdddd
commit fa455f597b

18
websh.c
View File

@ -146,7 +146,7 @@ static void parse(int argc, char *argv[])
} }
} }
pname = argv[0]; pname = argv[0];
} }
@ -159,7 +159,6 @@ static void parse(int argc, char *argv[])
*/ */
static void child2(int fd[], char *line) static void child2(int fd[], char *line)
{ {
/* CHILD TWO */
char readbuffer[MAX_LEN]; char readbuffer[MAX_LEN];
close(fd[1]); close(fd[1]);
int nbytes = read(fd[0], readbuffer, MAX_LEN); int nbytes = read(fd[0], readbuffer, MAX_LEN);
@ -168,16 +167,18 @@ static void child2(int fd[], char *line)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
readbuffer[nbytes-1] = '\0'; readbuffer[nbytes-1] = '\0';
if (eFlag && (strcmp(line, lines[0]) == 0)) { if (eFlag && (strcmp(line, lines[0]) == 0))
printf("<html><head></head><body>\n"); printf("<html><head></head><body>\n");
}
if (hFlag) { if (hFlag) {
/* remove \n from input string */
char *pch = strstr(line, "\n"); char *pch = strstr(line, "\n");
if (pch != NULL) if (pch != NULL)
strncpy(pch, "\0", 1); strncpy(pch, "\0", 1);
printf("<h1>%s</h1>\n", line); printf("<h1>%s</h1>\n", line);
} }
if (sFlag) { if (sFlag) {
/* try to find word in command and set in tag */
if (strstr(readbuffer, word) != NULL) { if (strstr(readbuffer, word) != NULL) {
printf("<%s>%s</%s><br />\n", tag, readbuffer, tag); printf("<%s>%s</%s><br />\n", tag, readbuffer, tag);
} else { } else {
@ -190,6 +191,8 @@ static void child2(int fd[], char *line)
while (lines[i] != NULL) { while (lines[i] != NULL) {
i++; i++;
} }
/* if last line, print closing tags */
if (eFlag && (strcmp(line, lines[i-1]) == 0)) { if (eFlag && (strcmp(line, lines[i-1]) == 0)) {
printf("</body></html>\n"); printf("</body></html>\n");
} }
@ -206,7 +209,6 @@ static void child2(int fd[], char *line)
*/ */
static void child1(int fd[], char *line) static void child1(int fd[], char *line)
{ {
/* CHILD ONE */
close(fd[0]); close(fd[0]);
dup2(fd[1], STDOUT_FILENO); dup2(fd[1], STDOUT_FILENO);
execl("/bin/bash", "/bin/bash", "-c", line, (char *) NULL); execl("/bin/bash", "/bin/bash", "-c", line, (char *) NULL);
@ -252,10 +254,11 @@ int main(int argc, char *argv[])
lines[count] = (char *) malloc (len); lines[count] = (char *) malloc (len);
strcpy(lines[count], line); strcpy(lines[count], line);
count++; count++;
/* only read a maximum of 1024 lines */
if (count == 1024) if (count == 1024)
break; break;
} }
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
switch (childpid1 = fork()) { switch (childpid1 = fork()) {
case -1: case -1:
@ -263,6 +266,7 @@ int main(int argc, char *argv[])
free(line); free(line);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case 0: case 0:
/* CHILD ONE */
child1(fd, lines[i]); child1(fd, lines[i]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
@ -273,10 +277,12 @@ int main(int argc, char *argv[])
free(line); free(line);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case 0: case 0:
/* CHILD TWO */
child2(fd, lines[i]); child2(fd, lines[i]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
default: default:
/* PARENT */
do { do {
w = waitpid(-1, &status, WUNTRACED | WCONTINUED); w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
if (w == -1) { if (w == -1) {