1 /*- 2 * Copyright (c) 1997 Wolfgang Helbig 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #ifndef lint 28 static const char rcsid[] = 29 "$FreeBSD$"; 30 #endif /* not lint */ 31 32 #include <calendar.h> 33 #include <ctype.h> 34 #include <err.h> 35 #include <langinfo.h> 36 #include <locale.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <sysexits.h> 41 #include <time.h> 42 #include <unistd.h> 43 44 /* Width of one month with backward compatibility */ 45 #define MONTH_WIDTH_B_J 27 46 #define MONTH_WIDTH_B 20 47 48 #define MONTH_WIDTH_J 24 49 #define MONTH_WIDTH 18 50 51 #define MAX_WIDTH 28 52 53 typedef struct date date; 54 55 struct monthlines { 56 char name[MAX_WIDTH + 1]; 57 char lines[7][MAX_WIDTH + 1]; 58 char weeks[MAX_WIDTH + 1]; 59 }; 60 61 struct weekdays { 62 char names[7][4]; 63 }; 64 65 /* The switches from Julian to Gregorian in some countries */ 66 static struct djswitch { 67 char *cc; /* Country code according to ISO 3166 */ 68 char *nm; /* Name of country */ 69 date dt; /* Last day of Julian calendar */ 70 } switches[] = { 71 {"AL", "Albania", {1912, 11, 30}}, 72 {"AT", "Austria", {1583, 10, 5}}, 73 {"AU", "Australia", {1752, 9, 2}}, 74 {"BE", "Belgium", {1582, 12, 14}}, 75 {"BG", "Bulgaria", {1916, 3, 18}}, 76 {"CA", "Canada", {1752, 9, 2}}, 77 {"CH", "Switzerland", {1655, 2, 28}}, 78 {"CN", "China", {1911, 12, 18}}, 79 {"CZ", "Czech Republic",{1584, 1, 6}}, 80 {"DE", "Germany", {1700, 2, 18}}, 81 {"DK", "Denmark", {1700, 2, 18}}, 82 {"ES", "Spain", {1582, 10, 4}}, 83 {"FI", "Finland", {1753, 2, 17}}, 84 {"FR", "France", {1582, 12, 9}}, 85 {"GB", "United Kingdom",{1752, 9, 2}}, 86 {"GR", "Greece", {1924, 3, 9}}, 87 {"HU", "Hungary", {1587, 10, 21}}, 88 {"IS", "Iceland", {1700, 11, 16}}, 89 {"IT", "Italy", {1582, 10, 4}}, 90 {"JP", "Japan", {1918, 12, 18}}, 91 {"LI", "Lithuania", {1918, 2, 1}}, 92 {"LN", "Latin", {9999, 05, 31}}, 93 {"LU", "Luxembourg", {1582, 12, 14}}, 94 {"LV", "Latvia", {1918, 2, 1}}, 95 {"NL", "Netherlands", {1582, 12, 14}}, 96 {"NO", "Norway", {1700, 2, 18}}, 97 {"PL", "Poland", {1582, 10, 4}}, 98 {"PT", "Portugal", {1582, 10, 4}}, 99 {"RO", "Romania", {1919, 3, 31}}, 100 {"RU", "Russia", {1918, 1, 31}}, 101 {"SI", "Slovenia", {1919, 3, 4}}, 102 {"SW", "Sweden", {1753, 2, 17}}, 103 {"TR", "Turkey", {1926, 12, 18}}, 104 {"US", "United States", {1752, 9, 2}}, 105 {"YU", "Yugoslavia", {1919, 3, 4}} 106 }; 107 108 struct djswitch *dftswitch = 109 switches + sizeof(switches) / sizeof(struct djswitch) - 2; 110 /* default switch (should be "US") */ 111 112 /* Table used to print day of month and week numbers */ 113 char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 114 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 115 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 116 " 48 49 50 51 52 53"; 117 118 /* Table used to print day of year and week numbers */ 119 char jdaystr[] = " 1 2 3 4 5 6 7 8 9" 120 " 10 11 12 13 14 15 16 17 18 19" 121 " 20 21 22 23 24 25 26 27 28 29" 122 " 30 31 32 33 34 35 36 37 38 39" 123 " 40 41 42 43 44 45 46 47 48 49" 124 " 50 51 52 53 54 55 56 57 58 59" 125 " 60 61 62 63 64 65 66 67 68 69" 126 " 70 71 72 73 74 75 76 77 78 79" 127 " 80 81 82 83 84 85 86 87 88 89" 128 " 90 91 92 93 94 95 96 97 98 99" 129 " 100 101 102 103 104 105 106 107 108 109" 130 " 110 111 112 113 114 115 116 117 118 119" 131 " 120 121 122 123 124 125 126 127 128 129" 132 " 130 131 132 133 134 135 136 137 138 139" 133 " 140 141 142 143 144 145 146 147 148 149" 134 " 150 151 152 153 154 155 156 157 158 159" 135 " 160 161 162 163 164 165 166 167 168 169" 136 " 170 171 172 173 174 175 176 177 178 179" 137 " 180 181 182 183 184 185 186 187 188 189" 138 " 190 191 192 193 194 195 196 197 198 199" 139 " 200 201 202 203 204 205 206 207 208 209" 140 " 210 211 212 213 214 215 216 217 218 219" 141 " 220 221 222 223 224 225 226 227 228 229" 142 " 230 231 232 233 234 235 236 237 238 239" 143 " 240 241 242 243 244 245 246 247 248 249" 144 " 250 251 252 253 254 255 256 257 258 259" 145 " 260 261 262 263 264 265 266 267 268 269" 146 " 270 271 272 273 274 275 276 277 278 279" 147 " 280 281 282 283 284 285 286 287 288 289" 148 " 290 291 292 293 294 295 296 297 298 299" 149 " 300 301 302 303 304 305 306 307 308 309" 150 " 310 311 312 313 314 315 316 317 318 319" 151 " 320 321 322 323 324 325 326 327 328 329" 152 " 330 331 332 333 334 335 336 337 338 339" 153 " 340 341 342 343 344 345 346 347 348 349" 154 " 350 351 352 353 354 355 356 357 358 359" 155 " 360 361 362 363 364 365 366"; 156 157 int flag_weeks; /* user wants number of week */ 158 int nswitch; /* user defined switch date */ 159 int nswitchb; /* switch date for backward compatibility */ 160 161 char *center(char *s, char *t, int w); 162 void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl); 163 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 164 void mkweekdays(struct weekdays * wds); 165 void printcc(void); 166 void printeaster(int year, int julian, int orthodox); 167 void printmonth(int year, int month, int jd_flag); 168 void printmonthb(int year, int month, int jd_flag); 169 void printyear(int year, int jd_flag); 170 void printyearb(int year, int jd_flag); 171 int firstday(int y, int m); 172 date *sdate(int ndays, struct date * d); 173 date *sdateb(int ndays, struct date * d); 174 int sndays(struct date * d); 175 int sndaysb(struct date * d); 176 static void usage(void); 177 int weekdayb(int nd); 178 179 int 180 main(int argc, char *argv[]) 181 { 182 struct djswitch *p, *q; /* to search user defined switch date */ 183 date never = {10000, 1, 1}; /* outside valid range of dates */ 184 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 185 int ch; /* holds the option character */ 186 int m = 0; /* month */ 187 int y = 0; /* year */ 188 int flag_backward = 0; /* user called cal--backward compat. */ 189 int flag_hole_year = 0; /* user wants the whole year */ 190 int flag_julian_cal = 0; /* user wants Julian Calendar */ 191 int flag_julian_day = 0; /* user wants the Julian day 192 * numbers */ 193 int flag_orthodox = 0; /* use wants Orthodox easter */ 194 int flag_easter = 0; /* use wants easter date */ 195 char *cp; /* character pointer */ 196 char *locale; /* locale to get country code */ 197 198 /* 199 * Use locale to determine the country code, 200 * and use the country code to determine the default 201 * switchdate and date format from the switches table. 202 */ 203 if (setlocale(LC_ALL, "") == NULL) 204 warn("setlocale"); 205 locale = setlocale(LC_TIME, NULL); 206 if (locale == NULL || 207 strcmp(locale, "C") == 0 || 208 strcmp(locale, "POSIX") == 0 || 209 strcmp(locale, "ASCII") == 0 || 210 strcmp(locale, "US-ASCII") == 0) 211 locale = "_US"; 212 q = switches + sizeof(switches) / sizeof(struct djswitch); 213 for (p = switches; p != q; p++) 214 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 215 break; 216 if (p == q) { 217 nswitch = ndaysj(&dftswitch->dt); 218 } else { 219 nswitch = ndaysj(&p->dt); 220 dftswitch = p; 221 } 222 223 224 /* 225 * Get the filename portion of argv[0] and set flag_backward if 226 * this program is called "cal". 227 */ 228 for (cp = argv[0]; *cp; cp++) 229 ; 230 while (cp >= argv[0] && *cp != '/') 231 cp--; 232 if (strcmp("cal", ++cp) == 0) 233 flag_backward = 1; 234 235 /* Set the switch date to United Kingdom if backwards compatible */ 236 if (flag_backward) 237 nswitchb = ndaysj(&ukswitch); 238 239 while ((ch = getopt(argc, argv, "Jejops:wy")) != -1) 240 switch (ch) { 241 case 'J': 242 if (flag_backward) 243 usage(); 244 nswitch = ndaysj(&never); 245 flag_julian_cal = 1; 246 break; 247 case 'e': 248 if (flag_backward) 249 usage(); 250 flag_easter = 1; 251 break; 252 case 'j': 253 flag_julian_day = 1; 254 break; 255 case 'o': 256 if (flag_backward) 257 usage(); 258 flag_orthodox = 1; 259 flag_easter = 1; 260 break; 261 case 'p': 262 if (flag_backward) 263 usage(); 264 printcc(); 265 return (0); 266 break; 267 case 's': 268 if (flag_backward) 269 usage(); 270 q = switches + 271 sizeof(switches) / sizeof(struct djswitch); 272 for (p = switches; 273 p != q && strcmp(p->cc, optarg) != 0; p++) 274 ; 275 if (p == q) 276 errx(EX_USAGE, 277 "%s: invalid country code", optarg); 278 nswitch = ndaysj(&(p->dt)); 279 break; 280 case 'w': 281 if (flag_backward) 282 usage(); 283 flag_weeks = 1; 284 break; 285 case 'y': 286 flag_hole_year = 1; 287 break; 288 default: 289 usage(); 290 } 291 292 argc -= optind; 293 argv += optind; 294 295 if (argc == 0) { 296 time_t t; 297 struct tm *tm; 298 299 t = time(NULL); 300 tm = localtime(&t); 301 y = tm->tm_year + 1900; 302 m = tm->tm_mon + 1; 303 } 304 305 switch (argc) { 306 case 2: 307 if (flag_easter) 308 usage(); 309 m = atoi(*argv++); 310 if (m < 1 || m > 12) 311 errx(EX_USAGE, "month %d not in range 1..12", m); 312 /* FALLTHROUGH */ 313 case 1: 314 y = atoi(*argv++); 315 if (y < 1 || y > 9999) 316 errx(EX_USAGE, "year %d not in range 1..9999", y); 317 break; 318 case 0: 319 break; 320 default: 321 usage(); 322 } 323 324 if (flag_easter) 325 printeaster(y, flag_julian_cal, flag_orthodox); 326 else if (argc == 1 || flag_hole_year) 327 if (flag_backward) 328 printyearb(y, flag_julian_day); 329 else 330 printyear(y, flag_julian_day); 331 else 332 if (flag_backward) 333 printmonthb(y, m, flag_julian_day); 334 else 335 printmonth(y, m, flag_julian_day); 336 337 return (0); 338 } 339 340 static void 341 usage(void) 342 { 343 344 fprintf(stderr, "%s\n%s\n%s\n", 345 "usage: cal [-jy] [[month] year]", 346 " ncal [-Jjpwy] [-s country_code] [[month] year]", 347 " ncal [-Jeo] [year]"); 348 exit(EX_USAGE); 349 } 350 351 /* print the assumed switches for all countries */ 352 void 353 printcc(void) 354 { 355 struct djswitch *p; 356 int n; /* number of lines to print */ 357 int m; /* offset from left to right table entry on the same line */ 358 359 #define FSTR "%c%s %-15s%4d-%02d-%02d" 360 #define DFLT(p) ((p) == dftswitch ? '*' : ' ') 361 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 362 363 n = sizeof(switches) / sizeof(struct djswitch); 364 m = (n + 1) / 2; 365 n /= 2; 366 for (p = switches; p != switches + n; p++) 367 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 368 if (m != n) 369 printf(FSTR"\n", FSTRARG(p)); 370 } 371 372 /* print the date of easter sunday */ 373 void 374 printeaster(int y, int julian, int orthodox) 375 { 376 date dt; 377 struct tm tm; 378 char buf[80]; 379 static int d_first = -1; 380 381 if (d_first < 0) 382 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 383 /* force orthodox easter for years before 1583 */ 384 if (y < 1583) 385 orthodox = 1; 386 387 if (orthodox) 388 if (julian) 389 easteroj(y, &dt); 390 else 391 easterog(y, &dt); 392 else 393 easterg(y, &dt); 394 395 memset(&tm, 0, sizeof(tm)); 396 tm.tm_year = dt.y - 1900; 397 tm.tm_mon = dt.m - 1; 398 tm.tm_mday = dt.d; 399 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 400 printf("%s\n", buf); 401 } 402 403 void 404 printmonth(int y, int m, int jd_flag) 405 { 406 struct monthlines month; 407 struct weekdays wds; 408 int i; 409 410 mkmonth(y, m - 1, jd_flag, &month); 411 mkweekdays(&wds); 412 printf(" %s %d\n", month.name, y); 413 for (i = 0; i != 7; i++) 414 printf("%.2s%s\n", wds.names[i], month.lines[i]); 415 if (flag_weeks) 416 printf(" %s\n", month.weeks); 417 } 418 419 void 420 printmonthb(int y, int m, int jd_flag) 421 { 422 struct monthlines month; 423 struct weekdays wds; 424 char s[MAX_WIDTH], t[MAX_WIDTH]; 425 int i; 426 int mw; 427 428 mkmonthb(y, m - 1, jd_flag, &month); 429 mkweekdays(&wds); 430 431 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 432 433 sprintf(s, "%s %d", month.name, y); 434 printf("%s\n", center(t, s, mw)); 435 436 if (jd_flag) 437 printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0], 438 wds.names[1], wds.names[2], wds.names[3], 439 wds.names[4], wds.names[5]); 440 else 441 printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0], 442 wds.names[1], wds.names[2], wds.names[3], 443 wds.names[4], wds.names[5]); 444 445 for (i = 0; i != 6; i++) 446 printf("%s\n", month.lines[i]+1); 447 } 448 449 void 450 printyear(int y, int jd_flag) 451 { 452 struct monthlines year[12]; 453 struct weekdays wds; 454 char s[80], t[80]; 455 int i, j; 456 int mpl; 457 int mw; 458 459 for (i = 0; i != 12; i++) 460 mkmonth(y, i, jd_flag, year + i); 461 mkweekdays(&wds); 462 mpl = jd_flag ? 3 : 4; 463 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH; 464 465 sprintf(s, "%d", y); 466 printf("%s\n", center(t, s, mpl * mw)); 467 468 for (j = 0; j != 12; j += mpl) { 469 printf(" %-*s%-*s", 470 mw, year[j].name, 471 mw, year[j + 1].name); 472 if (mpl == 3) 473 printf("%s\n", year[j + 2].name); 474 else 475 printf("%-*s%s\n", 476 mw, year[j + 2].name, 477 year[j + 3].name); 478 for (i = 0; i != 7; i++) { 479 printf("%.2s%-*s%-*s", 480 wds.names[i], 481 mw, year[j].lines[i], 482 mw, year[j + 1].lines[i]); 483 if (mpl == 3) 484 printf("%s\n", year[j + 2].lines[i]); 485 else 486 printf("%-*s%s\n", 487 mw, year[j + 2].lines[i], 488 year[j + 3].lines[i]); 489 } 490 if (flag_weeks) { 491 if (mpl == 3) 492 printf(" %-*s%-*s%-s\n", 493 mw, year[j].weeks, 494 mw, year[j + 1].weeks, 495 year[j + 2].weeks); 496 else 497 printf(" %-*s%-*s%-*s%-s\n", 498 mw, year[j].weeks, 499 mw, year[j + 1].weeks, 500 mw, year[j + 2].weeks, 501 year[j + 3].weeks); 502 } 503 } 504 } 505 506 void 507 printyearb(int y, int jd_flag) 508 { 509 struct monthlines year[12]; 510 struct weekdays wds; 511 char s[80], t[80]; 512 int i, j; 513 int mpl; 514 int mw; 515 516 for (i = 0; i != 12; i++) 517 mkmonthb(y, i, jd_flag, year + i); 518 mkweekdays(&wds); 519 mpl = jd_flag ? 2 : 3; 520 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 521 522 sprintf(s, "%d", y); 523 printf("%s\n\n", center(t, s, mw * mpl + mpl)); 524 525 for (j = 0; j != 12; j += mpl) { 526 printf("%-*s ", mw, center(s, year[j].name, mw)); 527 if (mpl == 2) 528 printf("%s\n", center(s, year[j + 1].name, mw)); 529 else 530 printf("%-*s %s\n", mw, 531 center(s, year[j + 1].name, mw), 532 center(t, year[j + 2].name, mw)); 533 534 if (mpl == 2) 535 printf(" %s %s %s %s %s %s %s " 536 " %s %s %s %s %s %s %.2s\n", 537 wds.names[6], wds.names[0], wds.names[1], 538 wds.names[2], wds.names[3], wds.names[4], 539 wds.names[5], 540 wds.names[6], wds.names[0], wds.names[1], 541 wds.names[2], wds.names[3], wds.names[4], 542 wds.names[5]); 543 else 544 printf("%s%s%s%s%s%s%s " 545 "%s%s%s%s%s%s%s " 546 "%s%s%s%s%s%s%.2s\n", 547 wds.names[6], wds.names[0], wds.names[1], 548 wds.names[2], wds.names[3], wds.names[4], 549 wds.names[5], 550 wds.names[6], wds.names[0], wds.names[1], 551 wds.names[2], wds.names[3], wds.names[4], 552 wds.names[5], 553 wds.names[6], wds.names[0], wds.names[1], 554 wds.names[2], wds.names[3], wds.names[4], 555 wds.names[5]); 556 for (i = 0; i != 6; i++) { 557 if (mpl == 2) 558 printf("%-*s %s\n", 559 mw, year[j].lines[i]+1, 560 year[j + 1].lines[i]+1); 561 else 562 printf("%-*s %-*s %s\n", 563 mw, year[j].lines[i]+1, 564 mw, year[j + 1].lines[i]+1, 565 year[j + 2].lines[i]+1); 566 567 } 568 } 569 } 570 571 void 572 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines) 573 { 574 575 struct tm tm; /* for strftime printing local names of 576 * months */ 577 date dt; /* handy date */ 578 int dw; /* width of numbers */ 579 int first; /* first day of month */ 580 int firstm; /* first day of first week of month */ 581 int i, j, k; /* just indices */ 582 int last; /* the first day of next month */ 583 int jan1 = 0; /* the first day of this year */ 584 char *ds; /* pointer to day strings (daystr or 585 * jdaystr) */ 586 587 /* Set name of month. */ 588 memset(&tm, 0, sizeof(tm)); 589 tm.tm_mon = m; 590 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 591 mlines->name[0] = toupper((unsigned char)mlines->name[0]); 592 593 /* 594 * Set first and last to the day number of the first day of this 595 * month and the first day of next month respectively. Set jan1 to 596 * the day number of the first day of this year. 597 */ 598 first = firstday(y, m + 1); 599 if (m == 11) 600 last = firstday(y + 1, 1); 601 else 602 last = firstday(y, m + 2); 603 604 if (jd_flag) 605 jan1 = firstday(y, 1); 606 607 /* 608 * Set firstm to the day number of monday of the first week of 609 * this month. (This might be in the last month) 610 */ 611 firstm = first - weekday(first); 612 613 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 614 if (jd_flag) { 615 ds = jdaystr; 616 dw = 4; 617 } else { 618 ds = daystr; 619 dw = 3; 620 } 621 622 /* 623 * Fill the lines with day of month or day of year (julian day) 624 * line index: i, each line is one weekday. column index: j, each 625 * column is one day number. print column index: k. 626 */ 627 for (i = 0; i != 7; i++) { 628 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) 629 if (j >= first) { 630 if (jd_flag) 631 dt.d = j - jan1 + 1; 632 else 633 sdate(j, &dt); 634 memcpy(mlines->lines[i] + k, 635 ds + dt.d * dw, dw); 636 } else 637 memcpy(mlines->lines[i] + k, " ", dw); 638 mlines->lines[i][k] = '\0'; 639 640 } 641 642 /* fill the weeknumbers */ 643 if (flag_weeks) { 644 for (j = firstm, k = 0; j < last; k += dw, j += 7) 645 if (j <= nswitch) 646 memset(mlines->weeks + k, ' ', dw); 647 else 648 memcpy(mlines->weeks + k, 649 ds + week(j, &i)*dw, dw); 650 mlines->weeks[k] = '\0'; 651 } 652 } 653 654 void 655 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 656 { 657 658 struct tm tm; /* for strftime printing local names of 659 * months */ 660 date dt; /* handy date */ 661 int dw; /* width of numbers */ 662 int first; /* first day of month */ 663 int firsts; /* sunday of first week of month */ 664 int i, j, k; /* just indices */ 665 int jan1 = 0; /* the first day of this year */ 666 int last; /* the first day of next month */ 667 char *ds; /* pointer to day strings (daystr or 668 * jdaystr) */ 669 670 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 671 if (jd_flag) { 672 ds = jdaystr; 673 dw = 4; 674 } else { 675 ds = daystr; 676 dw = 3; 677 } 678 679 /* Set name of month centered */ 680 memset(&tm, 0, sizeof(tm)); 681 tm.tm_mon = m; 682 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 683 mlines->name[0] = toupper((unsigned char)mlines->name[0]); 684 685 /* 686 * Set first and last to the day number of the first day of this 687 * month and the first day of next month respectively. Set jan1 to 688 * the day number of Jan 1st of this year. 689 */ 690 dt.y = y; 691 dt.m = m + 1; 692 dt.d = 1; 693 first = sndaysb(&dt); 694 if (m == 11) { 695 dt.y = y + 1; 696 dt.m = 1; 697 dt.d = 1; 698 } else { 699 dt.y = y; 700 dt.m = m + 2; 701 dt.d = 1; 702 } 703 last = sndaysb(&dt); 704 705 if (jd_flag) { 706 dt.y = y; 707 dt.m = 1; 708 dt.d = 1; 709 jan1 = sndaysb(&dt); 710 } 711 712 /* 713 * Set firsts to the day number of sunday of the first week of 714 * this month. (This might be in the last month) 715 */ 716 firsts = first - (weekday(first)+1) % 7; 717 718 /* 719 * Fill the lines with day of month or day of year (Julian day) 720 * line index: i, each line is one week. column index: j, each 721 * column is one day number. print column index: k. 722 */ 723 for (i = 0; i != 6; i++) { 724 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 725 j++, k += dw) 726 if (j >= first) { 727 if (jd_flag) 728 dt.d = j - jan1 + 1; 729 else 730 sdateb(j, &dt); 731 memcpy(mlines->lines[i] + k, 732 ds + dt.d * dw, dw); 733 } else 734 memcpy(mlines->lines[i] + k, " ", dw); 735 if (k == 0) 736 mlines->lines[i][1] = '\0'; 737 else 738 mlines->lines[i][k] = '\0'; 739 } 740 } 741 742 /* Put the local names of weekdays into the wds */ 743 void 744 mkweekdays(struct weekdays *wds) 745 { 746 int i, len; 747 struct tm tm; 748 char buf[20]; 749 750 memset(&tm, 0, sizeof(tm)); 751 752 for (i = 0; i != 7; i++) { 753 tm.tm_wday = (i+1) % 7; 754 strftime(buf, sizeof(buf), "%a", &tm); 755 len = strlen(buf); 756 if (len > 2) 757 len = 2; 758 strcpy(wds->names[i], " "); 759 strncpy(wds->names[i] + 2 - len, buf, len); 760 } 761 } 762 763 /* 764 * Compute the day number of the first 765 * existing date after the first day in month. 766 * (the first day in month and even the month might not exist!) 767 */ 768 int 769 firstday(int y, int m) 770 { 771 date dt; 772 int nd; 773 774 dt.y = y; 775 dt.m = m; 776 dt.d = 1; 777 nd = sndays(&dt); 778 for (;;) { 779 sdate(nd, &dt); 780 if ((dt.m >= m && dt.y == y) || dt.y > y) 781 return (nd); 782 else 783 nd++; 784 } 785 /* NEVER REACHED */ 786 } 787 788 /* 789 * Compute the number of days from date, obey the local switch from 790 * Julian to Gregorian if specified by the user. 791 */ 792 int 793 sndays(struct date *d) 794 { 795 796 if (nswitch != 0) 797 if (nswitch < ndaysj(d)) 798 return (ndaysg(d)); 799 else 800 return (ndaysj(d)); 801 else 802 return ndaysg(d); 803 } 804 805 /* 806 * Compute the number of days from date, obey the switch from 807 * Julian to Gregorian as used by UK and her colonies. 808 */ 809 int 810 sndaysb(struct date *d) 811 { 812 813 if (nswitchb < ndaysj(d)) 814 return (ndaysg(d)); 815 else 816 return (ndaysj(d)); 817 } 818 819 /* Inverse of sndays */ 820 struct date * 821 sdate(int nd, struct date *d) 822 { 823 824 if (nswitch < nd) 825 return (gdate(nd, d)); 826 else 827 return (jdate(nd, d)); 828 } 829 830 /* Inverse of sndaysb */ 831 struct date * 832 sdateb(int nd, struct date *d) 833 { 834 835 if (nswitchb < nd) 836 return (gdate(nd, d)); 837 else 838 return (jdate(nd, d)); 839 } 840 841 /* Center string t in string s of length w by putting enough leading blanks */ 842 char * 843 center(char *s, char *t, int w) 844 { 845 char blanks[80]; 846 847 memset(blanks, ' ', sizeof(blanks)); 848 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 849 return (s); 850 } 851