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