Annotation of src/usr.bin/cal/cal.c, Revision 1.22
1.22 ! pyr 1: /* $OpenBSD: cal.c,v 1.21 2006/10/29 22:51:09 tom 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
1.13 tedu 37: static const char copyright[] =
1.1 deraadt 38: "@(#) Copyright (c) 1989, 1993, 1994\n\
39: The Regents of the University of California. All rights reserved.\n";
40: #if 0
41: static char sccsid[] = "@(#)cal.c 8.4 (Berkeley) 4/2/94";
42: #else
1.22 ! pyr 43: static const char rcsid[] = "$OpenBSD: cal.c,v 1.21 2006/10/29 22:51:09 tom Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
47: #include <sys/types.h>
48:
49: #include <ctype.h>
50: #include <err.h>
51: #include <stdio.h>
52: #include <stdlib.h>
53: #include <string.h>
54: #include <time.h>
1.5 deraadt 55: #include <tzfile.h>
1.1 deraadt 56: #include <unistd.h>
57:
58: #define THURSDAY 4 /* for reformation */
1.14 deraadt 59: #define SATURDAY 6 /* 1 Jan 1 was a Saturday */
1.1 deraadt 60:
1.14 deraadt 61: #define FIRST_MISSING_DAY 639799 /* 3 Sep 1752 */
62: #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
1.1 deraadt 63:
64: #define MAXDAYS 42 /* max slots in a month array */
65: #define SPACE -1 /* used in day array */
66:
1.13 tedu 67: static const int days_in_month[2][13] = {
1.1 deraadt 68: {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
69: {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
70: };
71:
1.22 ! pyr 72: const int sep1752s[MAXDAYS] = {
1.1 deraadt 73: SPACE, SPACE, 1, 2, 14, 15, 16,
74: 17, 18, 19, 20, 21, 22, 23,
75: 24, 25, 26, 27, 28, 29, 30,
76: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
77: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
78: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
1.22 ! pyr 79: }, sep1752m[MAXDAYS] = {
! 80: SPACE, 1, 2, 14, 15, 16, 17,
! 81: 18, 19, 20, 21, 22, 23, 24,
! 82: 25, 26, 27, 28, 29, 30, SPACE,
! 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: }, sep1752js[MAXDAYS] = {
1.1 deraadt 87: SPACE, SPACE, 245, 246, 258, 259, 260,
88: 261, 262, 263, 264, 265, 266, 267,
89: 268, 269, 270, 271, 272, 273, 274,
90: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
91: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
92: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
1.22 ! pyr 93: }, sep1752jm[MAXDAYS] = {
! 94: SPACE, 245, 246, 258, 259, 260, 261,
! 95: 262, 263, 264, 265, 266, 267, 268,
! 96: 269, 270, 271, 272, 273, 274, SPACE,
! 97: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
! 98: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
! 99: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
1.1 deraadt 100: }, empty[MAXDAYS] = {
101: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
102: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
103: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
104: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
105: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
106: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
107: };
108:
1.13 tedu 109: const char *month_names[12] = {
1.1 deraadt 110: "January", "February", "March", "April", "May", "June",
111: "July", "August", "September", "October", "November", "December",
112: };
113:
1.22 ! pyr 114: #define DAY_HEADINGS_S "Su Mo Tu We Th Fr Sa"
! 115: #define DAY_HEADINGS_M "Mo Tu We Th Fr Sa Su"
! 116: #define DAY_HEADINGS_JS " Su Mo Tu We Th Fr Sa"
! 117: #define DAY_HEADINGS_JM " Mo Tu We Th Fr Sa Su"
! 118:
! 119: const int *sep1752 = NULL;
! 120: const char *day_headings = NULL;
1.1 deraadt 121:
122: /* leap year -- account for gregorian reformation in 1752 */
123: #define leap_year(yr) \
124: ((yr) <= 1752 ? !((yr) % 4) : \
1.5 deraadt 125: (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
1.1 deraadt 126:
127: /* number of centuries since 1700, not inclusive */
128: #define centuries_since_1700(yr) \
129: ((yr) > 1700 ? (yr) / 100 - 17 : 0)
130:
131: /* number of centuries since 1700 whose modulo of 400 is 0 */
132: #define quad_centuries_since_1700(yr) \
133: ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
134:
135: /* number of leap years between year 1 and this year, not inclusive */
136: #define leap_years_since_year_1(yr) \
137: ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
138:
139: int julian;
1.22 ! pyr 140: int mflag = 0;
1.1 deraadt 141:
1.7 millert 142: void ascii_day(char *, int);
1.13 tedu 143: void center(const char *, int, int);
1.7 millert 144: void day_array(int, int, int *);
145: int day_in_week(int, int, int);
146: int day_in_year(int, int, int);
147: void j_yearly(int);
148: void monthly(int, int);
149: void trim_trailing_spaces(char *);
150: void usage(void);
151: void yearly(int);
1.13 tedu 152: int parsemonth(const char *);
1.1 deraadt 153:
154: int
1.10 deraadt 155: main(int argc, char *argv[])
1.1 deraadt 156: {
157: struct tm *local_time;
158: time_t now;
159: int ch, month, year, yflag;
1.20 tedu 160: const char *errstr;
1.1 deraadt 161:
1.6 deraadt 162: yflag = year = 0;
1.22 ! pyr 163: while ((ch = getopt(argc, argv, "jmy")) != -1)
1.1 deraadt 164: switch(ch) {
165: case 'j':
166: julian = 1;
167: break;
1.22 ! pyr 168: case 'm':
! 169: mflag = 1;
! 170: break;
1.1 deraadt 171: case 'y':
172: yflag = 1;
173: break;
174: case '?':
175: default:
176: usage();
177: }
178: argc -= optind;
179: argv += optind;
180:
1.22 ! pyr 181: day_headings = DAY_HEADINGS_S;
! 182: sep1752 = sep1752s;
! 183: if (mflag && julian) {
! 184: sep1752 = sep1752jm;
! 185: day_headings = DAY_HEADINGS_JM;
! 186: } else if (mflag) {
! 187: sep1752 = sep1752m;
! 188: day_headings = DAY_HEADINGS_M;
! 189: } else if (julian) {
! 190: sep1752 = sep1752js;
! 191: day_headings = DAY_HEADINGS_JS;
! 192: }
! 193:
1.1 deraadt 194: month = 0;
195: switch(argc) {
196: case 2:
1.12 tedu 197: month = parsemonth(*argv++);
198: if (!month)
199: errx(1, "Unable to parse month");
1.1 deraadt 200: /* FALLTHROUGH */
201: case 1:
1.12 tedu 202: if (argc == 1 && !isdigit(*argv[0])) {
203: month = parsemonth(*argv);
204: if (!month)
205: errx(1, "illegal year value: use 1-9999");
206: (void)time(&now);
207: local_time = localtime(&now);
208: year = local_time->tm_year + TM_YEAR_BASE;
209: } else {
1.20 tedu 210: year = strtonum(*argv, 1, 9999, &errstr);
211: if (errstr)
1.12 tedu 212: errx(1, "illegal year value: use 1-9999");
213: }
1.1 deraadt 214: break;
215: case 0:
216: (void)time(&now);
217: local_time = localtime(&now);
1.5 deraadt 218: year = local_time->tm_year + TM_YEAR_BASE;
1.1 deraadt 219: if (!yflag)
220: month = local_time->tm_mon + 1;
221: break;
222: default:
223: usage();
224: }
225:
226: if (month)
227: monthly(month, year);
228: else if (julian)
229: j_yearly(year);
230: else
231: yearly(year);
232: exit(0);
233: }
234:
235: #define DAY_LEN 3 /* 3 spaces per day */
236: #define J_DAY_LEN 4 /* 4 spaces per day */
237: #define WEEK_LEN 20 /* 7 * 3 - one space at the end */
238: #define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */
239: #define HEAD_SEP 2 /* spaces between day headings */
240: #define J_HEAD_SEP 2
241:
242: void
1.10 deraadt 243: monthly(int month, int year)
1.1 deraadt 244: {
245: int col, row, len, days[MAXDAYS];
246: char *p, lineout[30];
247:
248: day_array(month, year, days);
1.19 ray 249: (void)snprintf(lineout, sizeof(lineout), "%s %d",
1.8 deraadt 250: month_names[month - 1], year);
1.11 deraadt 251: len = strlen(lineout);
1.1 deraadt 252: (void)printf("%*s%s\n%s\n",
253: ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
1.22 ! pyr 254: lineout, day_headings);
1.1 deraadt 255: for (row = 0; row < 6; row++) {
256: for (col = 0, p = lineout; col < 7; col++,
257: p += julian ? J_DAY_LEN : DAY_LEN)
258: ascii_day(p, days[row * 7 + col]);
259: *p = '\0';
260: trim_trailing_spaces(lineout);
261: (void)printf("%s\n", lineout);
262: }
263: }
264:
265: void
1.10 deraadt 266: j_yearly(int year)
1.1 deraadt 267: {
268: int col, *dp, i, month, row, which_cal;
269: int days[12][MAXDAYS];
270: char *p, lineout[80];
271:
1.19 ray 272: (void)snprintf(lineout, sizeof(lineout), "%d", year);
1.1 deraadt 273: center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
274: (void)printf("\n\n");
275: for (i = 0; i < 12; i++)
276: day_array(i + 1, year, days[i]);
277: (void)memset(lineout, ' ', sizeof(lineout) - 1);
278: lineout[sizeof(lineout) - 1] = '\0';
279: for (month = 0; month < 12; month += 2) {
280: center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
281: center(month_names[month + 1], J_WEEK_LEN, 0);
1.22 ! pyr 282: (void)printf("\n%s%*s%s\n", day_headings,
! 283: J_HEAD_SEP, "", day_headings);
! 284:
1.1 deraadt 285: for (row = 0; row < 6; row++) {
286: for (which_cal = 0; which_cal < 2; which_cal++) {
287: p = lineout + which_cal * (J_WEEK_LEN + 2);
288: dp = &days[month + which_cal][row * 7];
289: for (col = 0; col < 7; col++, p += J_DAY_LEN)
290: ascii_day(p, *dp++);
291: }
292: *p = '\0';
293: trim_trailing_spaces(lineout);
294: (void)printf("%s\n", lineout);
295: }
296: }
297: (void)printf("\n");
298: }
299:
300: void
1.10 deraadt 301: yearly(int year)
1.1 deraadt 302: {
303: int col, *dp, i, month, row, which_cal;
304: int days[12][MAXDAYS];
305: char *p, lineout[80];
306:
1.19 ray 307: (void)snprintf(lineout, sizeof(lineout), "%d", year);
1.1 deraadt 308: center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
309: (void)printf("\n\n");
310: for (i = 0; i < 12; i++)
311: day_array(i + 1, year, days[i]);
312: (void)memset(lineout, ' ', sizeof(lineout) - 1);
313: lineout[sizeof(lineout) - 1] = '\0';
314: for (month = 0; month < 12; month += 3) {
315: center(month_names[month], WEEK_LEN, HEAD_SEP);
316: center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
317: center(month_names[month + 2], WEEK_LEN, 0);
1.22 ! pyr 318: (void)printf("\n%s%*s%s%*s%s\n", day_headings,
! 319: HEAD_SEP, "", day_headings, HEAD_SEP, "", day_headings);
! 320:
1.1 deraadt 321: for (row = 0; row < 6; row++) {
322: for (which_cal = 0; which_cal < 3; which_cal++) {
323: p = lineout + which_cal * (WEEK_LEN + 2);
324: dp = &days[month + which_cal][row * 7];
325: for (col = 0; col < 7; col++, p += DAY_LEN)
326: ascii_day(p, *dp++);
327: }
328: *p = '\0';
329: trim_trailing_spaces(lineout);
330: (void)printf("%s\n", lineout);
331: }
332: }
333: (void)printf("\n");
334: }
335:
336: /*
337: * day_array --
338: * Fill in an array of 42 integers with a calendar. Assume for a moment
339: * that you took the (maximum) 6 rows in a calendar and stretched them
340: * out end to end. You would have 42 numbers or spaces. This routine
341: * builds that array for any month from Jan. 1 through Dec. 9999.
342: */
343: void
1.10 deraadt 344: day_array(int month, int year, int *days)
1.1 deraadt 345: {
346: int day, dw, dm;
347:
348: if (month == 9 && year == 1752) {
1.22 ! pyr 349: memmove(days, sep1752, MAXDAYS * sizeof(int));
1.1 deraadt 350: return;
351: }
352: memmove(days, empty, MAXDAYS * sizeof(int));
353: dm = days_in_month[leap_year(year)][month];
1.22 ! pyr 354: dw = day_in_week(mflag?0:1, month, year);
1.1 deraadt 355: day = julian ? day_in_year(1, month, year) : 1;
356: while (dm--)
357: days[dw++] = day++;
358: }
359:
360: /*
361: * day_in_year --
362: * return the 1 based day number within the year
363: */
364: int
1.10 deraadt 365: day_in_year(int day, int month, int year)
1.1 deraadt 366: {
367: int i, leap;
368:
369: leap = leap_year(year);
370: for (i = 1; i < month; i++)
371: day += days_in_month[leap][i];
372: return (day);
373: }
374:
375: /*
376: * day_in_week
377: * return the 0 based day number for any date from 1 Jan. 1 to
378: * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
379: * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
380: * missing days.
381: */
382: int
1.10 deraadt 383: day_in_week(int day, int month, int year)
1.1 deraadt 384: {
385: long temp;
386:
387: temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
388: + day_in_year(day, month, year);
389: if (temp < FIRST_MISSING_DAY)
390: return ((temp - 1 + SATURDAY) % 7);
391: if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
392: return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
393: return (THURSDAY);
394: }
395:
396: void
1.10 deraadt 397: ascii_day(char *p, int day)
1.1 deraadt 398: {
399: int display, val;
1.13 tedu 400: static const char *aday[] = {
1.1 deraadt 401: "",
402: " 1", " 2", " 3", " 4", " 5", " 6", " 7",
403: " 8", " 9", "10", "11", "12", "13", "14",
404: "15", "16", "17", "18", "19", "20", "21",
405: "22", "23", "24", "25", "26", "27", "28",
406: "29", "30", "31",
407: };
408:
409: if (day == SPACE) {
410: memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
411: return;
412: }
413: if (julian) {
1.5 deraadt 414: val = day / 100;
415: if (val) {
1.1 deraadt 416: day %= 100;
417: *p++ = val + '0';
418: display = 1;
419: } else {
420: *p++ = ' ';
421: display = 0;
422: }
423: val = day / 10;
424: if (val || display)
425: *p++ = val + '0';
426: else
427: *p++ = ' ';
428: *p++ = day % 10 + '0';
429: } else {
430: *p++ = aday[day][0];
431: *p++ = aday[day][1];
432: }
433: *p = ' ';
434: }
435:
436: void
1.10 deraadt 437: trim_trailing_spaces(char *s)
1.1 deraadt 438: {
439: char *p;
440:
441: for (p = s; *p; ++p)
442: continue;
443: while (p > s && isspace(*--p))
444: continue;
445: if (p > s)
446: ++p;
447: *p = '\0';
448: }
449:
450: void
1.13 tedu 451: center(const char *str, int len, int separate)
1.1 deraadt 452: {
453:
454: len -= strlen(str);
1.21 tom 455: (void)printf("%*s%s%*s", len / 2, "", str,
456: len / 2 + len % 2 + separate, "");
1.1 deraadt 457: }
458:
459: void
1.10 deraadt 460: usage(void)
1.1 deraadt 461: {
462:
1.22 ! pyr 463: (void)fprintf(stderr, "usage: cal [-jmy] [month] [year]\n");
1.1 deraadt 464: exit(1);
1.12 tedu 465: }
466:
467: int
468: parsemonth(const char *s)
469: {
1.15 deraadt 470: struct tm tm;
471: char *cp;
1.12 tedu 472: int v;
473:
474: v = (int)strtol(s, &cp, 10);
1.17 tom 475: if (*cp != '\0') { /* s wasn't purely numeric */
476: v = 0;
477: if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
478: v = tm.tm_mon + 1;
479: }
1.15 deraadt 480: if (v <= 0 || v > 12)
1.17 tom 481: errx(1, "invalid month: use 1-12 or a name");
1.15 deraadt 482: return (v);
1.1 deraadt 483: }