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