#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argc, char** argv) {
int newin[2];
int newout[2];
int newerr[2];
if(pipe(newin) || pipe(newout) || pipe(newerr)) {
perror("pipe");
return EXIT_FAILURE;
}
pid_t pid = fork();
if(pid == 0) {
if(dup2(newin[0], STDIN_FILENO) == -1) {
perror("dup2");
return EXIT_FAILURE;
}
if(dup2(newout[1], STDOUT_FILENO) == -1) {
perror("dup2");
return EXIT_FAILURE;
}
if(dup2(newerr[1], STDERR_FILENO) == -1) {
perror("dup2");
return EXIT_FAILURE;
}
close(newin[1]);
return execl("/usr/bin/bc", "bc", NULL);
} else if(pid > 0) {
close(newout[1]);
close(newerr[1]);
write(newin[1], "5+2\n", 4);
close(newin[1]);
char buf[64];
ssize_t r;
while((r = read(newout[0], buf, 64)) > 0) {
write(STDOUT_FILENO, buf, r);
}
int status = -1;
waitpid(pid, &status, 0);
printf("child status code: %d\n", status);
close(newin[0]);
close(newout[0]);
close(newerr[0]);
} else {
perror("fork");
return EXIT_FAILURE;
}
return (EXIT_SUCCESS);
}