Comments for clarity
This commit is contained in:
parent
b41bdbdddd
commit
fa455f597b
18
websh.c
18
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("<html><head></head><body>\n");
|
||||
}
|
||||
|
||||
if (hFlag) {
|
||||
/* remove \n from input string */
|
||||
char *pch = strstr(line, "\n");
|
||||
if (pch != NULL)
|
||||
strncpy(pch, "\0", 1);
|
||||
printf("<h1>%s</h1>\n", line);
|
||||
}
|
||||
if (sFlag) {
|
||||
/* try to find word in command and set in tag */
|
||||
if (strstr(readbuffer, word) != NULL) {
|
||||
printf("<%s>%s</%s><br />\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("</body></html>\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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user