Advertisement
Guest User

Untitled

a guest
Aug 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7.     int fd_in[2];
  8.     int fd_out[2];
  9.     pid_t pid;
  10.  
  11.     if ((pipe(fd_in) < 0) && (pipe(fd_out) < 0)) {
  12.         return EXIT_FAILURE;
  13.     }
  14.    
  15.     if ((pid = fork()) < 0) {
  16.         return EXIT_FAILURE;
  17.    
  18.     } else if (pid != 0) { /* father */
  19.       close(fd_in[1]);
  20.       close(fd_out[0]);
  21.       dup2(fd_in[0], STDIN_FILENO);
  22.       dup2(fd_out[1], STDOUT_FILENO);
  23.       execlp("bash", "bash", (char *)0);
  24.      
  25.     } else { /* son */
  26.       FILE *file = fdopen(fd_out[0], "w");
  27.       int c;
  28.       close(fd_in[0]);
  29.       close(fd_out[1]);
  30.       write(fd_in[1], "ls\n", 3);
  31.       while( (c=fgetc(file) != EOF)) {
  32.          putchar(c);
  33.       }
  34.      
  35.     }
  36.  
  37.     return EXIT_SUCCESS;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement