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];
|
||||
}
|
||||
|
||||
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[])
|
||||
{
|
||||
parse(argc, argv);
|
||||
@ -126,59 +151,43 @@ int main(int argc, char *argv[])
|
||||
pid_t childpid2;
|
||||
pipe(fd);
|
||||
|
||||
while ((nread = getline(&line, &len, stdin)) != -1) {
|
||||
switch (childpid1 = fork()) {
|
||||
case -1:
|
||||
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:
|
||||
while ((nread = getline(&line, &len, stdin)) > 0) {
|
||||
childpid1 = fork();
|
||||
if (childpid1 > 0) {
|
||||
/* PARENT ONE */
|
||||
switch (childpid2 = fork()) {
|
||||
case -1:
|
||||
childpid2 = fork();
|
||||
if (childpid2 > 0) {
|
||||
/* PARENT TWO */
|
||||
} else if (childpid2 == 0) {
|
||||
/* CHILD TWO */
|
||||
child2(fd, readbuffer);
|
||||
} else {
|
||||
perror(pname);
|
||||
free(line);
|
||||
exit(EXIT_FAILURE);
|
||||
case 0:
|
||||
/* CHILD TWO */
|
||||
close(fd[1]);
|
||||
int nbytes = read(fd[0], readbuffer, MAX_LEN);
|
||||
if (nbytes == -1) {
|
||||
}
|
||||
do {
|
||||
w = waitpid(childpid2, &status, WUNTRACED | WCONTINUED);
|
||||
if (w == -1) {
|
||||
perror(pname);
|
||||
free(line);
|
||||
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)) {
|
||||
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;
|
||||
}
|
||||
|
||||
break;
|
||||
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 {
|
||||
perror(pname);
|
||||
free(line);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user