Main logic completed
This commit is contained in:
parent
d33281b1ac
commit
76dcf53c8d
85
websh.c
85
websh.c
@ -17,6 +17,7 @@
|
|||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
|
|
||||||
#define MAX_LEN 256
|
#define MAX_LEN 256
|
||||||
|
#define MAX_LINES 1024
|
||||||
|
|
||||||
static const char *pname;
|
static const char *pname;
|
||||||
|
|
||||||
@ -26,6 +27,7 @@ static int sFlag = 0;
|
|||||||
|
|
||||||
static const char *word;
|
static const char *word;
|
||||||
static const char *tag;
|
static const char *tag;
|
||||||
|
static char *lines[MAX_LINES] = {NULL};
|
||||||
|
|
||||||
volatile sig_atomic_t quit = 0;
|
volatile sig_atomic_t quit = 0;
|
||||||
|
|
||||||
@ -99,16 +101,42 @@ void parse(int argc, char *argv[])
|
|||||||
pname = argv[0];
|
pname = argv[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
void child2(int fd[], char readbuffer[])
|
void child2(int fd[], char *line)
|
||||||
{
|
{
|
||||||
/* CHILD TWO */
|
/* CHILD TWO */
|
||||||
|
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);
|
||||||
if (nbytes == -1) {
|
if (nbytes == -1) {
|
||||||
perror(pname);
|
perror(pname);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("%s", readbuffer);
|
readbuffer[nbytes-1] = '\0';
|
||||||
|
if (eFlag && (strcmp(line, lines[0]) == 0)) {
|
||||||
|
printf("<html><head></head><body>\n");
|
||||||
|
}
|
||||||
|
if (hFlag) {
|
||||||
|
char *pch = strstr(line, "\n");
|
||||||
|
if (pch != NULL)
|
||||||
|
strncpy(pch, "\0", 1);
|
||||||
|
printf("<h1>%s</h1>\n", line);
|
||||||
|
}
|
||||||
|
if (sFlag) {
|
||||||
|
if (strstr(readbuffer, word) != NULL) {
|
||||||
|
printf("<%s>%s</%s><br />\n", tag, readbuffer, tag);
|
||||||
|
} else {
|
||||||
|
printf("%s<br />\n", readbuffer);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printf("%s<br />\n", readbuffer);
|
||||||
|
}
|
||||||
|
int i = 0;
|
||||||
|
while (lines[i] != NULL) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (eFlag && (strcmp(line, lines[i-1]) == 0)) {
|
||||||
|
printf("</body></html>\n");
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,35 +166,47 @@ int main(int argc, char *argv[])
|
|||||||
char *line = NULL;
|
char *line = NULL;
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
ssize_t nread;
|
ssize_t nread;
|
||||||
char readbuffer[MAX_LEN];
|
|
||||||
int status;
|
int status;
|
||||||
int w;
|
int w;
|
||||||
|
|
||||||
if (eFlag)
|
|
||||||
printf("<html><head></head><body>\n");
|
|
||||||
|
|
||||||
int fd[2];
|
int fd[2];
|
||||||
pid_t childpid1;
|
pid_t childpid1;
|
||||||
pid_t childpid2;
|
pid_t childpid2;
|
||||||
pipe(fd);
|
pipe(fd);
|
||||||
|
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
while ((nread = getline(&line, &len, stdin)) > 0) {
|
while ((nread = getline(&line, &len, stdin)) > 0) {
|
||||||
childpid1 = fork();
|
lines[count] = (char *) malloc (len);
|
||||||
if (childpid1 > 0) {
|
strcpy(lines[count], line);
|
||||||
/* PARENT ONE */
|
count++;
|
||||||
childpid2 = fork();
|
if (count == 1024)
|
||||||
if (childpid2 > 0) {
|
break;
|
||||||
/* PARENT TWO */
|
}
|
||||||
} else if (childpid2 == 0) {
|
|
||||||
/* CHILD TWO */
|
for (int i = 0; i < count; i++) {
|
||||||
child2(fd, readbuffer);
|
switch (childpid1 = fork()) {
|
||||||
} else {
|
case -1:
|
||||||
perror(pname);
|
perror(pname);
|
||||||
free(line);
|
free(line);
|
||||||
exit(EXIT_FAILURE);
|
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 {
|
do {
|
||||||
w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED);
|
w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
|
||||||
if (w == -1) {
|
if (w == -1) {
|
||||||
perror(pname);
|
perror(pname);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
@ -180,13 +220,8 @@ int main(int argc, char *argv[])
|
|||||||
printf("Continued\n");
|
printf("Continued\n");
|
||||||
}
|
}
|
||||||
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||||
} else if (childpid1 == 0) {
|
break;
|
||||||
/* CHILD ONE */
|
}
|
||||||
child1(fd, line);
|
|
||||||
} else {
|
|
||||||
perror(pname);
|
|
||||||
free(line);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user