Annotation of src/usr.bin/cal/cal.c, Revision 1.20
1.20 ! tedu 1: /* $OpenBSD: cal.c,v 1.19 2006/03/24 03:44:14 ray 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.20 ! tedu 43: static const char rcsid[] = "$OpenBSD: cal.c,v 1.19 2006/03/24 03:44:14 ray 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.13 tedu 72: const int sep1752[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,
79: }, j_sep1752[MAXDAYS] = {
80: SPACE, SPACE, 245, 246, 258, 259, 260,
81: 261, 262, 263, 264, 265, 266, 267,
82: 268, 269, 270, 271, 272, 273, 274,
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: }, empty[MAXDAYS] = {
87: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
88: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
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: SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
93: };
94:
1.13 tedu 95: const char *month_names[12] = {
1.1 deraadt 96: "January", "February", "March", "April", "May", "June",
97: "July", "August", "September", "October", "November", "December",
98: };
99:
1.13 tedu 100: const char *day_headings = "Su Mo Tu We Th Fr Sa";
101: const char *j_day_headings = " Su Mo Tu We Th Fr Sa";
1.1 deraadt 102:
103: /* leap year -- account for gregorian reformation in 1752 */
104: #define leap_year(yr) \
105: ((yr) <= 1752 ? !((yr) % 4) : \
1.5 deraadt 106: (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
1.1 deraadt 107:
108: /* number of centuries since 1700, not inclusive */
109: #define centuries_since_1700(yr) \
110: ((yr) > 1700 ? (yr) / 100 - 17 : 0)
111:
112: /* number of centuries since 1700 whose modulo of 400 is 0 */
113: #define quad_centuries_since_1700(yr) \
114: ((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
115:
116: /* number of leap years between year 1 and this year, not inclusive */
117: #define leap_years_since_year_1(yr) \
118: ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
119:
120: int julian;
121:
1.7 millert 122: void ascii_day(char *, int);
1.13 tedu 123: void center(const char *, int, int);
1.7 millert 124: void day_array(int, int, int *);
125: int day_in_week(int, int, int);
126: int day_in_year(int, int, int);
127: void j_yearly(int);
128: void monthly(int, int);
129: void trim_trailing_spaces(char *);
130: void usage(void);
131: void yearly(int);
1.13 tedu 132: int parsemonth(const char *);
1.1 deraadt 133:
134: int
1.10 deraadt 135: main(int argc, char *argv[])
1.1 deraadt 136: {
137: struct tm *local_time;
138: time_t now;
139: int ch, month, year, yflag;
1.20 ! tedu 140: const char *errstr;
1.1 deraadt 141:
1.6 deraadt 142: yflag = year = 0;
1.4 millert 143: while ((ch = getopt(argc, argv, "jy")) != -1)
1.1 deraadt 144: switch(ch) {
145: case 'j':
146: julian = 1;
147: break;
148: case 'y':
149: yflag = 1;
150: break;
151: case '?':
152: default:
153: usage();
154: }
155: argc -= optind;
156: argv += optind;
157:
158: month = 0;
159: switch(argc) {
160: case 2:
1.12 tedu 161: month = parsemonth(*argv++);
162: if (!month)
163: errx(1, "Unable to parse month");
1.1 deraadt 164: /* FALLTHROUGH */
165: case 1:
1.12 tedu 166: if (argc == 1 && !isdigit(*argv[0])) {
167: month = parsemonth(*argv);
168: if (!month)
169: errx(1, "illegal year value: use 1-9999");
170: (void)time(&now);
171: local_time = localtime(&now);
172: year = local_time->tm_year + TM_YEAR_BASE;
173: } else {
1.20 ! tedu 174: year = strtonum(*argv, 1, 9999, &errstr);
! 175: if (errstr)
1.12 tedu 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.19 ray 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.19 ray 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.19 ray 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,
1.13 tedu 312: julian ? j_sep1752 : sep1752, MAXDAYS * sizeof(int));
1.1 deraadt 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;
1.13 tedu 363: static const char *aday[] = {
1.1 deraadt 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.13 tedu 414: center(const 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:
1.18 jmc 427: (void)fprintf(stderr, "usage: cal [-jy] [month] [year]\n");
1.1 deraadt 428: exit(1);
1.12 tedu 429: }
430:
431: int
432: parsemonth(const char *s)
433: {
1.15 deraadt 434: struct tm tm;
435: char *cp;
1.12 tedu 436: int v;
437:
438: v = (int)strtol(s, &cp, 10);
1.17 tom 439: if (*cp != '\0') { /* s wasn't purely numeric */
440: v = 0;
441: if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
442: v = tm.tm_mon + 1;
443: }
1.15 deraadt 444: if (v <= 0 || v > 12)
1.17 tom 445: errx(1, "invalid month: use 1-12 or a name");
1.15 deraadt 446: return (v);
1.1 deraadt 447: }