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