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