Home | History | Annotate | Line # | Download | only in cal
cal.c revision 1.2
      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.2 1993/08/01 18:18:17 mycroft 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 		trim_trailing_spaces(lineout);
    204 		(void)printf("%s\n", lineout);
    205 	}
    206 }
    207 
    208 j_yearly(year)
    209 	int year;
    210 {
    211 	register int col, *dp, i, month, row, which_cal;
    212 	register char *p;
    213 	int days[12][MAXDAYS];
    214 	char lineout[80];
    215 
    216 	(void)sprintf(lineout, "%d", year);
    217 	center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
    218 	(void)printf("\n\n");
    219 	for (i = 0; i < 12; i++)
    220 		day_array(i + 1, year, days[i]);
    221 	(void)memset(lineout, ' ', sizeof(lineout) - 1);
    222 	lineout[sizeof(lineout) - 1] = '\0';
    223 	for (month = 0; month < 12; month += 2) {
    224 		center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
    225 		center(month_names[month + 1], J_WEEK_LEN, 0);
    226 		(void)printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
    227 		    j_day_headings);
    228 		for (row = 0; row < 6; row++) {
    229 			for (which_cal = 0; which_cal < 2; which_cal++) {
    230 				p = lineout + which_cal * (J_WEEK_LEN + 2);
    231 				dp = &days[month + which_cal][row * 7];
    232 				for (col = 0; col < 7; col++, p += J_DAY_LEN)
    233 					ascii_day(p, *dp++);
    234 			}
    235 			trim_trailing_spaces(lineout);
    236 			(void)printf("%s\n", lineout);
    237 		}
    238 	}
    239 	(void)printf("\n");
    240 }
    241 
    242 yearly(year)
    243 	int year;
    244 {
    245 	register int col, *dp, i, month, row, which_cal;
    246 	register char *p;
    247 	int days[12][MAXDAYS];
    248 	char lineout[80];
    249 
    250 	(void)sprintf(lineout, "%d", year);
    251 	center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0);
    252 	(void)printf("\n\n");
    253 	for (i = 0; i < 12; i++)
    254 		day_array(i + 1, year, days[i]);
    255 	(void)memset(lineout, ' ', sizeof(lineout) - 1);
    256 	lineout[sizeof(lineout) - 1] = '\0';
    257 	for (month = 0; month < 12; month += 3) {
    258 		center(month_names[month], WEEK_LEN, HEAD_SEP);
    259 		center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
    260 		center(month_names[month + 2], WEEK_LEN, 0);
    261 		(void)printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
    262 		    "", day_headings, HEAD_SEP, "", day_headings);
    263 		for (row = 0; row < 6; row++) {
    264 			for (which_cal = 0; which_cal < 3; which_cal++) {
    265 				p = lineout + which_cal * (WEEK_LEN + 2);
    266 				dp = &days[month + which_cal][row * 7];
    267 				for (col = 0; col < 7; col++, p += DAY_LEN)
    268 					ascii_day(p, *dp++);
    269 			}
    270 			trim_trailing_spaces(lineout);
    271 			(void)printf("%s\n", lineout);
    272 		}
    273 	}
    274 	(void)printf("\n");
    275 }
    276 
    277 /*
    278  * day_array --
    279  *	Fill in an array of 42 integers with a calendar.  Assume for a moment
    280  *	that you took the (maximum) 6 rows in a calendar and stretched them
    281  *	out end to end.  You would have 42 numbers or spaces.  This routine
    282  *	builds that array for any month from Jan. 1 through Dec. 9999.
    283  */
    284 day_array(month, year, days)
    285 	register int *days;
    286 	int month, year;
    287 {
    288 	register int i, day, dw, dm;
    289 
    290 	if (month == 9 && year == 1752) {
    291 		bcopy(julian ? j_sep1752 : sep1752,
    292 		    days, MAXDAYS * sizeof(int));
    293 		return;
    294 	}
    295 	bcopy(empty, days, MAXDAYS * sizeof(int));
    296 	dm = days_in_month[leap_year(year)][month];
    297 	dw = day_in_week(1, month, year);
    298 	day = julian ? day_in_year(1, month, year) : 1;
    299 	while (dm--)
    300 		days[dw++] = day++;
    301 }
    302 
    303 /*
    304  * day_in_year --
    305  *	return the 1 based day number within the year
    306  */
    307 day_in_year(day, month, year)
    308 	register int day, month;
    309 	int year;
    310 {
    311 	register int i, leap;
    312 
    313 	leap = leap_year(year);
    314 	for (i = 1; i < month; i++)
    315 		day += days_in_month[leap][i];
    316 	return(day);
    317 }
    318 
    319 /*
    320  * day_in_week
    321  *	return the 0 based day number for any date from 1 Jan. 1 to
    322  *	31 Dec. 9999.  Assumes the Gregorian reformation eliminates
    323  *	3 Sep. 1752 through 13 Sep. 1752.  Returns Thursday for all
    324  *	missing days.
    325  */
    326 day_in_week(day, month, year)
    327 	int day, month, year;
    328 {
    329 	long temp;
    330 
    331 	temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
    332 	    + day_in_year(day, month, year);
    333 	if (temp < FIRST_MISSING_DAY)
    334 		return((temp - 1 + SATURDAY) % 7);
    335 	if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS))
    336 		return(((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
    337 	return(THURSDAY);
    338 }
    339 
    340 ascii_day(p, day)
    341 	register char *p;
    342 	register int day;
    343 {
    344 	register int display, val;
    345 	static char *aday[] = {
    346 		"",
    347 		" 1", " 2", " 3", " 4", " 5", " 6", " 7",
    348 		" 8", " 9", "10", "11", "12", "13", "14",
    349 		"15", "16", "17", "18", "19", "20", "21",
    350 		"22", "23", "24", "25", "26", "27", "28",
    351 		"29", "30", "31",
    352 	};
    353 
    354 	if (day == SPACE) {
    355 		memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
    356 		return;
    357 	}
    358 	if (julian) {
    359 		if (val = day / 100) {
    360 			day %= 100;
    361 			*p++ = val + '0';
    362 			display = 1;
    363 		} else {
    364 			*p++ = ' ';
    365 			display = 0;
    366 		}
    367 		val = day / 10;
    368 		if (val || display)
    369 			*p++ = val + '0';
    370 		else
    371 			*p++ = ' ';
    372 		*p++ = day % 10 + '0';
    373 	} else {
    374 		*p++ = aday[day][0];
    375 		*p++ = aday[day][1];
    376 	}
    377 	*p = ' ';
    378 }
    379 
    380 trim_trailing_spaces(s)
    381 	register char *s;
    382 {
    383 	register char *p;
    384 
    385 	for (p = s; *p; ++p);
    386 	while (p > s && isspace(*--p));
    387 	if (p > s)
    388 		++p;
    389 	*p = '\0';
    390 }
    391 
    392 center(str, len, separate)
    393 	char *str;
    394 	register int len;
    395 	int separate;
    396 {
    397 	len -= strlen(str);
    398 	(void)printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
    399 	if (separate)
    400 		(void)printf("%*s", separate, "");
    401 }
    402 
    403 usage()
    404 {
    405 	(void)fprintf(stderr, "usage: cal [-jy] [[month] year]\n");
    406 	exit(1);
    407 }
    408