ncal.c revision 78016
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 78016 2001-06-10 21:06:01Z 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 {"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 (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 locale = "_US"; 210 q = switches + sizeof(switches) / sizeof(struct djswitch); 211 for (p = switches; p != q; p++) 212 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 213 break; 214 if (p == q) { 215 nswitch = ndaysj(&dftswitch->dt); 216 } else { 217 nswitch = ndaysj(&p->dt); 218 dftswitch = p; 219 } 220 221 222 /* 223 * Get the filename portion of argv[0] and set flag_backward if 224 * this program is called "cal". 225 */ 226 for (cp = argv[0]; *cp; cp++) 227 ; 228 while (cp >= argv[0] && *cp != '/') 229 cp--; 230 if (strcmp("cal", ++cp) == 0) 231 flag_backward = 1; 232 233 /* Set the switch date to United Kingdom if backwards compatible */ 234 if (flag_backward) 235 nswitchb = ndaysj(&ukswitch); 236 237 while ((ch = getopt(argc, argv, "Jejops:wy")) != -1) 238 switch (ch) { 239 case 'J': 240 if (flag_backward) 241 usage(); 242 nswitch = ndaysj(&never); 243 flag_julian_cal = 1; 244 break; 245 case 'e': 246 if (flag_backward) 247 usage(); 248 flag_easter = 1; 249 break; 250 case 'j': 251 flag_julian_day = 1; 252 break; 253 case 'o': 254 if (flag_backward) 255 usage(); 256 flag_orthodox = 1; 257 flag_easter = 1; 258 break; 259 case 'p': 260 if (flag_backward) 261 usage(); 262 printcc(); 263 return (0); 264 break; 265 case 's': 266 if (flag_backward) 267 usage(); 268 q = switches + 269 sizeof(switches) / sizeof(struct djswitch); 270 for (p = switches; 271 p != q && strcmp(p->cc, optarg) != 0; p++) 272 ; 273 if (p == q) 274 errx(EX_USAGE, 275 "%s: invalid country code", optarg); 276 nswitch = ndaysj(&(p->dt)); 277 break; 278 case 'w': 279 if (flag_backward) 280 usage(); 281 flag_weeks = 1; 282 break; 283 case 'y': 284 flag_hole_year = 1; 285 break; 286 default: 287 usage(); 288 } 289 290 argc -= optind; 291 argv += optind; 292 293 if (argc == 0) { 294 time_t t; 295 struct tm *tm; 296 297 t = time(NULL); 298 tm = localtime(&t); 299 y = tm->tm_year + 1900; 300 m = tm->tm_mon + 1; 301 } 302 303 switch (argc) { 304 case 2: 305 if (flag_easter) 306 usage(); 307 m = atoi(*argv++); 308 if (m < 1 || m > 12) 309 errx(EX_USAGE, "month %d not in range 1..12", m); 310 /* FALLTHROUGH */ 311 case 1: 312 y = atoi(*argv++); 313 if (y < 1 || y > 9999) 314 errx(EX_USAGE, "year %d not in range 1..9999", y); 315 break; 316 case 0: 317 break; 318 default: 319 usage(); 320 } 321 322 if (flag_easter) 323 printeaster(y, flag_julian_cal, flag_orthodox); 324 else if (argc == 1 || flag_hole_year) 325 if (flag_backward) 326 printyearb(y, flag_julian_day); 327 else 328 printyear(y, flag_julian_day); 329 else 330 if (flag_backward) 331 printmonthb(y, m, flag_julian_day); 332 else 333 printmonth(y, m, flag_julian_day); 334 335 return (0); 336} 337 338static void 339usage(void) 340{ 341 342 fprintf(stderr, "%s\n%s\n%s\n", 343 "usage: cal [-jy] [[month] year]", 344 " ncal [-Jjpwy] [-s country_code] [[month] year]", 345 " ncal [-Jeo] [year]"); 346 exit(EX_USAGE); 347} 348 349/* print the assumed switches for all countries */ 350void 351printcc(void) 352{ 353 struct djswitch *p; 354 int n; /* number of lines to print */ 355 int m; /* offset from left to right table entry on the same line */ 356 357#define FSTR "%c%s %-15s%4d-%02d-%02d" 358#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 359#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 360 361 n = sizeof(switches) / sizeof(struct djswitch); 362 m = (n + 1) / 2; 363 n /= 2; 364 for (p = switches; p != switches + n; p++) 365 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 366 if (m != n) 367 printf(FSTR"\n", FSTRARG(p)); 368} 369 370/* print the date of easter sunday */ 371void 372printeaster(int y, int julian, int orthodox) 373{ 374 date dt; 375 struct tm tm; 376 char buf[80]; 377 static int d_first = -1; 378 379 if (d_first < 0) 380 d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 381 /* force orthodox easter for years before 1583 */ 382 if (y < 1583) 383 orthodox = 1; 384 385 if (orthodox) 386 if (julian) 387 easteroj(y, &dt); 388 else 389 easterog(y, &dt); 390 else 391 easterg(y, &dt); 392 393 memset(&tm, 0, sizeof(tm)); 394 tm.tm_year = dt.y - 1900; 395 tm.tm_mon = dt.m - 1; 396 tm.tm_mday = dt.d; 397 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 398 printf("%s\n", buf); 399} 400 401void 402printmonth(int y, int m, int jd_flag) 403{ 404 struct monthlines month; 405 struct weekdays wds; 406 int i; 407 408 mkmonth(y, m - 1, jd_flag, &month); 409 mkweekdays(&wds); 410 printf(" %s %d\n", month.name, y); 411 for (i = 0; i != 7; i++) 412 printf("%.2s%s\n", wds.names[i], month.lines[i]); 413 if (flag_weeks) 414 printf(" %s\n", month.weeks); 415} 416 417void 418printmonthb(int y, int m, int jd_flag) 419{ 420 struct monthlines month; 421 struct weekdays wds; 422 char s[MAX_WIDTH], t[MAX_WIDTH]; 423 int i; 424 int mw; 425 426 mkmonthb(y, m - 1, jd_flag, &month); 427 mkweekdays(&wds); 428 429 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 430 431 sprintf(s, "%s %d", month.name, y); 432 printf("%s\n", center(t, s, mw)); 433 434 if (jd_flag) 435 printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0], 436 wds.names[1], wds.names[2], wds.names[3], 437 wds.names[4], wds.names[5]); 438 else 439 printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0], 440 wds.names[1], wds.names[2], wds.names[3], 441 wds.names[4], wds.names[5]); 442 443 for (i = 0; i != 6; i++) 444 printf("%s\n", month.lines[i]+1); 445} 446 447void 448printyear(int y, int jd_flag) 449{ 450 struct monthlines year[12]; 451 struct weekdays wds; 452 char s[80], t[80]; 453 int i, j; 454 int mpl; 455 int mw; 456 457 for (i = 0; i != 12; i++) 458 mkmonth(y, i, jd_flag, year + i); 459 mkweekdays(&wds); 460 mpl = jd_flag ? 3 : 4; 461 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH; 462 463 sprintf(s, "%d", y); 464 printf("%s\n", center(t, s, mpl * mw)); 465 466 for (j = 0; j != 12; j += mpl) { 467 printf(" %-*s%-*s", 468 mw, year[j].name, 469 mw, year[j + 1].name); 470 if (mpl == 3) 471 printf("%s\n", year[j + 2].name); 472 else 473 printf("%-*s%s\n", 474 mw, year[j + 2].name, 475 year[j + 3].name); 476 for (i = 0; i != 7; i++) { 477 printf("%.2s%-*s%-*s", 478 wds.names[i], 479 mw, year[j].lines[i], 480 mw, year[j + 1].lines[i]); 481 if (mpl == 3) 482 printf("%s\n", year[j + 2].lines[i]); 483 else 484 printf("%-*s%s\n", 485 mw, year[j + 2].lines[i], 486 year[j + 3].lines[i]); 487 } 488 if (flag_weeks) { 489 if (mpl == 3) 490 printf(" %-*s%-*s%-s\n", 491 mw, year[j].weeks, 492 mw, year[j + 1].weeks, 493 year[j + 2].weeks); 494 else 495 printf(" %-*s%-*s%-*s%-s\n", 496 mw, year[j].weeks, 497 mw, year[j + 1].weeks, 498 mw, year[j + 2].weeks, 499 year[j + 3].weeks); 500 } 501 } 502} 503 504void 505printyearb(int y, int jd_flag) 506{ 507 struct monthlines year[12]; 508 struct weekdays wds; 509 char s[80], t[80]; 510 int i, j; 511 int mpl; 512 int mw; 513 514 for (i = 0; i != 12; i++) 515 mkmonthb(y, i, jd_flag, year + i); 516 mkweekdays(&wds); 517 mpl = jd_flag ? 2 : 3; 518 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 519 520 sprintf(s, "%d", y); 521 printf("%s\n\n", center(t, s, mw * mpl + mpl)); 522 523 for (j = 0; j != 12; j += mpl) { 524 printf("%-*s ", mw, center(s, year[j].name, mw)); 525 if (mpl == 2) 526 printf("%s\n", center(s, year[j + 1].name, mw)); 527 else 528 printf("%-*s %s\n", mw, 529 center(s, year[j + 1].name, mw), 530 center(t, year[j + 2].name, mw)); 531 532 if (mpl == 2) 533 printf(" %s %s %s %s %s %s %s " 534 " %s %s %s %s %s %s %.2s\n", 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 wds.names[6], wds.names[0], wds.names[1], 539 wds.names[2], wds.names[3], wds.names[4], 540 wds.names[5]); 541 else 542 printf("%s%s%s%s%s%s%s " 543 "%s%s%s%s%s%s%s " 544 "%s%s%s%s%s%s%.2s\n", 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 wds.names[6], wds.names[0], wds.names[1], 552 wds.names[2], wds.names[3], wds.names[4], 553 wds.names[5]); 554 for (i = 0; i != 6; i++) { 555 if (mpl == 2) 556 printf("%-*s %s\n", 557 mw, year[j].lines[i]+1, 558 year[j + 1].lines[i]+1); 559 else 560 printf("%-*s %-*s %s\n", 561 mw, year[j].lines[i]+1, 562 mw, year[j + 1].lines[i]+1, 563 year[j + 2].lines[i]+1); 564 565 } 566 } 567} 568 569void 570mkmonth(int y, int m, int jd_flag, struct monthlines *mlines) 571{ 572 573 struct tm tm; /* for strftime printing local names of 574 * months */ 575 date dt; /* handy date */ 576 int dw; /* width of numbers */ 577 int first; /* first day of month */ 578 int firstm; /* first day of first week of month */ 579 int i, j, k; /* just indices */ 580 int last; /* the first day of next month */ 581 int jan1 = 0; /* the first day of this year */ 582 char *ds; /* pointer to day strings (daystr or 583 * jdaystr) */ 584 585 /* Set name of month. */ 586 memset(&tm, 0, sizeof(tm)); 587 tm.tm_mon = m; 588 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 589 mlines->name[0] = toupper((unsigned char)mlines->name[0]); 590 591 /* 592 * Set first and last to the day number of the first day of this 593 * month and the first day of next month respectively. Set jan1 to 594 * the day number of the first day of this year. 595 */ 596 first = firstday(y, m + 1); 597 if (m == 11) 598 last = firstday(y + 1, 1); 599 else 600 last = firstday(y, m + 2); 601 602 if (jd_flag) 603 jan1 = firstday(y, 1); 604 605 /* 606 * Set firstm to the day number of monday of the first week of 607 * this month. (This might be in the last month) 608 */ 609 firstm = first - weekday(first); 610 611 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 612 if (jd_flag) { 613 ds = jdaystr; 614 dw = 4; 615 } else { 616 ds = daystr; 617 dw = 3; 618 } 619 620 /* 621 * Fill the lines with day of month or day of year (julian day) 622 * line index: i, each line is one weekday. column index: j, each 623 * column is one day number. print column index: k. 624 */ 625 for (i = 0; i != 7; i++) { 626 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) 627 if (j >= first) { 628 if (jd_flag) 629 dt.d = j - jan1 + 1; 630 else 631 sdate(j, &dt); 632 memcpy(mlines->lines[i] + k, 633 ds + dt.d * dw, dw); 634 } else 635 memcpy(mlines->lines[i] + k, " ", dw); 636 mlines->lines[i][k] = '\0'; 637 638 } 639 640 /* fill the weeknumbers */ 641 if (flag_weeks) { 642 for (j = firstm, k = 0; j < last; k += dw, j += 7) 643 if (j <= nswitch) 644 memset(mlines->weeks + k, ' ', dw); 645 else 646 memcpy(mlines->weeks + k, 647 ds + week(j, &i)*dw, dw); 648 mlines->weeks[k] = '\0'; 649 } 650} 651 652void 653mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 654{ 655 656 struct tm tm; /* for strftime printing local names of 657 * months */ 658 date dt; /* handy date */ 659 int dw; /* width of numbers */ 660 int first; /* first day of month */ 661 int firsts; /* sunday of first week of month */ 662 int i, j, k; /* just indices */ 663 int jan1 = 0; /* the first day of this year */ 664 int last; /* the first day of next month */ 665 char *ds; /* pointer to day strings (daystr or 666 * jdaystr) */ 667 668 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 669 if (jd_flag) { 670 ds = jdaystr; 671 dw = 4; 672 } else { 673 ds = daystr; 674 dw = 3; 675 } 676 677 /* Set name of month centered */ 678 memset(&tm, 0, sizeof(tm)); 679 tm.tm_mon = m; 680 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 681 mlines->name[0] = toupper((unsigned char)mlines->name[0]); 682 683 /* 684 * Set first and last to the day number of the first day of this 685 * month and the first day of next month respectively. Set jan1 to 686 * the day number of Jan 1st of this year. 687 */ 688 dt.y = y; 689 dt.m = m + 1; 690 dt.d = 1; 691 first = sndaysb(&dt); 692 if (m == 11) { 693 dt.y = y + 1; 694 dt.m = 1; 695 dt.d = 1; 696 } else { 697 dt.y = y; 698 dt.m = m + 2; 699 dt.d = 1; 700 } 701 last = sndaysb(&dt); 702 703 if (jd_flag) { 704 dt.y = y; 705 dt.m = 1; 706 dt.d = 1; 707 jan1 = sndaysb(&dt); 708 } 709 710 /* 711 * Set firsts to the day number of sunday of the first week of 712 * this month. (This might be in the last month) 713 */ 714 firsts = first - (weekday(first)+1) % 7; 715 716 /* 717 * Fill the lines with day of month or day of year (Julian day) 718 * line index: i, each line is one week. column index: j, each 719 * column is one day number. print column index: k. 720 */ 721 for (i = 0; i != 6; i++) { 722 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 723 j++, k += dw) 724 if (j >= first) { 725 if (jd_flag) 726 dt.d = j - jan1 + 1; 727 else 728 sdateb(j, &dt); 729 memcpy(mlines->lines[i] + k, 730 ds + dt.d * dw, dw); 731 } else 732 memcpy(mlines->lines[i] + k, " ", dw); 733 if (k == 0) 734 mlines->lines[i][1] = '\0'; 735 else 736 mlines->lines[i][k] = '\0'; 737 } 738} 739 740/* Put the local names of weekdays into the wds */ 741void 742mkweekdays(struct weekdays *wds) 743{ 744 int i, len; 745 struct tm tm; 746 char buf[20]; 747 748 memset(&tm, 0, sizeof(tm)); 749 750 for (i = 0; i != 7; i++) { 751 tm.tm_wday = (i+1) % 7; 752 strftime(buf, sizeof(buf), "%a", &tm); 753 len = strlen(buf); 754 if (len > 2) 755 len = 2; 756 strcpy(wds->names[i], " "); 757 strncpy(wds->names[i] + 2 - len, buf, len); 758 } 759} 760 761/* 762 * Compute the day number of the first 763 * existing date after the first day in month. 764 * (the first day in month and even the month might not exist!) 765 */ 766int 767firstday(int y, int m) 768{ 769 date dt; 770 int nd; 771 772 dt.y = y; 773 dt.m = m; 774 dt.d = 1; 775 nd = sndays(&dt); 776 for (;;) { 777 sdate(nd, &dt); 778 if ((dt.m >= m && dt.y == y) || dt.y > y) 779 return (nd); 780 else 781 nd++; 782 } 783 /* NEVER REACHED */ 784} 785 786/* 787 * Compute the number of days from date, obey the local switch from 788 * Julian to Gregorian if specified by the user. 789 */ 790int 791sndays(struct date *d) 792{ 793 794 if (nswitch != 0) 795 if (nswitch < ndaysj(d)) 796 return (ndaysg(d)); 797 else 798 return (ndaysj(d)); 799 else 800 return ndaysg(d); 801} 802 803/* 804 * Compute the number of days from date, obey the switch from 805 * Julian to Gregorian as used by UK and her colonies. 806 */ 807int 808sndaysb(struct date *d) 809{ 810 811 if (nswitchb < ndaysj(d)) 812 return (ndaysg(d)); 813 else 814 return (ndaysj(d)); 815} 816 817/* Inverse of sndays */ 818struct date * 819sdate(int nd, struct date *d) 820{ 821 822 if (nswitch < nd) 823 return (gdate(nd, d)); 824 else 825 return (jdate(nd, d)); 826} 827 828/* Inverse of sndaysb */ 829struct date * 830sdateb(int nd, struct date *d) 831{ 832 833 if (nswitchb < nd) 834 return (gdate(nd, d)); 835 else 836 return (jdate(nd, d)); 837} 838 839/* Center string t in string s of length w by putting enough leading blanks */ 840char * 841center(char *s, char *t, int w) 842{ 843 char blanks[80]; 844 845 memset(blanks, ' ', sizeof(blanks)); 846 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 847 return (s); 848} 849