Advertisement
Guest User

Untitled

a guest
May 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <err.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include <string.h>
  8.  
  9. int main(int argc,char * argv[])
  10. {
  11.     if(argc!=3)
  12.     {
  13.         errx(1,"Invalid number of arguments");
  14.     }
  15.  
  16.     int pimp1[2];
  17.     int pimp2[2];
  18.     if(pipe(pimp1)==-1)
  19.     {
  20.         errx(100,"Couldnt make a pipe");
  21.     }
  22.    
  23.     if(pipe(pimp2)==-1)
  24.     {
  25.         errx(100,"Couldnt make the pipe");
  26.     }
  27.  
  28.     int fd1=fork();
  29.     if(fd1==-1)
  30.     {
  31.         errx(2,"Ops something bad happened");
  32.     }
  33.     else if(fd1==0)
  34.     {
  35.        close(pimp1[1]);
  36.        close(0);
  37.        dup(pimp1[0]);
  38.        //char buff[10];
  39.        //ssize_t readsize=read(0,&buff,sizeof(buff));
  40.        //write(1,&buff,readsize);
  41.        execlp("wc","wc","-c",(char*)NULL);
  42.        exit(99);
  43.        //exit(0);
  44.     }
  45.  
  46.     int fd2=fork();
  47.     if(fd2==-1)
  48.     {
  49.       errx(3,"Ops something bad happened");
  50.     }
  51.     else if(fd2==0)
  52.     {
  53.         close(pimp2[1]);
  54.         close(0);
  55.         dup(pimp2[0]);
  56.         //char buff[10];
  57.         //ssize_t readsize=read(0,&buff,sizeof(buff));
  58.         //write(1,&buff,readsize);
  59.         execlp("wc","wc","-c",(char*)NULL);
  60.         exit(99);
  61.        // exit(0);
  62.     }
  63.  
  64.     close(pimp1[0]);
  65.     close(pimp2[0]);
  66.  
  67.     int counter=1;
  68.     ssize_t readsize=0;
  69.     while((readsize=read(0,&buff,sizeof(buff)))>0)
  70.     {
  71.         if(buff[readsize-1]=='\n')
  72.         {
  73.            readsize--;
  74.        
  75.         }
  76.  
  77.        
  78.         if(counter%2==0)
  79.         {
  80.             if(write(pimp2[1],&buff,6)!=6)
  81.             {
  82.                 close(pimp2[1]);
  83.                 close(pimp1[1]);
  84.                 errx(10,"Error while writing");
  85.             }
  86.             close(pimp2[1]);
  87.         }
  88.         else
  89.         {
  90.             if(write(pimp1[1],&buff,6)!=6)
  91.             {
  92.                 close(pimp2[1]);
  93.                 close(pimp1[1]);
  94.                 errx(10,"Error while writing");
  95.             }
  96.             close(pimp1[1]);
  97.         }
  98.         counter++;
  99.     }
  100.    
  101.  
  102.     if(readsize==-1)
  103.     {
  104.         errx(23,"Relax, u piece of shit, u fucking garbage");
  105.     }
  106.  
  107.   ("Sup\n");
  108.     int status1=0;
  109.     int status2=0;
  110.     wait(&status1);
  111.     wait(&status2);
  112.  
  113.  
  114.     if(!WIFEXITED(status1)||WEXITSTATUS(status1)!=0)
  115.     {
  116.         errx(20,"Error with the first command");
  117.     }
  118.     else if(!WIFEXITED(status2)||WEXITSTATUS(status2)!=0)
  119.     {
  120.        errx(20,"Error with the second command");
  121.     }
  122.     exit(0);
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement