views:

445

answers:

6

Ok I need to make this program to display "cal" 3 month(one month before and one month after) side by side, rather than just one single month it displays in any Linux/UNIX. I got it working to display 3 calendar by using "system(customCommand)" three times; but then it's not side by side.

I got some hint to use the following system calls:

close(..) pipe(..) dup2(..) read(..) and write(..)

my question is what should I start with? Do I need to create child process and than catch it in pipe(..)?

How can I display three calendar side by side.

ex.

    February 2009          March 2009             April 2009
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
 1  2  3  4  5  6  7    1  2  3  4  5  6  7             1  2  3  4
 8  9 10 11 12 13 14    8  9 10 11 12 13 14    5  6  7  8  9 10 11
15 16 17 18 19 20 21   15 16 17 18 19 20 21   12 13 14 15 16 17 18
22 23 24 25 26 27 28   22 23 24 25 26 27 28   19 20 21 22 23 24 25
                       29 30 31               26 27 28 29 30
+3  A: 

just do

cal -3
Steve B.
-1: "cal: unknown option"... That's not a POSIX option, fyi
dwc
+1 from me. It works perfectly on my system, and does exactly what you want.
Alex Fort
Actually, he's right, I'd forgotten that this will not work on a mac terminal, which is the only other unix I've been on lately. The question did say "any linux/unix".
Steve B.
A: 

The approach I would use for this would be to capture the output, split it into lines, and printf the lines out next to each other. I'd probably do it in Perl, though, rather than C.

Or just use cal -3, if your cal has it.

Adam Jaskiewicz
+1  A: 

Does this not work?

cal -3

chyne
wo never thought about that. thanks for the hint. but I've given instruction to use the following system call :(
Jonathan
+1  A: 

Ok, how about cal -3?

cal -3 12 2120 to make it a special month and year, with one before and one after.

E Dominique
+6  A: 

Assuming you want to write it yourself instead of using "cal -3", what I'd do (in psuedo code):

popen three calls to "cal" with the appropriate args

while (at least one of the three pipes hasn't hit EOF yet)
{
  read a line from the first if it isn't at EOF
  pad the results out to a width W, print it
  read a line from the second if it isn't at EOF
  pad the results out to a width W, print it
  read a line from the third if it isn't at EOF
  print it
  print "\n"
}

pclose all three.
Paul Tomblin
thanks! that's what i was looking for, so by that I don't need to create child processes?
Jonathan
popen creates the child processes for you. It's a shortcut - you can do the same with pipe or fork/exec if you prefer.
Paul Tomblin
http://linux.die.net/man/2/pipe has example code for doing it the hard way.
Paul Tomblin
+5  A: 

if "cal -3" doesn't work, just use paste :)

$ TERM=linux setterm -regtabs 24
$ paste <(cal 2 2009) <(cal 3 2009) <(cal 4 2009)
    febbraio 2009            marzo 2009              aprile 2009
do lu ma me gi ve sa    do lu ma me gi ve sa    do lu ma me gi ve sa
 1  2  3  4  5  6  7     1  2  3  4  5  6  7              1  2  3  4
 8  9 10 11 12 13 14     8  9 10 11 12 13 14     5  6  7  8  9 10 11
15 16 17 18 19 20 21    15 16 17 18 19 20 21    12 13 14 15 16 17 18
22 23 24 25 26 27 28    22 23 24 25 26 27 28    19 20 21 22 23 24 25
                        29 30 31                26 27 28 29 30

$

(setterm ignores -regtabs unless TERM=linux or TERM=con.)

ZeD
Looks like the last line has a problem. Is that the way the actual output looks, or did the formatting get messed up in the post?
Fred Larson
It is how the actual output looks, because the February output is short a line. I fixed it :)
ephemient