Home | History | Annotate | Line # | Download | only in cal
cal.c revision 1.3
      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[] = "from: @(#)cal.c	5.2 (Berkeley) 4/19/91";*/
     45 static char rcsid[] = "$Id: cal.c,v 1.3 1994/01/25 00:49:35 cgd Exp $";
     46 #endif /* not lint */
     47 
     48 #include <sys/types.h>
     49 #include <sys/time.h>
     50 #include <stdio.h>
     51 #include <ctype.h>
     52 
     53 #define	THURSDAY		4		/* for reformation */
     54 #define	SATURDAY 		6		/* 1 Jan 1 was a Saturday */
     55 
     56 #define	FIRST_MISSING_DAY 	639787		/* 3 Sep 1752 */
     57 #define	NUMBER_MISSING_DAYS 	11		/* 11 day correction */
     58 
     59 #define	MAXDAYS			42		/* max slots in a month array */
     60 #define	SPACE			-1		/* used in day array */
     61 
     62 static int days_in_month[2][13] = {
     63 	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
     64 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
     65 };
     66 
     67 int sep1752[MAXDAYS] = {
     68 	SPACE,	SPACE,	1,	2,	14,	15,	16,
     69 	17,	18,	19,	20,	21,	22,	23,
     70 	24,	25,	26,	27,	28,	29,	30,
     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 }, j_sep1752[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 }, empty[MAXDAYS] = {
     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 	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,	SPACE,
     88 };
     89 
     90 char *month_names[12] = {
     91 	"January", "February", "March", "April", "May", "June",
     92 	"July", "August", "September", "October", "November", "December",
     93 };
     94 
     95 char *day_headings = " S  M Tu  W Th  F  S";
     96 char *j_day_headings = "  S   M  Tu   W  Th   F   S";
     97 
     98 /* leap year -- account for gregorian reformation in 1752 */
     99 #define	leap_year(yr) \
    100 	((yr) <= 1752 ? !((yr) % 4) : \
    101 	!((yr) % 4) && ((yr) % 100) || !((yr) % 400))
    102 
    103 /* number of centuries since 1700, not inclusive */
    104 #define	centuries_since_1700(yr) \
    105 	((yr) > 1700 ? (yr) / 100 - 17 : 0)
    106 
    107 /* number of centuries since 1700 whose modulo of 400 is 0 */
    108 #define	quad_centuries_since_1700(yr) \
    109 	((yr) > 1600 ? ((yr) - 1600) / 400 : 0)
    110 
    111 /* number of leap years between year 1 and this year, not inclusive */
    112 #define	leap_years_since_year_1(yr) \
    113 	((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
    114 
    115 int julian;
    116 
    117 main(argc, argv)
    118 	int argc;
    119 	char **argv;
    120 {
    121 	extern char *optarg;
    122 	extern int optind;
    123 	struct tm *local_time;
    124 	time_t now, time();
    125 	int ch, month, year, yflag;
    126 
    127 	yflag = 0;
    128 	while ((ch = getopt(argc, argv, "jy")) != EOF)
    129 		switch(ch) {
    130 		case 'j':
    131 			julian = 1;
    132 			break;
    133 		case 'y':
    134 			yflag = 1;
    135 			break;
    136 		case '?':
    137 		default:
    138 			usage();
    139 		}
    140 	argc -= optind;
    141 	argv += optind;
    142 
    143 	month = 0;
    144 	switch(argc) {
    145 	case 2:
    146 		if ((month = atoi(*argv++)) <= 0 || month > 12) {
    147 			(void)fprintf(stderr,
    148 			    "cal: illegal month value: use 0-12\n");
    149 			exit(1);
    150 		}
    151 		/* FALLTHROUGH */
    152 	case 1:
    153 		if ((year = atoi(*argv)) <= 0 || year > 9999) {
    154 			(void)fprintf(stderr,
    155 			    "cal: illegal year value: use 0-9999\n");
    156 			exit(1);
    157 		}
    158 		break;
    159 	case 0:
    160 		(void)time(&now);
    161 		local_time = localtime(&now);
    162 		year = local_time->tm_year + 1900;
    163 		if (!yflag)
    164 			month = local_time->tm_mon + 1;
    165 		break;
    166 	default:
    167 		usage();
    168 	}
    169 
    170 	if (month)
    171 		monthly(month, year);
    172 	else if (julian)
    173 		j_yearly(year);
    174 	else
    175 		yearly(year);
    176 	exit(0);
    177 }
    178 
    179 #define	DAY_LEN		3		/* 3 spaces per day */
    180 #define	J_DAY_LEN	4		/* 4 spaces per day */
    181 #define	WEEK_LEN	20		/* 7 * 3 - one space at the end */
    182 #define	J_WEEK_LEN	27		/* 7 * 4 - one space at the end */
    183 #define	HEAD_SEP	2		/* spaces between day headings */
    184 #define	J_HEAD_SEP	2
    185 
    186 monthly(month, year)
    187 	int month, year;
    188 {
    189 	register int col, row;
    190 	register char *p;
    191 	int len, days[MAXDAYS];
    192 	char lineout[30];
    193 
    194 	day_array(month, year, days);
    195 	len = sprintf(lineout, "%s %d", month_names[month - 1], year);
    196 	(void)printf("%*s%s\n%s\n",
    197 	    ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
    198 	    lineout, julian ? j_day_headings : day_headings);
    199 	for (row = 0; row < 6; row++) {
    200 		for (col = 0, p = lineout; col < 7; col++,
    201 		    p += julian ? J_DAY_LEN : DAY_LEN)
    202 			ascii_day(p, days[row * 7 + col]);
    203 		*p = '\0';
    204 		trim_trailing_spaces(lineout);
    205 		(void)printf("%s\n", lineout);
    206 	}
    207 }
    208 
    209 j_yearly(year)
    210 	int year;
    211 {
    212 	register int col, *dp, i, month, row, which_cal;
    213 	register char *p;
    214 	int days[12][MAXDAYS];
    215 	char lineout[80];
    216 
    217 	(void)sprintf(lineout, "%d", year);
    218 	center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
    219 	(void)printf("\n\n");
    220 	for (i = 0; i < 12; i++)
    221 		day_array(i + 1, year, days[i]);
    222 	(void)memset(lineout, ' ', sizeof(lineout) - 1);
    223 	lineout[sizeof(lineout) - 1] = '\0';
    224 	for (month = 0; month < 12; month += 2) {
    225 		center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
    226 		center(month_names[month + 1], J_WEEK_LEN, 0);
    227 		(void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
    228 		    j_day_headings);
    229 		for (row = 0; row < 6; row++) {
    230 			for (which_cal = 0; which_cal < 2; which_cal++) {
    231 				p = lineout + which_cal * (J_WEEK_LEN + 2);
    232 				dp = &days[month + which_cal][row * 7];
    233 				for (col = 0; col < 7; col++, p += J_DAY_LEN)
    234 					ascii_day(p, *dp++);
    235 			}
    236 			*p = '\0';
    237 			trim_trailing_spaces(lineout);
    238 			(void)printf("%s\n", lineout);
    239 		}
    240 	}
    241 	(void)printf("\n");
    242 }
    243 
    244 yearly(year)
    245 	int year;
    246 {
    247 	register int col, *dp, i, month, row, which_cal;
    248 	register char *p;
    249 	int days[12][MAXDAYS];
    250 	char lineout[80];
    251 
    252 	(void)sprintf(lineout, "%d", year);
    253 	center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
    254 	(void)printf("\n\n");
    255 	for (i = 0; i < 12; i++)
    256 		day_array(i + 1, year, days[i]);
    257 	(void)memset(lineout, ' ', sizeof(lineout) - 1);
    258 	lineout[sizeof(lineout) - 1] = '\0';
    259 	for (month = 0; month < 12; month += 3) {
    260 		center(month_names[month], WEEK_LEN, HEAD_SEP);
    261 		center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
    262 		center(month_names[month + 2], WEEK_LEN, 0);
    263 		(void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
    264 		    "", day_headings, HEAD_SEP, "", day_headings);
    265 		for (row = 0; row < 6; row++) {
    266 			for (which_cal = 0; which_cal < 3; which_cal++) {
    267 				p = lineout + which_cal * (WEEK_LEN + 2);
    268 				dp = &days[month + which_cal][row * 7];
    269 				for (col = 0; col < 7; col++, p += DAY_LEN)
    270 					ascii_day(p, *dp++);
    271 			}
    272 			*p = '\0';
    273 			trim_trailing_spaces(lineout);
    274 			(void)printf("%s\n", lineout);
    275 		}
    276 	}
    277 	(void)printf("\n");
    278 }
    279 
    280 /*
    281  * day_array --
    282  *	Fill in an array of 42 integers with a calendar.  Assume for a moment
    283  *	that you took the (maximum) 6 rows in a calendar and stretched them
    284  *	out end to end.  You would have 42 numbers or spaces.  This routine
    285  *	builds that array for any month from Jan. 1 through Dec. 9999.
    286  */
    287 day_array(month, year, days)
    288 	register int *days;
    289 	int month, year;
    290 {
    291 	register int i, day, dw, dm;
    292 
    293 	if (month == 9 && year == 1752) {
    294 		bcopy(julian ? j_sep1752 : sep1752,
    295 		    days, MAXDAYS * sizeof(int));
    296 		return;
    297 	}
    298 	bcopy(empty, days, MAXDAYS * sizeof(int));
    299 	dm = days_in_month[leap_year(year)][month];
    300 	dw = day_in_week(1, month, year);
    301 	day = julian ? day_in_year(1, month, year) : 1;
    302 	while (dm--)
    303 		days[dw++] = day++;
    304 }
    305 
    306 /*
    307  * day_in_year --
    308  *	return the 1 based day number within the year
    309  */
    310 day_in_year(day, month, year)
    311 	register int day, month;
    312 	int year;
    313 {
    314 	register int i, leap;
    315 
    316 	leap = leap_year(year);
    317 	for (i = 1; i < month; i++)
    318 		day += days_in_month[leap][i];
    319 	return(day);
    320 }
    321 
    322 /*
    323  * day_in_week
    324  *	return the 0 based day number for any date from 1 Jan. 1 to
    325  *	31 Dec. 9999.  Assumes the Gregorian reformation eliminates
    326  *	3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
    327  *	missing days.
    328  */
    329 day_in_week(day, month, year)
    330 	int day, month, year;
    331 {
    332 	long temp;
    333 
    334 	temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
    335 	    + day_in_year(day, month, year);
    336 	if (temp < FIRST_MISSING_DAY)
    337 		return((temp - 1 + SATURDAY) % 7);
    338 	if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
    339 		return(((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
    340 	return(THURSDAY);
    341 }
    342 
    343 ascii_day(p, day)
    344 	register char *p;
    345 	register int day;
    346 {
    347 	register int display, val;
    348 	static char *aday[] = {
    349 		"",
    350 		" 1", " 2", " 3", " 4", " 5", " 6", " 7",
    351 		" 8", " 9", "10", "11", "12", "13", "14",
    352 		"15", "16", "17", "18", "19", "20", "21",
    353 		"22", "23", "24", "25", "26", "27", "28",
    354 		"29", "30", "31",
    355 	};
    356 
    357 	if (day == SPACE) {
    358 		memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
    359 		return;
    360 	}
    361 	if (julian) {
    362 		if (val = day / 100) {
    363 			day %= 100;
    364 			*p++ = val + '0';
    365 			display = 1;
    366 		} else {
    367 			*p++ = ' ';
    368 			display = 0;
    369 		}
    370 		val = day / 10;
    371 		if (val || display)
    372 			*p++ = val + '0';
    373 		else
    374 			*p++ = ' ';
    375 		*p++ = day % 10 + '0';
    376 	} else {
    377 		*p++ = aday[day][0];
    378 		*p++ = aday[day][1];
    379 	}
    380 	*p = ' ';
    381 }
    382 
    383 trim_trailing_spaces(s)
    384 	register char *s;
    385 {
    386 	register char *p;
    387 
    388 	for (p = s; *p; ++p);
    389 	while (p > s && isspace(*--p));
    390 	if (p > s)
    391 		++p;
    392 	*p = '\0';
    393 }
    394 
    395 center(str, len, separate)
    396 	char *str;
    397 	register int len;
    398 	int separate;
    399 {
    400 	len -= strlen(str);
    401 	(void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
    402 	if (separate)
    403 		(void)printf("%*s", separate, "");
    404 }
    405 
    406 usage()
    407 {
    408 	(void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n");
    409 	exit(1);
    410 }
    411