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