cal.c revision 1.5 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Kim Letkeman.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 char copyright[] =
39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40 All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 /*static char sccsid[] = "from: @(#)cal.c 5.2 (Berkeley) 4/19/91";*/
45 static char rcsid[] = "$Id: cal.c,v 1.5 1995/01/16 19:06:47 ws Exp $";
46 #endif /* not lint */
47
48 #include <sys/types.h>
49 #include <sys/time.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <ctype.h>
53
54 #define THURSDAY 4 /* for reformation */
55 #define SATURDAY 6 /* 1 Jan 1 was a Saturday */
56
57 #define FIRST_MISSING_DAY 639799 /* 3 Sep 1752 */
58 #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
59
60 #define MAXDAYS 42 /* max slots in a month array */
61 #define SPACE -1 /* used in day array */
62
63 static int days_in_month[2][13] = {
64 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
65 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
66 };
67
68 int sep1752[MAXDAYS] = {
69 SPACE, SPACE, 1, 2, 14, 15, 16,
70 17, 18, 19, 20, 21, 22, 23,
71 24, 25, 26, 27, 28, 29, 30,
72 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
73 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
74 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
75 }, j_sep1752[MAXDAYS] = {
76 SPACE, SPACE, 245, 246, 258, 259, 260,
77 261, 262, 263, 264, 265, 266, 267,
78 268, 269, 270, 271, 272, 273, 274,
79 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
80 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
81 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
82 }, empty[MAXDAYS] = {
83 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
84 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
85 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
86 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
87 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
88 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
89 };
90
91 char *month_names[12] = {
92 "January", "February", "March", "April", "May", "June",
93 "July", "August", "September", "October", "November", "December",
94 };
95
96 char *day_headings = " S M Tu W Th F S";
97 char *j_day_headings = " S M Tu W Th F S";
98
99 /* leap year -- account for gregorian reformation in 1752 */
100 #define leap_year(yr) \
101 ((yr) <= 1752 ? !((yr) % 4) : \
102 !((yr) % 4) && ((yr) % 100) || !((yr) % 400))
103
104 /* number of centuries since 1700, not inclusive */
105 #define centuries_since_1700(yr) \
106 ((yr) > 1700 ? (yr) / 100 - 17 : 0)
107
108 /* number of centuries since 1700 whose modulo of 400 is 0 */
109 #define quad_centuries_since_1700(yr) \
110 ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
111
112 /* number of leap years between year 1 and this year, not inclusive */
113 #define leap_years_since_year_1(yr) \
114 ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
115
116 int julian;
117
118 main(argc, argv)
119 int argc;
120 char **argv;
121 {
122 extern char *optarg;
123 extern int optind;
124 struct tm *local_time;
125 time_t now, time();
126 int ch, month, year, yflag;
127
128 yflag = 0;
129 while ((ch = getopt(argc, argv, "jy")) != EOF)
130 switch(ch) {
131 case 'j':
132 julian = 1;
133 break;
134 case 'y':
135 yflag = 1;
136 break;
137 case '?':
138 default:
139 usage();
140 }
141 argc -= optind;
142 argv += optind;
143
144 month = 0;
145 switch(argc) {
146 case 2:
147 if ((month = atoi(*argv++)) <= 0 || month > 12) {
148 (void)fprintf(stderr,
149 "cal: illegal month value: use 0-12\n");
150 exit(1);
151 }
152 /* FALLTHROUGH */
153 case 1:
154 if ((year = atoi(*argv)) <= 0 || year > 9999) {
155 (void)fprintf(stderr,
156 "cal: illegal year value: use 0-9999\n");
157 exit(1);
158 }
159 break;
160 case 0:
161 (void)time(&now);
162 local_time = localtime(&now);
163 year = local_time->tm_year + 1900;
164 if (!yflag)
165 month = local_time->tm_mon + 1;
166 break;
167 default:
168 usage();
169 }
170
171 if (month)
172 monthly(month, year);
173 else if (julian)
174 j_yearly(year);
175 else
176 yearly(year);
177 exit(0);
178 }
179
180 #define DAY_LEN 3 /* 3 spaces per day */
181 #define J_DAY_LEN 4 /* 4 spaces per day */
182 #define WEEK_LEN 20 /* 7 * 3 - one space at the end */
183 #define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */
184 #define HEAD_SEP 2 /* spaces between day headings */
185 #define J_HEAD_SEP 2
186
187 monthly(month, year)
188 int month, year;
189 {
190 register int col, row;
191 register char *p;
192 int len, days[MAXDAYS];
193 char lineout[30];
194
195 day_array(month, year, days);
196 len = sprintf(lineout, "%s %d", month_names[month - 1], year);
197 (void)printf("%*s%s\n%s\n",
198 ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
199 lineout, julian ? j_day_headings : day_headings);
200 for (row = 0; row < 6; row++) {
201 for (col = 0, p = lineout; col < 7; col++,
202 p += julian ? J_DAY_LEN : DAY_LEN)
203 ascii_day(p, days[row * 7 + col]);
204 *p = '\0';
205 trim_trailing_spaces(lineout);
206 (void)printf("%s\n", lineout);
207 }
208 }
209
210 j_yearly(year)
211 int year;
212 {
213 register int col, *dp, i, month, row, which_cal;
214 register char *p;
215 int days[12][MAXDAYS];
216 char lineout[80];
217
218 (void)sprintf(lineout, "%d", year);
219 center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
220 (void)printf("\n\n");
221 for (i = 0; i < 12; i++)
222 day_array(i + 1, year, days[i]);
223 (void)memset(lineout, ' ', sizeof(lineout) - 1);
224 lineout[sizeof(lineout) - 1] = '\0';
225 for (month = 0; month < 12; month += 2) {
226 center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
227 center(month_names[month + 1], J_WEEK_LEN, 0);
228 (void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
229 j_day_headings);
230 for (row = 0; row < 6; row++) {
231 for (which_cal = 0; which_cal < 2; which_cal++) {
232 p = lineout + which_cal * (J_WEEK_LEN + 2);
233 dp = &days[month + which_cal][row * 7];
234 for (col = 0; col < 7; col++, p += J_DAY_LEN)
235 ascii_day(p, *dp++);
236 }
237 *p = '\0';
238 trim_trailing_spaces(lineout);
239 (void)printf("%s\n", lineout);
240 }
241 }
242 (void)printf("\n");
243 }
244
245 yearly(year)
246 int year;
247 {
248 register int col, *dp, i, month, row, which_cal;
249 register char *p;
250 int days[12][MAXDAYS];
251 char lineout[80];
252
253 (void)sprintf(lineout, "%d", year);
254 center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
255 (void)printf("\n\n");
256 for (i = 0; i < 12; i++)
257 day_array(i + 1, year, days[i]);
258 (void)memset(lineout, ' ', sizeof(lineout) - 1);
259 lineout[sizeof(lineout) - 1] = '\0';
260 for (month = 0; month < 12; month += 3) {
261 center(month_names[month], WEEK_LEN, HEAD_SEP);
262 center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
263 center(month_names[month + 2], WEEK_LEN, 0);
264 (void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
265 "", day_headings, HEAD_SEP, "", day_headings);
266 for (row = 0; row < 6; row++) {
267 for (which_cal = 0; which_cal < 3; which_cal++) {
268 p = lineout + which_cal * (WEEK_LEN + 2);
269 dp = &days[month + which_cal][row * 7];
270 for (col = 0; col < 7; col++, p += DAY_LEN)
271 ascii_day(p, *dp++);
272 }
273 *p = '\0';
274 trim_trailing_spaces(lineout);
275 (void)printf("%s\n", lineout);
276 }
277 }
278 (void)printf("\n");
279 }
280
281 /*
282 * day_array --
283 * Fill in an array of 42 integers with a calendar. Assume for a moment
284 * that you took the (maximum) 6 rows in a calendar and stretched them
285 * out end to end. You would have 42 numbers or spaces. This routine
286 * builds that array for any month from Jan. 1 through Dec. 9999.
287 */
288 day_array(month, year, days)
289 register int *days;
290 int month, year;
291 {
292 register int i, day, dw, dm;
293
294 if (month == 9 && year == 1752) {
295 bcopy(julian ? j_sep1752 : sep1752,
296 days, MAXDAYS * sizeof(int));
297 return;
298 }
299 bcopy(empty, days, MAXDAYS * sizeof(int));
300 dm = days_in_month[leap_year(year)][month];
301 dw = day_in_week(1, month, year);
302 day = julian ? day_in_year(1, month, year) : 1;
303 while (dm--)
304 days[dw++] = day++;
305 }
306
307 /*
308 * day_in_year --
309 * return the 1 based day number within the year
310 */
311 day_in_year(day, month, year)
312 register int day, month;
313 int year;
314 {
315 register int i, leap;
316
317 leap = leap_year(year);
318 for (i = 1; i < month; i++)
319 day += days_in_month[leap][i];
320 return(day);
321 }
322
323 /*
324 * day_in_week
325 * return the 0 based day number for any date from 1 Jan. 1 to
326 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
327 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
328 * missing days.
329 */
330 day_in_week(day, month, year)
331 int day, month, year;
332 {
333 long temp;
334
335 temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
336 + day_in_year(day, month, year);
337 if (temp < FIRST_MISSING_DAY)
338 return((temp - 1 + SATURDAY) % 7);
339 if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
340 return(((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
341 return(THURSDAY);
342 }
343
344 ascii_day(p, day)
345 register char *p;
346 register int day;
347 {
348 register int display, val;
349 static char *aday[] = {
350 "",
351 " 1", " 2", " 3", " 4", " 5", " 6", " 7",
352 " 8", " 9", "10", "11", "12", "13", "14",
353 "15", "16", "17", "18", "19", "20", "21",
354 "22", "23", "24", "25", "26", "27", "28",
355 "29", "30", "31",
356 };
357
358 if (day == SPACE) {
359 memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
360 return;
361 }
362 if (julian) {
363 if (val = day / 100) {
364 day %= 100;
365 *p++ = val + '0';
366 display = 1;
367 } else {
368 *p++ = ' ';
369 display = 0;
370 }
371 val = day / 10;
372 if (val || display)
373 *p++ = val + '0';
374 else
375 *p++ = ' ';
376 *p++ = day % 10 + '0';
377 } else {
378 *p++ = aday[day][0];
379 *p++ = aday[day][1];
380 }
381 *p = ' ';
382 }
383
384 trim_trailing_spaces(s)
385 register char *s;
386 {
387 register char *p;
388
389 for (p = s; *p; ++p);
390 while (p > s && isspace(*--p));
391 if (p > s)
392 ++p;
393 *p = '\0';
394 }
395
396 center(str, len, separate)
397 char *str;
398 register int len;
399 int separate;
400 {
401 len -= strlen(str);
402 (void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
403 if (separate)
404 (void)printf("%*s", separate, "");
405 }
406
407 usage()
408 {
409 (void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n");
410 exit(1);
411 }
412