views:

4112

answers:

3

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

A: 

Displaying three calendars at once has nothing to do with forking processes and really you don't need to get in to pipes and stuff.

What you want to use is the ncurses library to do special control of your output.

SoapBox
well one of my requirement is to use pipe() and dup2(). use read()/write() to stdin/stdout
Jonathan
ncurses is overkill
Jonathan Leffler
+2  A: 

This looks like homework, so I'll give you a way to approach the problem:

  1. Get it working with one calendar, reading in one line at a time and writing to stdout.
  2. Now store each line in an array of strings, and print out each line once you get the whole calendar read in.
  3. Get it working with three calendars, storing the results of each into three separate arrays of strings, then printing out all three (not next to each other).
  4. Instead of printing out all of the lines from one calendar, then all of the lines from the next calendar, etc., print out the first line from each calendar, then the second line from each calendar, etc.
  5. Fiddle around with the formatting until it looks right.
Adam Jaskiewicz
A: 

Why not using FILE *fp = popen("my command", "r"); , reading the output into an array of strings, repeating that three times and concatenating arrays properly?

valenok