Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <sys/wait.h>
  5. #include <sys/stat.h>
  6. #include <sys/types.h>
  7. #include <fcntl.h>
  8. int main(){
  9.     int status;
  10.     //subshell
  11.     int pid=fork();
  12.     if(pid){
  13.         waitpid(pid,&status,0);
  14.         return status;
  15.     } else{
  16.         int pip[2];
  17.         pipe(pip);
  18.         int st;
  19.         //cat
  20.         int pid1=fork();
  21.         if(!pid1){
  22.             dup2(pip[1],1);
  23.             close(pip[1]);
  24.             close(pip[0]);
  25.             execlp("cat","cat","a",NULL);
  26.         }
  27.         //cut
  28.         int pid2=fork();
  29.         if(!pid2){
  30.             dup2(pip[0],0);
  31.             close(pip[0]);
  32.             close(pip[1]);
  33.             //redirection
  34.             int out=open("out",O_CREAT|O_WRONLY|O_APPEND,644);
  35.             dup2(out,1);
  36.             close(out);
  37.             execlp("cut","cut","-d;","-f1",NULL);
  38.         }
  39.         close(pip[1]);
  40.         close(pip[0]);
  41.         waitpid(pid1,&st,0);
  42.         waitpid(pid2,&st,0);
  43.         //echo
  44.         if(WEXITSTATUS(st)>0){
  45.             if(!fork()){
  46.                 execlp("echo","echo","benis",NULL);
  47.             }
  48.             wait(&st);
  49.         }
  50.         exit(st);
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement