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