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