1 /* $OpenBSD: cal.c,v 1.32 2024/08/18 19:58:35 deraadt Exp $ */
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. Neither the name of the University nor the names of its contributors
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 #include <sys/types.h>
37
38 #include <ctype.h>
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45
46 #define THURSDAY 4 /* for reformation */
47 #define SATURDAY 6 /* 1 Jan 1 was a Saturday */
48
49 #define FIRST_MISSING_DAY 639799 /* 3 Sep 1752 */
50 #define NUMBER_MISSING_DAYS 11 /* 11 day correction */
51
52 #define MAXDAYS 42 /* max slots in a month array */
53 #define SPACE -1 /* used in day array */
54
55 static const int days_in_month[2][13] = {
56 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
57 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
58 };
59
60 const int sep1752s[MAXDAYS] = {
61 SPACE, SPACE, 1, 2, 14, 15, 16,
62 17, 18, 19, 20, 21, 22, 23,
63 24, 25, 26, 27, 28, 29, 30,
64 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
65 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
66 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
67 }, sep1752m[MAXDAYS] = {
68 SPACE, 1, 2, 14, 15, 16, 17,
69 18, 19, 20, 21, 22, 23, 24,
70 25, 26, 27, 28, 29, 30, SPACE,
71 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
72 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
73 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
74 }, sep1752js[MAXDAYS] = {
75 SPACE, SPACE, 245, 246, 258, 259, 260,
76 261, 262, 263, 264, 265, 266, 267,
77 268, 269, 270, 271, 272, 273, 274,
78 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
79 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
80 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
81 }, sep1752jm[MAXDAYS] = {
82 SPACE, 245, 246, 258, 259, 260, 261,
83 262, 263, 264, 265, 266, 267, 268,
84 269, 270, 271, 272, 273, 274, SPACE,
85 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
86 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
87 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
88 }, empty[MAXDAYS] = {
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 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
94 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
95 };
96
97 const char *month_names[12] = {
98 "January", "February", "March", "April", "May", "June",
99 "July", "August", "September", "October", "November", "December",
100 };
101
102 #define DAY_HEADINGS_S "Su Mo Tu We Th Fr Sa"
103 #define DAY_HEADINGS_M "Mo Tu We Th Fr Sa Su"
104 #define DAY_HEADINGS_JS " Su Mo Tu We Th Fr Sa"
105 #define DAY_HEADINGS_JM " Mo Tu We Th Fr Sa Su"
106
107 const int *sep1752 = NULL;
108 const char *day_headings = NULL;
109
110 /* leap year -- account for gregorian reformation in 1752 */
111 #define leap_year(yr) \
112 ((yr) <= 1752 ? !((yr) % 4) : \
113 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
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 int mflag = 0;
129 int wflag = 0;
130
131 void ascii_day(char *, int);
132 void center(const char *, int, int);
133 void day_array(int, int, int *);
134 int day_in_week(int, int, int);
135 int day_in_year(int, int, int);
136 int week(int, int, int);
137 int isoweek(int, int, int);
138 void j_yearly(int);
139 void monthly(int, int);
140 void trim_trailing_spaces(char *);
141 void usage(void);
142 void yearly(int);
143 int parsemonth(const char *);
144
145 int
main(int argc,char * argv[])146 main(int argc, char *argv[])
147 {
148 struct tm *local_time;
149 time_t now;
150 int ch, month, year, yflag;
151 const char *errstr;
152
153 if (pledge("stdio", NULL) == -1)
154 err(1, "pledge");
155
156 yflag = year = 0;
157 while ((ch = getopt(argc, argv, "jmwy")) != -1)
158 switch(ch) {
159 case 'j':
160 julian = 1;
161 break;
162 case 'm':
163 mflag = 1;
164 break;
165 case 'w':
166 wflag = 1;
167 break;
168 case 'y':
169 yflag = 1;
170 break;
171 default:
172 usage();
173 }
174 argc -= optind;
175 argv += optind;
176
177 if (julian && wflag)
178 usage();
179
180 day_headings = DAY_HEADINGS_S;
181 sep1752 = sep1752s;
182 if (mflag && julian) {
183 sep1752 = sep1752jm;
184 day_headings = DAY_HEADINGS_JM;
185 } else if (mflag) {
186 sep1752 = sep1752m;
187 day_headings = DAY_HEADINGS_M;
188 } else if (julian) {
189 sep1752 = sep1752js;
190 day_headings = DAY_HEADINGS_JS;
191 }
192
193 month = 0;
194 switch(argc) {
195 case 2:
196 month = parsemonth(*argv++);
197 if (!month)
198 errx(1, "Unable to parse month");
199 /* FALLTHROUGH */
200 case 1:
201 if (argc == 1 && !isdigit((unsigned char)*argv[0])) {
202 month = parsemonth(*argv);
203 if (!month)
204 errx(1, "illegal year value: use 1-9999");
205 (void)time(&now);
206 local_time = localtime(&now);
207 year = local_time->tm_year + 1900;
208 } else {
209 year = strtonum(*argv, 1, 9999, &errstr);
210 if (errstr)
211 errx(1, "illegal year value: use 1-9999");
212 }
213 break;
214 case 0:
215 (void)time(&now);
216 local_time = localtime(&now);
217 year = local_time->tm_year + 1900;
218 if (!yflag)
219 month = local_time->tm_mon + 1;
220 break;
221 default:
222 usage();
223 }
224
225 if (month)
226 monthly(month, year);
227 else if (julian)
228 j_yearly(year);
229 else
230 yearly(year);
231 exit(0);
232 }
233
234 #define DAY_LEN 3 /* 3 spaces per day */
235 #define J_DAY_LEN 4 /* 4 spaces per day */
236 #define WEEK_LEN 20 /* 7 * 3 - one space at the end */
237 #define WEEKNUMBER_LEN 5 /* 5 spaces per week number */
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 int
week(int day,int month,int year)243 week(int day, int month, int year)
244 {
245 int yearday;
246 int firstweekday;
247 int weekday;
248 int firstday;
249 int firstsunday;
250 int shift;
251
252 if (mflag)
253 return isoweek(day, month, year);
254
255 yearday = day_in_year(day, month, year);
256 firstweekday = day_in_week(1, 1, year) + 1;
257 weekday = day_in_week(day, month, year) + 1;
258 firstday = day_in_year(1, 1, year);
259 firstsunday = firstday + (8 - firstweekday);
260
261 shift = 1;
262 if (yearday < firstsunday)
263 return (1);
264 if (firstweekday > THURSDAY - 1)
265 shift = 2;
266 return ((((yearday + 1) - (weekday - 1)) / 7) + shift);
267 }
268
269 int
isoweek(int day,int month,int year)270 isoweek(int day, int month, int year)
271 {
272 /* http://www.tondering.dk/claus/cal/node8.html */
273 int a, b, c, s, e, f, g, d, n;
274
275 a = month <= 2 ? year - 1 : year;
276 b = a/4 - a/100 + a/400;
277 c = (a-1)/4 - (a-1)/100 + (a-1)/400;
278 s = b - c;
279 if (month <= 2) {
280 e = 0;
281 f = day - 1 + 31 * (month-1);
282 } else {
283 e = s + 1;
284 f = day + ((153 * (month-3) + 2) / 5) + 58 + s;
285 }
286 g = (a + b) % 7;
287 d = (f + g - e) % 7;
288 n = f + 3 - d;
289
290 if (n < 0)
291 return 53 - (g - s) / 5;
292 else if (n > 364 + s)
293 return 1;
294 else
295 return n/7 + 1;
296 }
297
298 void
monthly(int month,int year)299 monthly(int month, int year)
300 {
301 int col, row, len, days[MAXDAYS], firstday;
302 char *p, lineout[30];
303
304 day_array(month, year, days);
305 (void)snprintf(lineout, sizeof(lineout), "%s %d",
306 month_names[month - 1], year);
307 len = strlen(lineout);
308 (void)printf("%*s%s\n%s\n",
309 ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
310 lineout, day_headings);
311 for (row = 0; row < 6; row++) {
312 firstday = SPACE;
313 for (col = 0, p = lineout; col < 7; col++,
314 p += julian ? J_DAY_LEN : DAY_LEN) {
315 if (firstday == SPACE && days[row * 7 + col] != SPACE)
316 firstday = days[row * 7 + col];
317 ascii_day(p, days[row * 7 + col]);
318 }
319 *p = '\0';
320 trim_trailing_spaces(lineout);
321 (void)printf("%-20s", lineout);
322 if (wflag && firstday != SPACE)
323 printf(" [%2d]", week(firstday, month, year));
324 printf("\n");
325 }
326 }
327
328 void
j_yearly(int year)329 j_yearly(int year)
330 {
331 int col, *dp, i, month, row, which_cal;
332 int days[12][MAXDAYS];
333 char *p, lineout[80];
334
335 (void)snprintf(lineout, sizeof(lineout), "%d", year);
336 center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
337 (void)printf("\n\n");
338 for (i = 0; i < 12; i++)
339 day_array(i + 1, year, days[i]);
340 (void)memset(lineout, ' ', sizeof(lineout) - 1);
341 lineout[sizeof(lineout) - 1] = '\0';
342 for (month = 0; month < 12; month += 2) {
343 center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
344 center(month_names[month + 1], J_WEEK_LEN, 0);
345 (void)printf("\n%s%*s%s\n", day_headings,
346 J_HEAD_SEP, "", day_headings);
347
348 for (row = 0; row < 6; row++) {
349 for (which_cal = 0; which_cal < 2; which_cal++) {
350 p = lineout + which_cal * (J_WEEK_LEN + 2);
351 dp = &days[month + which_cal][row * 7];
352 for (col = 0; col < 7; col++, p += J_DAY_LEN)
353 ascii_day(p, *dp++);
354 }
355 *p = '\0';
356 trim_trailing_spaces(lineout);
357 (void)printf("%s\n", lineout);
358 }
359 }
360 (void)printf("\n");
361 }
362
363 void
yearly(int year)364 yearly(int year)
365 {
366 int col, *dp, i, month, row, which_cal, week_len, wn, firstday;
367 int days[12][MAXDAYS];
368 char *p, lineout[81];
369
370 week_len = WEEK_LEN;
371 if (wflag)
372 week_len += WEEKNUMBER_LEN;
373 (void)snprintf(lineout, sizeof(lineout), "%d", year);
374 center(lineout, week_len * 3 + HEAD_SEP * 2, 0);
375 (void)printf("\n\n");
376 for (i = 0; i < 12; i++)
377 day_array(i + 1, year, days[i]);
378 (void)memset(lineout, ' ', sizeof(lineout) - 1);
379 lineout[sizeof(lineout) - 1] = '\0';
380 for (month = 0; month < 12; month += 3) {
381 center(month_names[month], week_len, HEAD_SEP);
382 center(month_names[month + 1], week_len, HEAD_SEP);
383 center(month_names[month + 2], week_len, 0);
384 (void)printf("\n%s%*s%s%*s%s\n", day_headings,
385 HEAD_SEP + (wflag ? WEEKNUMBER_LEN : 0), "", day_headings,
386 HEAD_SEP + (wflag ? WEEKNUMBER_LEN : 0), "", day_headings);
387
388 for (row = 0; row < 6; row++) {
389 for (which_cal = 0; which_cal < 3; which_cal++) {
390 p = lineout + which_cal * (week_len + 2);
391
392 dp = &days[month + which_cal][row * 7];
393 firstday = SPACE;
394 for (col = 0; col < 7; col++, p += DAY_LEN) {
395 if (firstday == SPACE && *dp != SPACE)
396 firstday = *dp;
397 ascii_day(p, *dp++);
398 }
399 if (wflag && firstday != SPACE) {
400 wn = week(firstday,
401 month + which_cal + 1, year);
402 (void)snprintf(p, 5, "[%2d]", wn);
403 p += strlen(p);
404 *p = ' ';
405 } else
406 memset(p, ' ', 4);
407 }
408 *p = '\0';
409 trim_trailing_spaces(lineout);
410 (void)printf("%s\n", lineout);
411 }
412 }
413 (void)printf("\n");
414 }
415
416 /*
417 * day_array --
418 * Fill in an array of 42 integers with a calendar. Assume for a moment
419 * that you took the (maximum) 6 rows in a calendar and stretched them
420 * out end to end. You would have 42 numbers or spaces. This routine
421 * builds that array for any month from Jan. 1 through Dec. 9999.
422 */
423 void
day_array(int month,int year,int * days)424 day_array(int month, int year, int *days)
425 {
426 int day, dw, dm;
427
428 if (month == 9 && year == 1752) {
429 memmove(days, sep1752, MAXDAYS * sizeof(int));
430 return;
431 }
432 memmove(days, empty, MAXDAYS * sizeof(int));
433 dm = days_in_month[leap_year(year)][month];
434 dw = day_in_week(mflag?0:1, month, year);
435 day = julian ? day_in_year(1, month, year) : 1;
436 while (dm--)
437 days[dw++] = day++;
438 }
439
440 /*
441 * day_in_year --
442 * return the 1 based day number within the year
443 */
444 int
day_in_year(int day,int month,int year)445 day_in_year(int day, int month, int year)
446 {
447 int i, leap;
448
449 leap = leap_year(year);
450 for (i = 1; i < month; i++)
451 day += days_in_month[leap][i];
452 return (day);
453 }
454
455 /*
456 * day_in_week
457 * return the 0 based day number for any date from 1 Jan. 1 to
458 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
459 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
460 * missing days.
461 */
462 int
day_in_week(int day,int month,int year)463 day_in_week(int day, int month, int year)
464 {
465 long temp;
466
467 temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
468 + day_in_year(day, month, year);
469 if (temp < FIRST_MISSING_DAY)
470 return ((temp - 1 + SATURDAY) % 7);
471 if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
472 return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
473 return (THURSDAY);
474 }
475
476 void
ascii_day(char * p,int day)477 ascii_day(char *p, int day)
478 {
479 int display, val;
480 static const char *aday[] = {
481 "",
482 " 1", " 2", " 3", " 4", " 5", " 6", " 7",
483 " 8", " 9", "10", "11", "12", "13", "14",
484 "15", "16", "17", "18", "19", "20", "21",
485 "22", "23", "24", "25", "26", "27", "28",
486 "29", "30", "31",
487 };
488
489 if (day == SPACE) {
490 memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
491 return;
492 }
493 if (julian) {
494 val = day / 100;
495 if (val) {
496 day %= 100;
497 *p++ = val + '0';
498 display = 1;
499 } else {
500 *p++ = ' ';
501 display = 0;
502 }
503 val = day / 10;
504 if (val || display)
505 *p++ = val + '0';
506 else
507 *p++ = ' ';
508 *p++ = day % 10 + '0';
509 } else {
510 *p++ = aday[day][0];
511 *p++ = aday[day][1];
512 }
513 *p = ' ';
514 }
515
516 void
trim_trailing_spaces(char * s)517 trim_trailing_spaces(char *s)
518 {
519 char *p;
520
521 for (p = s; *p; ++p)
522 continue;
523 while (p > s && isspace((unsigned char)*--p))
524 continue;
525 if (p > s)
526 ++p;
527 *p = '\0';
528 }
529
530 void
center(const char * str,int len,int separate)531 center(const char *str, int len, int separate)
532 {
533
534 len -= strlen(str);
535 (void)printf("%*s%s%*s", len / 2, "", str,
536 len / 2 + len % 2 + separate, "");
537 }
538
539 void
usage(void)540 usage(void)
541 {
542
543 (void)fprintf(stderr, "usage: cal [-jmwy] [month] [year]\n");
544 exit(1);
545 }
546
547 int
parsemonth(const char * s)548 parsemonth(const char *s)
549 {
550 struct tm tm;
551 char *cp;
552 int v;
553
554 v = (int)strtol(s, &cp, 10);
555 if (*cp != '\0') { /* s wasn't purely numeric */
556 v = 0;
557 if ((cp = strptime(s, "%b", &tm)) != NULL && *cp == '\0')
558 v = tm.tm_mon + 1;
559 }
560 if (v <= 0 || v > 12)
561 errx(1, "invalid month: use 1-12 or a name");
562 return (v);
563 }
564