#include #include #include #include #include int main(void) { pid_t pid; int pfdw[2]; int pfdr[2]; int nc, status; char expresion[100]; char respuesta[100]; if (pipe(pfdw) < 0) { perror("pipe"); exit(1); } if (pipe(pfdr) < 0) { perror("pipe"); exit(1); } if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { /* Attach standard input to the end of the pipe where parent write. */ dup2(pfdw[0], 0); close(pfdw[1]); /* Attach standard output to the end of the pipe where parent read. */ dup2(pfdr[1],1); close(pfdr[0]); execlp("bc", "bc", 0); perror("exec"); _exit(127); } close(pfdw[0]); close(pfdr[1]); do { nc=read(STDIN_FILENO, expresion, sizeof(expresion)); expresion[nc]='\n'; write(pfdw[1], expresion, nc+1); nc=read(pfdr[0], respuesta, sizeof(respuesta)); write(STDOUT_FILENO, respuesta, nc); } while (strncmp(expresion, "quit", 4)!=0); close(pfdw[1]); close(pfdr[0]); waitpid(pid, &status, 0); exit(0); }