xref: /386bsd/usr/src/usr.bin/cal/cal.c (revision a2142627)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Kim Letkeman.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 char copyright[] =
39 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
40  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 static char sccsid[] = "@(#)cal.c	5.2 (Berkeley) 4/19/91";
45 #endif /* not lint */
46 
47 #include <sys/types.h>
48 #include <sys/time.h>
49 #include <stdio.h>
50 #include <ctype.h>
51 
52 #define	THURSDAY		4		/* for reformation */
53 #define	SATURDAY 		6		/* 1 Jan 1 was a Saturday */
54 
55 #define	FIRST_MISSING_DAY 	639787		/* 3 Sep 1752 */
56 #define	NUMBER_MISSING_DAYS 	11		/* 11 day correction */
57 
58 #define	MAXDAYS			42		/* max slots in a month array */
59 #define	SPACE			-1		/* used in day array */
60 
61 static int days_in_month[2][13] = {
62 	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
63 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
64 };
65 
66 int sep1752[MAXDAYS] = {
67 	SPACE,	SPACE,	1,	2,	14,	15,	16,
68 	17,	18,	19,	20,	21,	22,	23,
69 	24,	25,	26,	27,	28,	29,	30,
70 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
71 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
72 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
73 }, j_sep1752[MAXDAYS] = {
74 	SPACE,	SPACE,	245,	246,	258,	259,	260,
75 	261,	262,	263,	264,	265,	266,	267,
76 	268,	269,	270,	271,	272,	273,	274,
77 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
78 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
79 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
80 }, empty[MAXDAYS] = {
81 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
82 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	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 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
87 };
88 
89 char *month_names[12] = {
90 	"January", "February", "March", "April", "May", "June",
91 	"July", "August", "September", "October", "November", "December",
92 };
93 
94 char *day_headings = " S  M Tu  W Th  F  S";
95 char *j_day_headings = "  S   M  Tu   W  Th   F   S";
96 
97 /* leap year -- account for gregorian reformation in 1752 */
98 #define	leap_year(yr) \
99 	((yr) <= 1752 ? !((yr) % 4) : \
100 	!((yr) % 4) && ((yr) % 100) || !((yr) % 400))
101 
102 /* number of centuries since 1700, not inclusive */
103 #define	centuries_since_1700(yr) \
104 	((yr) > 1700 ? (yr) / 100 - 17 : 0)
105 
106 /* number of centuries since 1700 whose modulo of 400 is 0 */
107 #define	quad_centuries_since_1700(yr) \
108 	((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
109 
110 /* number of leap years between year 1 and this year, not inclusive */
111 #define	leap_years_since_year_1(yr) \
112 	((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
113 
114 int julian;
115 
main(argc,argv)116 main(argc, argv)
117 	int argc;
118 	char **argv;
119 {
120 	extern char *optarg;
121 	extern int optind;
122 	struct tm *local_time;
123 	time_t now, time();
124 	int ch, month, year, yflag;
125 
126 	yflag = 0;
127 	while ((ch = getopt(argc, argv, "jy")) != EOF)
128 		switch(ch) {
129 		case 'j':
130 			julian = 1;
131 			break;
132 		case 'y':
133 			yflag = 1;
134 			break;
135 		case '?':
136 		default:
137 			usage();
138 		}
139 	argc -= optind;
140 	argv += optind;
141 
142 	month = 0;
143 	switch(argc) {
144 	case 2:
145 		if ((month = atoi(*argv++)) <= 0 || month > 12) {
146 			(void)fprintf(stderr,
147 			    "cal: illegal month value: use 0-12\n");
148 			exit(1);
149 		}
150 		/* FALLTHROUGH */
151 	case 1:
152 		if ((year = atoi(*argv)) <= 0 || year > 9999) {
153 			(void)fprintf(stderr,
154 			    "cal: illegal year value: use 0-9999\n");
155 			exit(1);
156 		}
157 		break;
158 	case 0:
159 		(void)time(&now);
160 		local_time = localtime(&now);
161 		year = local_time->tm_year + 1900;
162 		if (!yflag)
163 			month = local_time->tm_mon + 1;
164 		break;
165 	default:
166 		usage();
167 	}
168 
169 	if (month)
170 		monthly(month, year);
171 	else if (julian)
172 		j_yearly(year);
173 	else
174 		yearly(year);
175 	exit(0);
176 }
177 
178 #define	DAY_LEN		3		/* 3 spaces per day */
179 #define	J_DAY_LEN	4		/* 4 spaces per day */
180 #define	WEEK_LEN	20		/* 7 * 3 - one space at the end */
181 #define	J_WEEK_LEN	27		/* 7 * 4 - one space at the end */
182 #define	HEAD_SEP	2		/* spaces between day headings */
183 #define	J_HEAD_SEP	2
184 
monthly(month,year)185 monthly(month, year)
186 	int month, year;
187 {
188 	register int col, row;
189 	register char *p;
190 	int len, days[MAXDAYS];
191 	char lineout[30];
192 
193 	day_array(month, year, days);
194 	len = sprintf(lineout, "%s %d", month_names[month - 1], year);
195 	(void)printf("%*s%s\n%s\n",
196 	    ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
197 	    lineout, julian ? j_day_headings : day_headings);
198 	for (row = 0; row < 6; row++) {
199 		for (col = 0, p = lineout; col < 7; col++,
200 		    p += julian ? J_DAY_LEN : DAY_LEN)
201 			ascii_day(p, days[row * 7 + col]);
202 		trim_trailing_spaces(lineout);
203 		(void)printf("%s\n", lineout);
204 	}
205 }
206 
j_yearly(year)207 j_yearly(year)
208 	int year;
209 {
210 	register int col, *dp, i, month, row, which_cal;
211 	register char *p;
212 	int days[12][MAXDAYS];
213 	char lineout[80];
214 
215 	(void)sprintf(lineout, "%d", year);
216 	center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
217 	(void)printf("\n\n");
218 	for (i = 0; i < 12; i++)
219 		day_array(i + 1, year, days[i]);
220 	(void)memset(lineout, ' ', sizeof(lineout) - 1);
221 	lineout[sizeof(lineout) - 1] = '\0';
222 	for (month = 0; month < 12; month += 2) {
223 		center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
224 		center(month_names[month + 1], J_WEEK_LEN, 0);
225 		(void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
226 		    j_day_headings);
227 		for (row = 0; row < 6; row++) {
228 			for (which_cal = 0; which_cal < 2; which_cal++) {
229 				p = lineout + which_cal * (J_WEEK_LEN + 2);
230 				dp = &days[month + which_cal][row * 7];
231 				for (col = 0; col < 7; col++, p += J_DAY_LEN)
232 					ascii_day(p, *dp++);
233 			}
234 			trim_trailing_spaces(lineout);
235 			(void)printf("%s\n", lineout);
236 		}
237 	}
238 	(void)printf("\n");
239 }
240 
yearly(year)241 yearly(year)
242 	int year;
243 {
244 	register int col, *dp, i, month, row, which_cal;
245 	register char *p;
246 	int days[12][MAXDAYS];
247 	char lineout[80];
248 
249 	(void)sprintf(lineout, "%d", year);
250 	center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
251 	(void)printf("\n\n");
252 	for (i = 0; i < 12; i++)
253 		day_array(i + 1, year, days[i]);
254 	(void)memset(lineout, ' ', sizeof(lineout) - 1);
255 	lineout[sizeof(lineout) - 1] = '\0';
256 	for (month = 0; month < 12; month += 3) {
257 		center(month_names[month], WEEK_LEN, HEAD_SEP);
258 		center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
259 		center(month_names[month + 2], WEEK_LEN, 0);
260 		(void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
261 		    "", day_headings, HEAD_SEP, "", day_headings);
262 		for (row = 0; row < 6; row++) {
263 			for (which_cal = 0; which_cal < 3; which_cal++) {
264 				p = lineout + which_cal * (WEEK_LEN + 2);
265 				dp = &days[month + which_cal][row * 7];
266 				for (col = 0; col < 7; col++, p += DAY_LEN)
267 					ascii_day(p, *dp++);
268 			}
269 			trim_trailing_spaces(lineout);
270 			(void)printf("%s\n", lineout);
271 		}
272 	}
273 	(void)printf("\n");
274 }
275 
276 /*
277  * day_array --
278  *	Fill in an array of 42 integers with a calendar.  Assume for a moment
279  *	that you took the (maximum) 6 rows in a calendar and stretched them
280  *	out end to end.  You would have 42 numbers or spaces.  This routine
281  *	builds that array for any month from Jan. 1 through Dec. 9999.
282  */
day_array(month,year,days)283 day_array(month, year, days)
284 	register int *days;
285 	int month, year;
286 {
287 	register int i, day, dw, dm;
288 
289 	if (month == 9 && year == 1752) {
290 		bcopy(julian ? j_sep1752 : sep1752,
291 		    days, MAXDAYS * sizeof(int));
292 		return;
293 	}
294 	bcopy(empty, days, MAXDAYS * sizeof(int));
295 	dm = days_in_month[leap_year(year)][month];
296 	dw = day_in_week(1, month, year);
297 	day = julian ? day_in_year(1, month, year) : 1;
298 	while (dm--)
299 		days[dw++] = day++;
300 }
301 
302 /*
303  * day_in_year --
304  *	return the 1 based day number within the year
305  */
day_in_year(day,month,year)306 day_in_year(day, month, year)
307 	register int day, month;
308 	int year;
309 {
310 	register int i, leap;
311 
312 	leap = leap_year(year);
313 	for (i = 1; i < month; i++)
314 		day += days_in_month[leap][i];
315 	return(day);
316 }
317 
318 /*
319  * day_in_week
320  *	return the 0 based day number for any date from 1 Jan. 1 to
321  *	31 Dec. 9999.  Assumes the Gregorian reformation eliminates
322  *	3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
323  *	missing days.
324  */
day_in_week(day,month,year)325 day_in_week(day, month, year)
326 	int day, month, year;
327 {
328 	long temp;
329 
330 	temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
331 	    + day_in_year(day, month, year);
332 	if (temp < FIRST_MISSING_DAY)
333 		return((temp - 1 + SATURDAY) % 7);
334 	if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
335 		return(((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
336 	return(THURSDAY);
337 }
338 
ascii_day(p,day)339 ascii_day(p, day)
340 	register char *p;
341 	register int day;
342 {
343 	register int display, val;
344 	static char *aday[] = {
345 		"",
346 		" 1", " 2", " 3", " 4", " 5", " 6", " 7",
347 		" 8", " 9", "10", "11", "12", "13", "14",
348 		"15", "16", "17", "18", "19", "20", "21",
349 		"22", "23", "24", "25", "26", "27", "28",
350 		"29", "30", "31",
351 	};
352 
353 	if (day == SPACE) {
354 		memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
355 		return;
356 	}
357 	if (julian) {
358 		if (val = day / 100) {
359 			day %= 100;
360 			*p++ = val + '0';
361 			display = 1;
362 		} else {
363 			*p++ = ' ';
364 			display = 0;
365 		}
366 		val = day / 10;
367 		if (val || display)
368 			*p++ = val + '0';
369 		else
370 			*p++ = ' ';
371 		*p++ = day % 10 + '0';
372 	} else {
373 		*p++ = aday[day][0];
374 		*p++ = aday[day][1];
375 	}
376 	*p = ' ';
377 }
378 
trim_trailing_spaces(s)379 trim_trailing_spaces(s)
380 	register char *s;
381 {
382 	register char *p;
383 
384 	for (p = s; *p; ++p);
385 	while (p > s && isspace(*--p));
386 	if (p > s)
387 		++p;
388 	*p = '\0';
389 }
390 
center(str,len,separate)391 center(str, len, separate)
392 	char *str;
393 	register int len;
394 	int separate;
395 {
396 	len -= strlen(str);
397 	(void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
398 	if (separate)
399 		(void)printf("%*s", separate, "");
400 }
401 
usage()402 usage()
403 {
404 	(void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n");
405 	exit(1);
406 }
407