Annotation of src/usr.bin/cal/cal.c, Revision 1.11
1.11 ! deraadt 1: /* $OpenBSD: cal.c,v 1.10 2003/06/10 22:20:45 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.11 ! deraadt 46: static char rcsid[] = "$OpenBSD: cal.c,v 1.10 2003/06/10 22:20:45 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:
162: if ((month = atoi(*argv++)) < 1 || month > 12)
163: errx(1, "illegal month value: use 1-12");
164: /* FALLTHROUGH */
165: case 1:
166: if ((year = atoi(*argv)) < 1 || year > 9999)
167: errx(1, "illegal year value: use 1-9999");
168: break;
169: case 0:
170: (void)time(&now);
171: local_time = localtime(&now);
1.5 deraadt 172: year = local_time->tm_year + TM_YEAR_BASE;
1.1 deraadt 173: if (!yflag)
174: month = local_time->tm_mon + 1;
175: break;
176: default:
177: usage();
178: }
179:
180: if (month)
181: monthly(month, year);
182: else if (julian)
183: j_yearly(year);
184: else
185: yearly(year);
186: exit(0);
187: }
188:
189: #define DAY_LEN 3 /* 3 spaces per day */
190: #define J_DAY_LEN 4 /* 4 spaces per day */
191: #define WEEK_LEN 20 /* 7 * 3 - one space at the end */
192: #define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */
193: #define HEAD_SEP 2 /* spaces between day headings */
194: #define J_HEAD_SEP 2
195:
196: void
1.10 deraadt 197: monthly(int month, int year)
1.1 deraadt 198: {
199: int col, row, len, days[MAXDAYS];
200: char *p, lineout[30];
201:
202: day_array(month, year, days);
1.11 ! deraadt 203: (void) snprintf(lineout, sizeof lineout, "%s %d",
1.8 deraadt 204: month_names[month - 1], year);
1.11 ! deraadt 205: len = strlen(lineout);
1.1 deraadt 206: (void)printf("%*s%s\n%s\n",
207: ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
208: lineout, julian ? j_day_headings : day_headings);
209: for (row = 0; row < 6; row++) {
210: for (col = 0, p = lineout; col < 7; col++,
211: p += julian ? J_DAY_LEN : DAY_LEN)
212: ascii_day(p, days[row * 7 + col]);
213: *p = '\0';
214: trim_trailing_spaces(lineout);
215: (void)printf("%s\n", lineout);
216: }
217: }
218:
219: void
1.10 deraadt 220: j_yearly(int year)
1.1 deraadt 221: {
222: int col, *dp, i, month, row, which_cal;
223: int days[12][MAXDAYS];
224: char *p, lineout[80];
225:
1.8 deraadt 226: (void)snprintf(lineout, sizeof lineout, "%d", year);
1.1 deraadt 227: center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
228: (void)printf("\n\n");
229: for (i = 0; i < 12; i++)
230: day_array(i + 1, year, days[i]);
231: (void)memset(lineout, ' ', sizeof(lineout) - 1);
232: lineout[sizeof(lineout) - 1] = '\0';
233: for (month = 0; month < 12; month += 2) {
234: center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
235: center(month_names[month + 1], J_WEEK_LEN, 0);
236: (void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
237: j_day_headings);
238: for (row = 0; row < 6; row++) {
239: for (which_cal = 0; which_cal < 2; which_cal++) {
240: p = lineout + which_cal * (J_WEEK_LEN + 2);
241: dp = &days[month + which_cal][row * 7];
242: for (col = 0; col < 7; col++, p += J_DAY_LEN)
243: ascii_day(p, *dp++);
244: }
245: *p = '\0';
246: trim_trailing_spaces(lineout);
247: (void)printf("%s\n", lineout);
248: }
249: }
250: (void)printf("\n");
251: }
252:
253: void
1.10 deraadt 254: yearly(int year)
1.1 deraadt 255: {
256: int col, *dp, i, month, row, which_cal;
257: int days[12][MAXDAYS];
258: char *p, lineout[80];
259:
1.8 deraadt 260: (void)snprintf(lineout, sizeof lineout, "%d", year);
1.1 deraadt 261: center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
262: (void)printf("\n\n");
263: for (i = 0; i < 12; i++)
264: day_array(i + 1, year, days[i]);
265: (void)memset(lineout, ' ', sizeof(lineout) - 1);
266: lineout[sizeof(lineout) - 1] = '\0';
267: for (month = 0; month < 12; month += 3) {
268: center(month_names[month], WEEK_LEN, HEAD_SEP);
269: center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
270: center(month_names[month + 2], WEEK_LEN, 0);
271: (void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
272: "", day_headings, HEAD_SEP, "", day_headings);
273: for (row = 0; row < 6; row++) {
274: for (which_cal = 0; which_cal < 3; which_cal++) {
275: p = lineout + which_cal * (WEEK_LEN + 2);
276: dp = &days[month + which_cal][row * 7];
277: for (col = 0; col < 7; col++, p += DAY_LEN)
278: ascii_day(p, *dp++);
279: }
280: *p = '\0';
281: trim_trailing_spaces(lineout);
282: (void)printf("%s\n", lineout);
283: }
284: }
285: (void)printf("\n");
286: }
287:
288: /*
289: * day_array --
290: * Fill in an array of 42 integers with a calendar. Assume for a moment
291: * that you took the (maximum) 6 rows in a calendar and stretched them
292: * out end to end. You would have 42 numbers or spaces. This routine
293: * builds that array for any month from Jan. 1 through Dec. 9999.
294: */
295: void
1.10 deraadt 296: day_array(int month, int year, int *days)
1.1 deraadt 297: {
298: int day, dw, dm;
299:
300: if (month == 9 && year == 1752) {
301: memmove(days,
302: julian ? j_sep1752 : sep1752, MAXDAYS * sizeof(int));
303: return;
304: }
305: memmove(days, empty, MAXDAYS * sizeof(int));
306: dm = days_in_month[leap_year(year)][month];
307: dw = day_in_week(1, month, year);
308: day = julian ? day_in_year(1, month, year) : 1;
309: while (dm--)
310: days[dw++] = day++;
311: }
312:
313: /*
314: * day_in_year --
315: * return the 1 based day number within the year
316: */
317: int
1.10 deraadt 318: day_in_year(int day, int month, int year)
1.1 deraadt 319: {
320: int i, leap;
321:
322: leap = leap_year(year);
323: for (i = 1; i < month; i++)
324: day += days_in_month[leap][i];
325: return (day);
326: }
327:
328: /*
329: * day_in_week
330: * return the 0 based day number for any date from 1 Jan. 1 to
331: * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
332: * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
333: * missing days.
334: */
335: int
1.10 deraadt 336: day_in_week(int day, int month, int year)
1.1 deraadt 337: {
338: long temp;
339:
340: temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
341: + day_in_year(day, month, year);
342: if (temp < FIRST_MISSING_DAY)
343: return ((temp - 1 + SATURDAY) % 7);
344: if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
345: return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
346: return (THURSDAY);
347: }
348:
349: void
1.10 deraadt 350: ascii_day(char *p, int day)
1.1 deraadt 351: {
352: int display, val;
353: static char *aday[] = {
354: "",
355: " 1", " 2", " 3", " 4", " 5", " 6", " 7",
356: " 8", " 9", "10", "11", "12", "13", "14",
357: "15", "16", "17", "18", "19", "20", "21",
358: "22", "23", "24", "25", "26", "27", "28",
359: "29", "30", "31",
360: };
361:
362: if (day == SPACE) {
363: memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
364: return;
365: }
366: if (julian) {
1.5 deraadt 367: val = day / 100;
368: if (val) {
1.1 deraadt 369: day %= 100;
370: *p++ = val + '0';
371: display = 1;
372: } else {
373: *p++ = ' ';
374: display = 0;
375: }
376: val = day / 10;
377: if (val || display)
378: *p++ = val + '0';
379: else
380: *p++ = ' ';
381: *p++ = day % 10 + '0';
382: } else {
383: *p++ = aday[day][0];
384: *p++ = aday[day][1];
385: }
386: *p = ' ';
387: }
388:
389: void
1.10 deraadt 390: trim_trailing_spaces(char *s)
1.1 deraadt 391: {
392: char *p;
393:
394: for (p = s; *p; ++p)
395: continue;
396: while (p > s && isspace(*--p))
397: continue;
398: if (p > s)
399: ++p;
400: *p = '\0';
401: }
402:
403: void
1.10 deraadt 404: center(char *str, int len, int separate)
1.1 deraadt 405: {
406:
407: len -= strlen(str);
408: (void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
409: if (separate)
410: (void)printf("%*s", separate, "");
411: }
412:
413: void
1.10 deraadt 414: usage(void)
1.1 deraadt 415: {
416:
417: (void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n");
418: exit(1);
419: }