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