
take a look at this code
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc , char **argv)
{
int fd;
long int number1 , number2 , number3;
char op;
char *msg = "fuck";
if(!argv[1])
{
printf("%s pipe_name\n",argv[0]);
_exit(0);
}
fd = open(argv[1] , O_RDONLY);
if(fd == -1)
{
fprintf(stderr , "An error has occured %s\n",strerror(errno));
_exit(1);
}
close(0); //close the stdin fd
if(dup2(fd , 0) == -1)
{
fprintf(stderr , "An error has occured %s\n",strerror(errno));
_exit(1);
}
close(fd);
//now i can do what ever i want :D
scanf("%d %c %d",&number1 , &op , &number2);
printf("%d %d %c\n",number1 , number2 , op);
switch(op)
{
case '+':
number3 = number1 + number2;
break;
default:
{
char *ptr = (char *)&number3;
int x;
for(x =0 ; x<5 ; x++)
{
ptr[x] = msg[x];
}
}
break;
}
if(number3 > 0 && number3 < 9)
{
printf("Sum is %ld\n",number3);
}else
{
printf("%s\n",(char *)&number3);
}
return 0;
}
here is the make file for it
readpipe : read.o
gcc -o readpipe read.c
.PHONY : install
.PHONY : clean
install:
mkfifo pipe
clean :
rm piperead *.o
are we set ?
try it
readpipe pipe
from another terminal window do this
echo "1 + 1" > pipe
and see the result
do it again with "1 - 1" and let me see