Restructuring of main() function
This commit is contained in:
parent
2523c18eea
commit
662bbf673a
99
websh.c
99
websh.c
@ -100,6 +100,31 @@ void parse(int argc, char *argv[])
|
|||||||
pname = argv[0];
|
pname = argv[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void child2(int fd[], char readbuffer[])
|
||||||
|
{
|
||||||
|
/* CHILD TWO */
|
||||||
|
close(fd[1]);
|
||||||
|
int nbytes = read(fd[0], readbuffer, MAX_LEN);
|
||||||
|
if (nbytes == -1) {
|
||||||
|
perror(pname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
printf("%s", readbuffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
fprintf(stderr, "[%s] exec failed\n", pname);
|
||||||
|
free(line);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
parse(argc, argv);
|
parse(argc, argv);
|
||||||
@ -126,59 +151,43 @@ int main(int argc, char *argv[])
|
|||||||
pid_t childpid2;
|
pid_t childpid2;
|
||||||
pipe(fd);
|
pipe(fd);
|
||||||
|
|
||||||
while ((nread = getline(&line, &len, stdin)) != -1) {
|
while ((nread = getline(&line, &len, stdin)) > 0) {
|
||||||
switch (childpid1 = fork()) {
|
childpid1 = fork();
|
||||||
case -1:
|
if (childpid1 > 0) {
|
||||||
perror(pname);
|
|
||||||
free(line);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
case 0:
|
|
||||||
/* CHILD ONE */
|
|
||||||
close(fd[0]);
|
|
||||||
dup2(fd[1], STDOUT_FILENO);
|
|
||||||
execl("/bin/bash", "/bin/bash", "-c", line, (char *) NULL);
|
|
||||||
fprintf(stderr, "[%s] exec failed\n", pname);
|
|
||||||
free(line);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
default:
|
|
||||||
/* PARENT ONE */
|
/* PARENT ONE */
|
||||||
switch (childpid2 = fork()) {
|
childpid2 = fork();
|
||||||
case -1:
|
if (childpid2 > 0) {
|
||||||
|
/* PARENT TWO */
|
||||||
|
} else if (childpid2 == 0) {
|
||||||
|
/* CHILD TWO */
|
||||||
|
child2(fd, readbuffer);
|
||||||
|
} else {
|
||||||
perror(pname);
|
perror(pname);
|
||||||
free(line);
|
free(line);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
case 0:
|
}
|
||||||
/* CHILD TWO */
|
do {
|
||||||
close(fd[1]);
|
w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED);
|
||||||
int nbytes = read(fd[0], readbuffer, MAX_LEN);
|
if (w == -1) {
|
||||||
if (nbytes == -1) {
|
|
||||||
perror(pname);
|
perror(pname);
|
||||||
free(line);
|
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("%s", readbuffer);
|
|
||||||
return 0;
|
|
||||||
default:
|
|
||||||
/* PARENT TWO */
|
|
||||||
do {
|
|
||||||
w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED);
|
|
||||||
if (w == -1) {
|
|
||||||
perror(pname);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (WIFSIGNALED(status)) {
|
if (WIFSIGNALED(status)) {
|
||||||
printf("Killed by signal %d\n", WTERMSIG(status));
|
printf("Killed by signal %d\n", WTERMSIG(status));
|
||||||
} else if (WIFSTOPPED(status)) {
|
} else if (WIFSTOPPED(status)) {
|
||||||
printf("Stopped by signal %d\n", WSTOPSIG(status));
|
printf("Stopped by signal %d\n", WSTOPSIG(status));
|
||||||
} else if (WIFCONTINUED(status)) {
|
} else if (WIFCONTINUED(status)) {
|
||||||
printf("Continued\n");
|
printf("Continued\n");
|
||||||
}
|
}
|
||||||
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
|
||||||
break;
|
} else if (childpid1 == 0) {
|
||||||
}
|
/* CHILD ONE */
|
||||||
|
child1(fd, line);
|
||||||
break;
|
} else {
|
||||||
|
perror(pname);
|
||||||
|
free(line);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user