I need to simulated a Linux command line utility "cal -3
" where it will displays three calendar side by side. What I need right now is to get my pipe working. I've been told that I can't use fork()
, rather using dup2() and write(), read(), close()
to call system("myCustomCommand")
tree times. Right now my program does not display calendar side bye side.
I am trying to use the pipe and went into problem. Here is what I am trying,
int pfd[2];
int p; //for pipe
int d; //for dup2
const int BSIZE = 256;
char buf[BSIZE];
p = pipe(pfd);
if (p == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (p == 0)
{
d = dup2(pfd[1], 0);
close(pfd[1]);
nbytes = read (pfd[1], buf , BSIZE);
close(pfd[0]);
exit(EXIT_SUCCESS);
}
else
{
close(pfd[0]);
write(pfd[1], "test\n", BSIZE);
close(pfd[1]);
exit(EXIT_SUCCESS);
}
this code does not display anything, I want to display my cal program to stdout