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