Deleted Added
full compact
ncal.c (186401) ncal.c (189804)
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

--- 12 unchanged lines hidden (view full) ---

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[] =
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

--- 12 unchanged lines hidden (view full) ---

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 186401 2008-12-22 21:22:42Z wollman $";
29 "$FreeBSD: head/usr.bin/ncal/ncal.c 189804 2009-03-14 18:55:51Z das $";
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>
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#include <wchar.h>
44#include <wctype.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 {
45
46/* Width of one month with backward compatibility */
47#define MONTH_WIDTH_B_J 27
48#define MONTH_WIDTH_B 20
49
50#define MONTH_WIDTH_J 24
51#define MONTH_WIDTH 18
52
53#define MAX_WIDTH 28
54
55typedef struct date date;
56
57struct monthlines {
56 char name[MAX_WIDTH + 1];
58 wchar_t name[MAX_WIDTH + 1];
57 char lines[7][MAX_WIDTH + 1];
58 char weeks[MAX_WIDTH + 1];
59};
60
61struct weekdays {
59 char lines[7][MAX_WIDTH + 1];
60 char weeks[MAX_WIDTH + 1];
61};
62
63struct weekdays {
62 char names[7][4];
64 wchar_t names[7][4];
63};
64
65/* The switches from Julian to Gregorian in some countries */
66static struct djswitch {
67 const char *cc; /* Country code according to ISO 3166 */
68 const char *nm; /* Name of country */
69 date dt; /* Last day of Julian calendar */
70} switches[] = {

--- 83 unchanged lines hidden (view full) ---

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);
65};
66
67/* The switches from Julian to Gregorian in some countries */
68static struct djswitch {
69 const char *cc; /* Country code according to ISO 3166 */
70 const char *nm; /* Name of country */
71 date dt; /* Last day of Julian calendar */
72} switches[] = {

--- 83 unchanged lines hidden (view full) ---

156 " 350 351 352 353 354 355 356 357 358 359"
157 " 360 361 362 363 364 365 366";
158
159int flag_weeks; /* user wants number of week */
160int nswitch; /* user defined switch date */
161int nswitchb; /* switch date for backward compatibility */
162
163char *center(char *s, char *t, int w);
164wchar_t *wcenter(wchar_t *s, wchar_t *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);
165int parsemonth(const char *s, int *m, int *y);
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);

--- 243 unchanged lines hidden (view full) ---

413printmonth(int y, int m, int jd_flag)
414{
415 struct monthlines month;
416 struct weekdays wds;
417 int i;
418
419 mkmonth(y, m - 1, jd_flag, &month);
420 mkweekdays(&wds);
165void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
166void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
167void mkweekdays(struct weekdays * wds);
168int parsemonth(const char *s, int *m, int *y);
169void printcc(void);
170void printeaster(int year, int julian, int orthodox);
171void printmonth(int year, int month, int jd_flag);
172void printmonthb(int year, int month, int jd_flag);

--- 243 unchanged lines hidden (view full) ---

416printmonth(int y, int m, int jd_flag)
417{
418 struct monthlines month;
419 struct weekdays wds;
420 int i;
421
422 mkmonth(y, m - 1, jd_flag, &month);
423 mkweekdays(&wds);
421 printf(" %s %d\n", month.name, y);
424 printf(" %ls %d\n", month.name, y);
422 for (i = 0; i != 7; i++)
425 for (i = 0; i != 7; i++)
423 printf("%.2s%s\n", wds.names[i], month.lines[i]);
426 printf("%.2ls%s\n", wds.names[i], month.lines[i]);
424 if (flag_weeks)
425 printf(" %s\n", month.weeks);
426}
427
428void
429printmonthb(int y, int m, int jd_flag)
430{
431 struct monthlines month;
432 struct weekdays wds;
427 if (flag_weeks)
428 printf(" %s\n", month.weeks);
429}
430
431void
432printmonthb(int y, int m, int jd_flag)
433{
434 struct monthlines month;
435 struct weekdays wds;
433 char s[MAX_WIDTH], t[MAX_WIDTH];
436 wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
434 int i;
435 int mw;
436
437 mkmonthb(y, m - 1, jd_flag, &month);
438 mkweekdays(&wds);
439
440 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
441
437 int i;
438 int mw;
439
440 mkmonthb(y, m - 1, jd_flag, &month);
441 mkweekdays(&wds);
442
443 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
444
442 sprintf(s, "%s %d", month.name, y);
443 printf("%s\n", center(t, s, mw));
445 swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
446 wprintf(L"%ls\n", wcenter(t, s, mw));
444
445 if (jd_flag)
447
448 if (jd_flag)
446 printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
449 wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
450 wds.names[6], wds.names[0],
447 wds.names[1], wds.names[2], wds.names[3],
448 wds.names[4], wds.names[5]);
449 else
451 wds.names[1], wds.names[2], wds.names[3],
452 wds.names[4], wds.names[5]);
453 else
450 printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
451 wds.names[1], wds.names[2], wds.names[3],
454 wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6],
455 wds.names[0], wds.names[1], wds.names[2], wds.names[3],
452 wds.names[4], wds.names[5]);
453
454 for (i = 0; i != 6; i++)
455 printf("%s\n", month.lines[i]+1);
456}
457
458void
459printyear(int y, int jd_flag)

--- 10 unchanged lines hidden (view full) ---

470 mkweekdays(&wds);
471 mpl = jd_flag ? 3 : 4;
472 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
473
474 sprintf(s, "%d", y);
475 printf("%s\n", center(t, s, mpl * mw));
476
477 for (j = 0; j != 12; j += mpl) {
456 wds.names[4], wds.names[5]);
457
458 for (i = 0; i != 6; i++)
459 printf("%s\n", month.lines[i]+1);
460}
461
462void
463printyear(int y, int jd_flag)

--- 10 unchanged lines hidden (view full) ---

474 mkweekdays(&wds);
475 mpl = jd_flag ? 3 : 4;
476 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
477
478 sprintf(s, "%d", y);
479 printf("%s\n", center(t, s, mpl * mw));
480
481 for (j = 0; j != 12; j += mpl) {
478 printf(" %-*s%-*s",
482 printf(" %-*ls%-*ls",
479 mw, year[j].name,
480 mw, year[j + 1].name);
481 if (mpl == 3)
483 mw, year[j].name,
484 mw, year[j + 1].name);
485 if (mpl == 3)
482 printf("%s\n", year[j + 2].name);
486 printf("%ls\n", year[j + 2].name);
483 else
487 else
484 printf("%-*s%s\n",
488 printf("%-*ls%ls\n",
485 mw, year[j + 2].name,
486 year[j + 3].name);
487 for (i = 0; i != 7; i++) {
489 mw, year[j + 2].name,
490 year[j + 3].name);
491 for (i = 0; i != 7; i++) {
488 printf("%.2s%-*s%-*s",
492 printf("%.2ls%-*s%-*s",
489 wds.names[i],
490 mw, year[j].lines[i],
491 mw, year[j + 1].lines[i]);
492 if (mpl == 3)
493 printf("%s\n", year[j + 2].lines[i]);
494 else
495 printf("%-*s%s\n",
496 mw, year[j + 2].lines[i],

--- 16 unchanged lines hidden (view full) ---

513}
514
515void
516printyearb(int y, int jd_flag)
517{
518 struct monthlines year[12];
519 struct weekdays wds;
520 char s[80], t[80];
493 wds.names[i],
494 mw, year[j].lines[i],
495 mw, year[j + 1].lines[i]);
496 if (mpl == 3)
497 printf("%s\n", year[j + 2].lines[i]);
498 else
499 printf("%-*s%s\n",
500 mw, year[j + 2].lines[i],

--- 16 unchanged lines hidden (view full) ---

517}
518
519void
520printyearb(int y, int jd_flag)
521{
522 struct monthlines year[12];
523 struct weekdays wds;
524 char s[80], t[80];
525 wchar_t ws[80], wt[80];
521 int i, j;
522 int mpl;
523 int mw;
524
525 for (i = 0; i != 12; i++)
526 mkmonthb(y, i, jd_flag, year + i);
527 mkweekdays(&wds);
528 mpl = jd_flag ? 2 : 3;
529 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
530
531 sprintf(s, "%d", y);
532 printf("%s\n\n", center(t, s, mw * mpl + mpl));
533
534 for (j = 0; j != 12; j += mpl) {
526 int i, j;
527 int mpl;
528 int mw;
529
530 for (i = 0; i != 12; i++)
531 mkmonthb(y, i, jd_flag, year + i);
532 mkweekdays(&wds);
533 mpl = jd_flag ? 2 : 3;
534 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
535
536 sprintf(s, "%d", y);
537 printf("%s\n\n", center(t, s, mw * mpl + mpl));
538
539 for (j = 0; j != 12; j += mpl) {
535 printf("%-*s ", mw, center(s, year[j].name, mw));
540 printf("%-*ls ", mw, wcenter(ws, year[j].name, mw));
536 if (mpl == 2)
541 if (mpl == 2)
537 printf("%s\n", center(s, year[j + 1].name, mw));
542 printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
538 else
543 else
539 printf("%-*s %s\n", mw,
540 center(s, year[j + 1].name, mw),
541 center(t, year[j + 2].name, mw));
544 printf("%-*ls %ls\n", mw,
545 wcenter(ws, year[j + 1].name, mw),
546 wcenter(wt, year[j + 2].name, mw));
542
543 if (mpl == 2)
547
548 if (mpl == 2)
544 printf(" %s %s %s %s %s %s %s "
545 " %s %s %s %s %s %s %.2s\n",
549 wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
550 " %ls %ls %ls %ls %ls %ls %.2ls\n",
546 wds.names[6], wds.names[0], wds.names[1],
547 wds.names[2], wds.names[3], wds.names[4],
548 wds.names[5],
549 wds.names[6], wds.names[0], wds.names[1],
550 wds.names[2], wds.names[3], wds.names[4],
551 wds.names[5]);
552 else
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 wds.names[6], wds.names[0], wds.names[1],
555 wds.names[2], wds.names[3], wds.names[4],
556 wds.names[5]);
557 else
553 printf("%s%s%s%s%s%s%s "
554 "%s%s%s%s%s%s%s "
555 "%s%s%s%s%s%s%.2s\n",
558 wprintf(L"%ls%ls%ls%ls%ls%ls%ls "
559 "%ls%ls%ls%ls%ls%ls%ls "
560 "%ls%ls%ls%ls%ls%ls%.2ls\n",
556 wds.names[6], wds.names[0], wds.names[1],
557 wds.names[2], wds.names[3], wds.names[4],
558 wds.names[5],
559 wds.names[6], wds.names[0], wds.names[1],
560 wds.names[2], wds.names[3], wds.names[4],
561 wds.names[5],
562 wds.names[6], wds.names[0], wds.names[1],
563 wds.names[2], wds.names[3], wds.names[4],

--- 27 unchanged lines hidden (view full) ---

591 int last; /* the first day of next month */
592 int jan1 = 0; /* the first day of this year */
593 char *ds; /* pointer to day strings (daystr or
594 * jdaystr) */
595
596 /* Set name of month. */
597 memset(&tm, 0, sizeof(tm));
598 tm.tm_mon = m;
561 wds.names[6], wds.names[0], wds.names[1],
562 wds.names[2], wds.names[3], wds.names[4],
563 wds.names[5],
564 wds.names[6], wds.names[0], wds.names[1],
565 wds.names[2], wds.names[3], wds.names[4],
566 wds.names[5],
567 wds.names[6], wds.names[0], wds.names[1],
568 wds.names[2], wds.names[3], wds.names[4],

--- 27 unchanged lines hidden (view full) ---

596 int last; /* the first day of next month */
597 int jan1 = 0; /* the first day of this year */
598 char *ds; /* pointer to day strings (daystr or
599 * jdaystr) */
600
601 /* Set name of month. */
602 memset(&tm, 0, sizeof(tm));
603 tm.tm_mon = m;
599 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
600 mlines->name[0] = toupper((unsigned char)mlines->name[0]);
604 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
605 L"%OB", &tm);
606 mlines->name[0] = towupper(mlines->name[0]);
601
602 /*
603 * Set first and last to the day number of the first day of this
604 * month and the first day of next month respectively. Set jan1 to
605 * the day number of the first day of this year.
606 */
607 first = firstday(y, m + 1);
608 if (m == 11)

--- 74 unchanged lines hidden (view full) ---

683 } else {
684 ds = daystr;
685 dw = 3;
686 }
687
688 /* Set name of month centered */
689 memset(&tm, 0, sizeof(tm));
690 tm.tm_mon = m;
607
608 /*
609 * Set first and last to the day number of the first day of this
610 * month and the first day of next month respectively. Set jan1 to
611 * the day number of the first day of this year.
612 */
613 first = firstday(y, m + 1);
614 if (m == 11)

--- 74 unchanged lines hidden (view full) ---

689 } else {
690 ds = daystr;
691 dw = 3;
692 }
693
694 /* Set name of month centered */
695 memset(&tm, 0, sizeof(tm));
696 tm.tm_mon = m;
691 strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
692 mlines->name[0] = toupper((unsigned char)mlines->name[0]);
697 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
698 L"%OB", &tm);
699 mlines->name[0] = towupper(mlines->name[0]);
693
694 /*
695 * Set first and last to the day number of the first day of this
696 * month and the first day of next month respectively. Set jan1 to
697 * the day number of Jan 1st of this year.
698 */
699 dt.y = y;
700 dt.m = m + 1;

--- 48 unchanged lines hidden (view full) ---

749}
750
751/* Put the local names of weekdays into the wds */
752void
753mkweekdays(struct weekdays *wds)
754{
755 int i, len;
756 struct tm tm;
700
701 /*
702 * Set first and last to the day number of the first day of this
703 * month and the first day of next month respectively. Set jan1 to
704 * the day number of Jan 1st of this year.
705 */
706 dt.y = y;
707 dt.m = m + 1;

--- 48 unchanged lines hidden (view full) ---

756}
757
758/* Put the local names of weekdays into the wds */
759void
760mkweekdays(struct weekdays *wds)
761{
762 int i, len;
763 struct tm tm;
757 char buf[20];
764 wchar_t buf[20];
758
759 memset(&tm, 0, sizeof(tm));
760
761 for (i = 0; i != 7; i++) {
762 tm.tm_wday = (i+1) % 7;
765
766 memset(&tm, 0, sizeof(tm));
767
768 for (i = 0; i != 7; i++) {
769 tm.tm_wday = (i+1) % 7;
763 strftime(buf, sizeof(buf), "%a", &tm);
764 len = strlen(buf);
770 wcsftime(buf, sizeof(buf), L"%a", &tm);
771 len = wcslen(buf);
765 if (len > 2)
766 len = 2;
772 if (len > 2)
773 len = 2;
767 strcpy(wds->names[i], " ");
768 strncpy(wds->names[i] + 2 - len, buf, len);
774 wcscpy(wds->names[i], L" ");
775 wcsncpy(wds->names[i] + 2 - len, buf, len);
769 }
770}
771
772/*
773 * Compute the day number of the first
774 * existing date after the first day in month.
775 * (the first day in month and even the month might not exist!)
776 */

--- 76 unchanged lines hidden (view full) ---

853{
854 char blanks[80];
855
856 memset(blanks, ' ', sizeof(blanks));
857 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
858 return (s);
859}
860
776 }
777}
778
779/*
780 * Compute the day number of the first
781 * existing date after the first day in month.
782 * (the first day in month and even the month might not exist!)
783 */

--- 76 unchanged lines hidden (view full) ---

860{
861 char blanks[80];
862
863 memset(blanks, ' ', sizeof(blanks));
864 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
865 return (s);
866}
867
868/* Center string t in string s of length w by putting enough leading blanks */
869wchar_t *
870wcenter(wchar_t *s, wchar_t *t, int w)
871{
872 char blanks[80];
873
874 memset(blanks, ' ', sizeof(blanks));
875 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
876 return (s);
877}
878
861int
862parsemonth(const char *s, int *m, int *y)
863{
864 int nm, ny;
865 char *cp;
866 struct tm tm;
867
868 nm = (int)strtol(s, &cp, 10);

--- 24 unchanged lines hidden ---
879int
880parsemonth(const char *s, int *m, int *y)
881{
882 int nm, ny;
883 char *cp;
884 struct tm tm;
885
886 nm = (int)strtol(s, &cp, 10);

--- 24 unchanged lines hidden ---