-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·196 lines (178 loc) · 4.98 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
*cmpt-300
*Assignment 5
*Naoya Makino
*SFU email ID: [email protected]
*SFU #: 301117541
*Dec 3rd, 2009
*File: main.c
*Description: display_3months(part a): display 3 months sequentially
* display_3months_side(part b): output the 3 months side-by-side
* in order to run part a, please comment out display_3months_side(month, year)
* and take out comments on display_3months(month, year) in main function
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <string>
#include <sys/types.h>
#include <assert.h>
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE -1
#endif
#define NUM_OF_LINES_OF_MONTH 8
#define MAXSIZE 20
#define JAN 1
#define DEC 12
using namespace std;
void display_3months(int month, int year);
void display_year(int year);
void display_3months_side(int month, int year);
void addSpaces(int count);
main (int argc, char **argv) {
time_t rawtime;
struct tm *timeinfo;
int month,year;
char buf[10], buf2[10];
time ( &rawtime );
timeinfo = localtime( &rawtime );
strftime(buf, sizeof(buf), "%m", timeinfo);
strftime(buf2, sizeof(buf2), "%Y", timeinfo);
month = atoi(buf);
year = atoi(buf2);
if(argc == 1)
//display_3months(month, year);
display_3months_side(month, year);
else if(argc == 2)
{
year = atoi(argv[1]);
display_year(year);
}
else if(argc == 3)
{
month = atoi(argv[1]);
year = atoi(argv[2]);
//display_3months(month, year);
display_3months_side(month, year);
}
}
/*part a*/
void display_3months(int month, int year)
{
assert(year > 0 && 9999 > year);
assert(month > 0 && month < 13);
char cal[MAXSIZE];
if(month == JAN)
{
sprintf(cal, "%s %d %d","/usr/bin/cal", DEC, year-1);
system(cal);
sprintf(cal, "%s %d %d","/usr/bin/cal", month, year);
system(cal);
sprintf(cal, "%s %d %d","/usr/bin/cal", month+1, year);
system(cal);
}
else if(month == DEC)
{
sprintf(cal, "%s %d %d","/usr/bin/cal", month-1, year);
system(cal);
sprintf(cal, "%s %d %d","/usr/bin/cal", month, year);
system(cal);
sprintf(cal, "%s %d %d","/usr/bin/cal", JAN, year+1);
system(cal);
}
else
{
sprintf(cal, "%s %d %d","/usr/bin/cal", month-1, year);
system(cal);
sprintf(cal, "%s %d %d","/usr/bin/cal", month, year);
system(cal);
sprintf(cal, "%s %d %d","/usr/bin/cal", month+1, year);
system(cal);
}
}
void display_year(int year)
{
assert(year > 0 && 9999 > year);
char cal[MAXSIZE];
sprintf(cal, "%s %d","/usr/bin/cal", year);
system(cal);
}
/*part b*/
void display_3months_side(int month, int year)
{
assert(year > 0 && 9999 > year);
assert(month > 0 && month < 13);
char cal[MAXSIZE],preCal[MAXSIZE],nextCal[MAXSIZE],
buf[MAXSIZE],buf2[MAXSIZE],buf3[MAXSIZE];
int fd[2],fd2[2],fd3[2];//filedes[0] is for reading, filedes[1] is for writing.
int defout,i,count = 0;
if (month == JAN) {
sprintf(cal, "%s %d %d","/usr/bin/cal", month, year);
sprintf(preCal, "%s %d %d","/usr/bin/cal", DEC, year-1);
sprintf(nextCal, "%s %d %d","/usr/bin/cal", month+1, year);
}
else if(month == DEC){
sprintf(cal, "%s %d %d","/usr/bin/cal", month, year);
sprintf(preCal, "%s %d %d","/usr/bin/cal", month-1, year);
sprintf(nextCal, "%s %d %d","/usr/bin/cal", JAN, year+1);
}
else {
sprintf(cal, "%s %d %d","/usr/bin/cal", month, year);
sprintf(preCal, "%s %d %d","/usr/bin/cal", month-1, year);
sprintf(nextCal, "%s %d %d","/usr/bin/cal", month+1, year);
}
if (pipe(fd) == FALSE) { perror("pipe"); exit(EXIT_FAILURE); }
if (pipe(fd2) == FALSE) { perror("pipe"); exit(EXIT_FAILURE); }
if (pipe(fd3) == FALSE) { perror("pipe"); exit(EXIT_FAILURE); }
defout = dup(STDOUT_FILENO);//make copy of stdout
dup2(fd[1], STDOUT_FILENO);//makes fd[1] be the copy of stdout, closing stdout first if necessary.
if (system(preCal) == FALSE) { perror("system"); exit(EXIT_FAILURE); }
dup2(fd2[1], STDOUT_FILENO);
if (system(cal) == FALSE) { perror("system"); exit(EXIT_FAILURE); }
dup2(fd3[1], STDOUT_FILENO);
if (system(nextCal) == FALSE) { perror("system"); exit(EXIT_FAILURE); }
for(i = 0;i < NUM_OF_LINES_OF_MONTH;i++)
{
dup2(defout, STDOUT_FILENO);//bring back defout to STDOUT_FILENO
/*i have gained this while loop idea from Juhan Houang*/
while (read(fd[0], buf, 1) > 0 && strcmp(buf, "\n") != 0)
{
write(STDOUT_FILENO, buf, 1);
count++;
}
addSpaces(count);//if there needs to space, ie) last lines of months, add spaces accordingly
write(STDOUT_FILENO, "\t", 1);
count = 0;
while (read(fd2[0], buf, 1) > 0 && strcmp(buf, "\n") != 0)
{
write(STDOUT_FILENO, buf, 1);
count++;
}
addSpaces(count);
write(STDOUT_FILENO, "\t", 1);
count = 0;
while (read(fd3[0], buf, 1) > 0 && strcmp(buf, "\n") != 0)
{
write(STDOUT_FILENO, buf, 1);
count++;
}
count = 0;
write(STDOUT_FILENO, "\n", 1);
}
close(fd[0]);close(fd[1]);
close(fd2[0]);close(fd2[1]);
close(fd3[0]);close(fd3[1]);
}
void addSpaces(int count)
{
int i;
if(count < MAXSIZE)
{
for(i=0;i<MAXSIZE - count;i++)
write(STDOUT_FILENO," ", 1);
}
}