Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <err.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- int main(int argc,char * argv[])
- {
- if(argc!=3)
- {
- errx(1,"Invalid number of arguments");
- }
- int pimp1[2];
- int pimp2[2];
- if(pipe(pimp1)==-1)
- {
- errx(100,"Couldnt make a pipe");
- }
- if(pipe(pimp2)==-1)
- {
- errx(100,"Couldnt make the pipe");
- }
- int fd1=fork();
- if(fd1==-1)
- {
- errx(2,"Ops something bad happened");
- }
- else if(fd1==0)
- {
- close(pimp1[1]);
- close(0);
- dup(pimp1[0]);
- //char buff[10];
- //ssize_t readsize=read(0,&buff,sizeof(buff));
- //write(1,&buff,readsize);
- execlp("wc","wc","-c",(char*)NULL);
- exit(99);
- //exit(0);
- }
- int fd2=fork();
- if(fd2==-1)
- {
- errx(3,"Ops something bad happened");
- }
- else if(fd2==0)
- {
- close(pimp2[1]);
- close(0);
- dup(pimp2[0]);
- //char buff[10];
- //ssize_t readsize=read(0,&buff,sizeof(buff));
- //write(1,&buff,readsize);
- execlp("wc","wc","-c",(char*)NULL);
- exit(99);
- // exit(0);
- }
- close(pimp1[0]);
- close(pimp2[0]);
- int counter=1;
- ssize_t readsize=0;
- while((readsize=read(0,&buff,sizeof(buff)))>0)
- {
- if(buff[readsize-1]=='\n')
- {
- readsize--;
- }
- if(counter%2==0)
- {
- if(write(pimp2[1],&buff,6)!=6)
- {
- close(pimp2[1]);
- close(pimp1[1]);
- errx(10,"Error while writing");
- }
- close(pimp2[1]);
- }
- else
- {
- if(write(pimp1[1],&buff,6)!=6)
- {
- close(pimp2[1]);
- close(pimp1[1]);
- errx(10,"Error while writing");
- }
- close(pimp1[1]);
- }
- counter++;
- }
- if(readsize==-1)
- {
- errx(23,"Relax, u piece of shit, u fucking garbage");
- }
- ("Sup\n");
- int status1=0;
- int status2=0;
- wait(&status1);
- wait(&status2);
- if(!WIFEXITED(status1)||WEXITSTATUS(status1)!=0)
- {
- errx(20,"Error with the first command");
- }
- else if(!WIFEXITED(status2)||WEXITSTATUS(status2)!=0)
- {
- errx(20,"Error with the second command");
- }
- exit(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement