ncal.c revision 53963
131744Shelbig/*-
231744Shelbig * Copyright (c) 1997 Wolfgang Helbig
331744Shelbig * All rights reserved.
431744Shelbig *
531744Shelbig * Redistribution and use in source and binary forms, with or without
631744Shelbig * modification, are permitted provided that the following conditions
731744Shelbig * are met:
831744Shelbig * 1. Redistributions of source code must retain the above copyright
931744Shelbig *    notice, this list of conditions and the following disclaimer.
1031744Shelbig * 2. Redistributions in binary form must reproduce the above copyright
1131744Shelbig *    notice, this list of conditions and the following disclaimer in the
1231744Shelbig *    documentation and/or other materials provided with the distribution.
1331744Shelbig *
1431744Shelbig * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1531744Shelbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1631744Shelbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1731744Shelbig * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1831744Shelbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1931744Shelbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2031744Shelbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2131744Shelbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2231744Shelbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2331744Shelbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2431744Shelbig * SUCH DAMAGE.
2531744Shelbig */
2632310Scharnier
2732310Scharnier#ifndef lint
2832310Scharnierstatic const char rcsid[] =
2950477Speter  "$FreeBSD: head/usr.bin/ncal/ncal.c 53963 1999-11-30 20:08:50Z ache $";
3032310Scharnier#endif /* not lint */
3132310Scharnier
3231744Shelbig#include <calendar.h>
3331744Shelbig#include <err.h>
3431744Shelbig#include <locale.h>
3531744Shelbig#include <stdio.h>
3631744Shelbig#include <stdlib.h>
3731744Shelbig#include <string.h>
3831744Shelbig#include <sysexits.h>
3931744Shelbig#include <time.h>
4031744Shelbig#include <unistd.h>
4131744Shelbig
4231744Shelbig/* Width of one month with backward compatibility */
4331744Shelbig#define MONTH_WIDTH_B_J 27
4431744Shelbig#define MONTH_WIDTH_B 20
4531744Shelbig
4631744Shelbig#define MONTH_WIDTH_J 24
4731744Shelbig#define MONTH_WIDTH 18
4831744Shelbig
4931744Shelbig#define MAX_WIDTH 28
5031744Shelbig
5131744Shelbigtypedef struct date date;
5231744Shelbig
5331744Shelbigstruct monthlines {
5431744Shelbig	char name[MAX_WIDTH + 1];
5531744Shelbig	char lines[7][MAX_WIDTH + 1];
5631744Shelbig	char weeks[MAX_WIDTH + 1];
5731744Shelbig};
5831744Shelbig
5931744Shelbigstruct weekdays {
6031744Shelbig	char names[7][4];
6131744Shelbig};
6231744Shelbig
6331744Shelbig/* The switches from Julian to Gregorian in some countries */
6431744Shelbigstatic struct djswitch {
6531744Shelbig	char *cc;	/* Country code according to ISO 3166 */
6631744Shelbig	char *nm;	/* Name of country */
6731744Shelbig	date dt;	/* Last day of Julian calendar */
6831744Shelbig} switches[] = {
6953963Sache	{"AL", "Albania",       {1912, 11, 30}},
7053963Sache	{"AT", "Austria",       {1583, 10,  5}},
7153963Sache	{"AU", "Australia",     {1752,  9,  2}},
7253963Sache	{"BE", "Belgium",       {1582, 12, 14}},
7353963Sache	{"BG", "Bulgaria",      {1916,  3, 18}},
7453963Sache	{"CA", "Canada",        {1752,  9,  2}},
7553963Sache	{"CH", "Switzerland",   {1655,  2, 28}},
7653963Sache	{"CN", "China",         {1911, 12, 18}},
7753963Sache	{"CZ", "Czech Republic",{1584,  1,  6}},
7853963Sache	{"DE", "Germany",       {1700,  2, 18}},
7953963Sache	{"DK", "Denmark",       {1700,  2, 18}},
8053963Sache	{"ES", "Spain",         {1582, 10,  4}},
8153963Sache	{"FI", "Finland",       {1753,  2, 17}},
8253963Sache	{"FR", "France",        {1582, 12,  9}},
8353963Sache	{"GB", "United Kingdom",{1752,  9,  2}},
8453963Sache	{"GR", "Greece",        {1924,  3,  9}},
8553963Sache	{"HU", "Hungary",       {1587, 10, 21}},
8653963Sache	{"IS", "Iceland",       {1700, 11, 16}},
8753963Sache	{"IT", "Italy",         {1582, 10,  4}},
8853963Sache	{"JP", "Japan",         {1918, 12, 18}},
8953963Sache	{"LI", "Lithuania",     {1918,  2,  1}},
9053963Sache	{"LN", "Latin",         {9999, 05, 31}},
9153963Sache	{"LU", "Luxembourg",    {1582, 12, 14}},
9253963Sache	{"LV", "Latvia",        {1918,  2,  1}},
9353963Sache	{"NL", "Netherlands",   {1582, 12, 14}},
9453963Sache	{"NO", "Norway",        {1700,  2, 18}},
9553963Sache	{"PL", "Poland",        {1582, 10,  4}},
9653963Sache	{"PT", "Portugal",      {1582, 10,  4}},
9753963Sache	{"RO", "Romania",       {1919,  3, 31}},
9853963Sache	{"RU", "Russia",        {1918,  1, 31}},
9953963Sache	{"SI", "Slovenia",      {1919,  3,  4}},
10053963Sache	{"SU", "USSR",          {1920,  3,  4}},
10153963Sache	{"SW", "Sweden",        {1753,  2, 17}},
10253963Sache	{"TR", "Turkey",        {1926, 12, 18}},
10353963Sache	{"US", "United States", {1752,  9,  2}},
10453963Sache	{"YU", "Yugoslavia",    {1919,  3,  4}}
10531744Shelbig};
10631744Shelbig
10732215Shelbigstruct djswitch *dftswitch =
10832215Shelbig    switches + sizeof(switches) / sizeof(struct djswitch) - 2;
10932215Shelbig    /* default switch (should be "US") */
11032215Shelbig
11131744Shelbig/* Table used to print day of month and week numbers */
11231744Shelbigchar daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
11331744Shelbig		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
11431744Shelbig		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
11531744Shelbig		" 48 49 50 51 52 53";
11631744Shelbig
11731744Shelbig/* Table used to print day of year and week numbers */
11831744Shelbigchar jdaystr[] = "       1   2   3   4   5   6   7   8   9"
11931744Shelbig		 "  10  11  12  13  14  15  16  17  18  19"
12031744Shelbig		 "  20  21  22  23  24  25  26  27  28  29"
12131744Shelbig		 "  30  31  32  33  34  35  36  37  38  39"
12231744Shelbig		 "  40  41  42  43  44  45  46  47  48  49"
12331744Shelbig		 "  50  51  52  53  54  55  56  57  58  59"
12431744Shelbig		 "  60  61  62  63  64  65  66  67  68  69"
12531744Shelbig		 "  70  71  72  73  74  75  76  77  78  79"
12631744Shelbig		 "  80  81  82  83  84  85  86  87  88  89"
12731744Shelbig		 "  90  91  92  93  94  95  96  97  98  99"
12831744Shelbig		 " 100 101 102 103 104 105 106 107 108 109"
12931744Shelbig		 " 110 111 112 113 114 115 116 117 118 119"
13031744Shelbig		 " 120 121 122 123 124 125 126 127 128 129"
13131744Shelbig		 " 130 131 132 133 134 135 136 137 138 139"
13231744Shelbig		 " 140 141 142 143 144 145 146 147 148 149"
13331744Shelbig		 " 150 151 152 153 154 155 156 157 158 159"
13431744Shelbig		 " 160 161 162 163 164 165 166 167 168 169"
13531744Shelbig		 " 170 171 172 173 174 175 176 177 178 179"
13631744Shelbig		 " 180 181 182 183 184 185 186 187 188 189"
13731744Shelbig		 " 190 191 192 193 194 195 196 197 198 199"
13831744Shelbig		 " 200 201 202 203 204 205 206 207 208 209"
13931744Shelbig		 " 210 211 212 213 214 215 216 217 218 219"
14031744Shelbig		 " 220 221 222 223 224 225 226 227 228 229"
14131744Shelbig		 " 230 231 232 233 234 235 236 237 238 239"
14231744Shelbig		 " 240 241 242 243 244 245 246 247 248 249"
14331744Shelbig		 " 250 251 252 253 254 255 256 257 258 259"
14431744Shelbig		 " 260 261 262 263 264 265 266 267 268 269"
14531744Shelbig		 " 270 271 272 273 274 275 276 277 278 279"
14631744Shelbig		 " 280 281 282 283 284 285 286 287 288 289"
14731744Shelbig		 " 290 291 292 293 294 295 296 297 298 299"
14831744Shelbig		 " 300 301 302 303 304 305 306 307 308 309"
14931744Shelbig		 " 310 311 312 313 314 315 316 317 318 319"
15031744Shelbig		 " 320 321 322 323 324 325 326 327 328 329"
15131744Shelbig		 " 330 331 332 333 334 335 336 337 338 339"
15231744Shelbig		 " 340 341 342 343 344 345 346 347 348 349"
15331744Shelbig		 " 350 351 352 353 354 355 356 357 358 359"
15431744Shelbig		 " 360 361 362 363 364 365 366";
15531744Shelbig
15632139Shelbigint     flag_weeks;		/* user wants number of week */
15731744Shelbigint     nswitch;		/* user defined switch date */
15831744Shelbigint	nswitchb;		/* switch date for backward compatibility */
15931744Shelbig
16031744Shelbigchar   *center(char *s, char *t, int w);
16131744Shelbigvoid	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
16231744Shelbigvoid    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
16331744Shelbigvoid    mkweekdays(struct weekdays * wds);
16431744Shelbigvoid    printcc(void);
16531744Shelbigvoid    printeaster(int year, int julian, int orthodox);
16631744Shelbigvoid    printmonth(int year, int month, int jd_flag);
16731744Shelbigvoid    printmonthb(int year, int month, int jd_flag);
16831744Shelbigvoid    printyear(int year, int jd_flag);
16931744Shelbigvoid    printyearb(int year, int jd_flag);
17032282Shelbigint	firstday(int y, int m);
17131744Shelbigdate   *sdate(int ndays, struct date * d);
17231744Shelbigdate   *sdateb(int ndays, struct date * d);
17331744Shelbigint     sndays(struct date * d);
17431744Shelbigint     sndaysb(struct date * d);
17532310Scharnierstatic void usage(void);
17631744Shelbigint     weekdayb(int nd);
17731744Shelbig
17831744Shelbigint
17931744Shelbigmain(int argc, char *argv[])
18031744Shelbig{
18131744Shelbig	struct  djswitch *p, *q;	/* to search user defined switch date */
18231744Shelbig	date	never = {10000, 1, 1};	/* outside valid range of dates */
18331744Shelbig	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
18431744Shelbig	int     ch;			/* holds the option character */
18531744Shelbig	int     m = 0;			/* month */
18631744Shelbig	int	y = 0;			/* year */
18731744Shelbig	int     flag_backward = 0;	/* user called cal--backward compat. */
18831744Shelbig	int     flag_hole_year = 0;	/* user wants the whole year */
18931744Shelbig	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
19031744Shelbig	int     flag_julian_day = 0;	/* user wants the Julian day
19131744Shelbig					 * numbers */
19231744Shelbig	int	flag_orthodox = 0;	/* use wants Orthodox easter */
19331744Shelbig	int	flag_easter = 0;	/* use wants easter date */
19431744Shelbig	char	*cp;			/* character pointer */
19532215Shelbig	char    *locale;		/* locale to get country code */
19631744Shelbig
19732215Shelbig	/*
19832215Shelbig	 * Use locale to determine the country code,
19932215Shelbig	 * and use the country code to determine the default
20032215Shelbig	 * switchdate and date format from the switches table.
20132215Shelbig	 */
20232215Shelbig	if ((locale = setlocale(LC_TIME, "")) == NULL)
20332215Shelbig		warn("setlocale");
20432215Shelbig	if (locale == NULL || locale == "C")
20532215Shelbig		locale = "_US";
20632215Shelbig	q = switches + sizeof(switches) / sizeof(struct djswitch);
20732215Shelbig	for (p = switches; p != q; p++)
20832215Shelbig		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
20932215Shelbig			break;
21032215Shelbig	if (p == q) {
21132215Shelbig		nswitch = ndaysj(&dftswitch->dt);
21232215Shelbig	} else {
21332215Shelbig		nswitch = ndaysj(&p->dt);
21432215Shelbig		dftswitch = p;
21532215Shelbig	}
21631744Shelbig
21732215Shelbig
21831744Shelbig	/*
21931744Shelbig	 * Get the filename portion of argv[0] and set flag_backward if
22031744Shelbig	 * this program is called "cal".
22131744Shelbig	 */
22231744Shelbig	for (cp = argv[0]; *cp; cp++)
22331744Shelbig		;
22431744Shelbig	while (cp >= argv[0] && *cp != '/')
22531744Shelbig		cp--;
22631744Shelbig	if (strcmp("cal", ++cp) == 0)
22731744Shelbig		flag_backward = 1;
22831744Shelbig
22931744Shelbig	/* Set the switch date to United Kingdom if backwards compatible */
23031744Shelbig	if (flag_backward)
23131744Shelbig		nswitchb = ndaysj(&ukswitch);
23231744Shelbig
23331744Shelbig	while ((ch = getopt(argc, argv, "Jejops:wy")) != -1)
23431744Shelbig		switch (ch) {
23531744Shelbig		case 'J':
23631744Shelbig			if (flag_backward)
23731744Shelbig				usage();
23831744Shelbig			nswitch = ndaysj(&never);
23931744Shelbig			flag_julian_cal = 1;
24031744Shelbig			break;
24131744Shelbig		case 'e':
24231744Shelbig			if (flag_backward)
24331744Shelbig				usage();
24431744Shelbig			flag_easter = 1;
24531744Shelbig			break;
24631744Shelbig		case 'j':
24731744Shelbig			flag_julian_day = 1;
24831744Shelbig			break;
24931744Shelbig		case 'o':
25031744Shelbig			if (flag_backward)
25131744Shelbig				usage();
25231744Shelbig			flag_orthodox = 1;
25331744Shelbig			flag_easter = 1;
25431744Shelbig			break;
25531744Shelbig		case 'p':
25631744Shelbig			if (flag_backward)
25731744Shelbig				usage();
25831744Shelbig			printcc();
25931744Shelbig			return (0);
26031744Shelbig			break;
26131744Shelbig		case 's':
26231744Shelbig			if (flag_backward)
26331744Shelbig				usage();
26431744Shelbig			q = switches +
26531744Shelbig			    sizeof(switches) / sizeof(struct djswitch);
26631744Shelbig			for (p = switches;
26731744Shelbig			     p != q && strcmp(p->cc, optarg) != 0; p++)
26831744Shelbig				;
26931744Shelbig			if (p == q)
27031744Shelbig				errx(EX_USAGE,
27131744Shelbig				    "%s: invalid country code", optarg);
27231744Shelbig			nswitch = ndaysj(&(p->dt));
27331744Shelbig			break;
27431744Shelbig		case 'w':
27531744Shelbig			if (flag_backward)
27631744Shelbig				usage();
27731744Shelbig			flag_weeks = 1;
27831744Shelbig			break;
27931744Shelbig		case 'y':
28031744Shelbig			flag_hole_year = 1;
28131744Shelbig			break;
28231744Shelbig		default:
28331744Shelbig			usage();
28431744Shelbig		}
28531744Shelbig
28631744Shelbig	argc -= optind;
28731744Shelbig	argv += optind;
28831744Shelbig
28931744Shelbig	if (argc == 0) {
29031744Shelbig		time_t t;
29131744Shelbig		struct tm *tm;
29231744Shelbig
29331744Shelbig		t = time(NULL);
29431744Shelbig		tm = localtime(&t);
29531744Shelbig		y = tm->tm_year + 1900;
29631744Shelbig		m = tm->tm_mon + 1;
29731744Shelbig	}
29831744Shelbig
29931744Shelbig	switch (argc) {
30031744Shelbig	case 2:
30131744Shelbig		if (flag_easter)
30231744Shelbig			usage();
30331744Shelbig		m = atoi(*argv++);
30431744Shelbig		if (m < 1 || m > 12)
30531744Shelbig			errx(EX_USAGE, "month %d not in range 1..12", m);
30631744Shelbig		/* FALLTHROUGH */
30731744Shelbig	case 1:
30831744Shelbig		y = atoi(*argv++);
30931744Shelbig		if (y < 1 || y > 9999)
31031744Shelbig			errx(EX_USAGE, "year %d not in range 1..9999", y);
31131744Shelbig		break;
31231744Shelbig	case 0:
31331744Shelbig		break;
31431744Shelbig	default:
31531744Shelbig		usage();
31631744Shelbig	}
31731744Shelbig
31831744Shelbig	if (flag_easter)
31931744Shelbig		printeaster(y, flag_julian_cal, flag_orthodox);
32031744Shelbig	else if (argc == 1 || flag_hole_year)
32131744Shelbig		if (flag_backward)
32231744Shelbig			printyearb(y, flag_julian_day);
32331744Shelbig		else
32431744Shelbig			printyear(y, flag_julian_day);
32531744Shelbig	else
32631744Shelbig		if (flag_backward)
32731744Shelbig			printmonthb(y, m, flag_julian_day);
32831744Shelbig		else
32931744Shelbig			printmonth(y, m, flag_julian_day);
33031744Shelbig
33131744Shelbig	return (0);
33231744Shelbig}
33331744Shelbig
33432310Scharnierstatic void
33531744Shelbigusage(void)
33631744Shelbig{
33731744Shelbig
33832310Scharnier	fprintf(stderr, "%s\n%s\n%s\n",
33932525Shelbig	    "usage: cal [-jy] [[month] year]",
34032310Scharnier	    "       ncal [-Jjpwy] [-s country_code] [[month] year]",
34132310Scharnier	    "       ncal [-Jeo] [year]");
34231744Shelbig	exit(EX_USAGE);
34331744Shelbig}
34431744Shelbig
34531744Shelbig/* print the assumed switches for all countries */
34631744Shelbigvoid
34731744Shelbigprintcc(void)
34831744Shelbig{
34931744Shelbig	struct djswitch *p;
35031744Shelbig	int n;	/* number of lines to print */
35131744Shelbig	int m;	/* offset from left to right table entry on the same line */
35231744Shelbig
35332215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d"
35432215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
35532215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
35631744Shelbig
35731744Shelbig	n = sizeof(switches) / sizeof(struct djswitch);
35831744Shelbig	m = (n + 1) / 2;
35931744Shelbig	n /= 2;
36032215Shelbig	for (p = switches; p != switches + n; p++)
36132215Shelbig		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
36231744Shelbig	if (m != n)
36332215Shelbig		printf(FSTR"\n", FSTRARG(p));
36431744Shelbig}
36531744Shelbig
36631744Shelbig/* print the date of easter sunday */
36731744Shelbigvoid
36831744Shelbigprinteaster(int y, int julian, int orthodox)
36931744Shelbig{
37032215Shelbig	date    dt;
37132215Shelbig	struct tm tm;
37232215Shelbig	char    buf[80];
37331744Shelbig
37431744Shelbig	/* force orthodox easter for years before 1583 */
37531744Shelbig	if (y < 1583)
37631744Shelbig		orthodox = 1;
37731744Shelbig
37831744Shelbig	if (orthodox)
37931744Shelbig		if (julian)
38031744Shelbig			easteroj(y, &dt);
38131744Shelbig		else
38231744Shelbig			easterog(y, &dt);
38331744Shelbig	else
38431744Shelbig		easterg(y, &dt);
38532215Shelbig
38632215Shelbig	memset(&tm, 0, sizeof(tm));
38732215Shelbig	tm.tm_year = dt.y - 1900;
38832215Shelbig	tm.tm_mon  = dt.m - 1;
38932215Shelbig	tm.tm_mday = dt.d;
39053963Sache	strftime(buf, sizeof(buf), "%EF %Y",  &tm);
39132215Shelbig	printf("%s\n", buf);
39231744Shelbig}
39331744Shelbig
39431744Shelbigvoid
39531744Shelbigprintmonth(int y, int m, int jd_flag)
39631744Shelbig{
39731744Shelbig	struct monthlines month;
39831744Shelbig	struct weekdays wds;
39931744Shelbig	int i;
40031744Shelbig
40131744Shelbig	mkmonth(y, m - 1, jd_flag, &month);
40231744Shelbig	mkweekdays(&wds);
40331744Shelbig	printf("    %s %d\n", month.name, y);
40431744Shelbig	for (i = 0; i != 7; i++)
40531744Shelbig		printf("%.2s%s\n", wds.names[i], month.lines[i]);
40631744Shelbig	if (flag_weeks)
40731744Shelbig		printf("  %s\n", month.weeks);
40831744Shelbig}
40931744Shelbig
41031744Shelbigvoid
41131744Shelbigprintmonthb(int y, int m, int jd_flag)
41231744Shelbig{
41331744Shelbig	struct monthlines month;
41431744Shelbig	struct weekdays wds;
41531744Shelbig	char s[MAX_WIDTH], t[MAX_WIDTH];
41631744Shelbig	int i;
41731744Shelbig	int mw;
41831744Shelbig
41931744Shelbig	mkmonthb(y, m - 1, jd_flag, &month);
42031744Shelbig	mkweekdays(&wds);
42131744Shelbig
42231744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
42331744Shelbig
42431744Shelbig	sprintf(s, "%s %d", month.name, y);
42531744Shelbig	printf("%s\n", center(t, s, mw));
42631744Shelbig
42731744Shelbig	if (jd_flag)
42831744Shelbig		printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
42931744Shelbig			wds.names[1], wds.names[2], wds.names[3],
43031744Shelbig			wds.names[4], wds.names[5]);
43131744Shelbig	else
43231744Shelbig		printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
43331744Shelbig			wds.names[1], wds.names[2], wds.names[3],
43431744Shelbig			wds.names[4], wds.names[5]);
43531744Shelbig
43631744Shelbig	for (i = 0; i != 6; i++)
43731744Shelbig		printf("%s\n", month.lines[i]+1);
43831744Shelbig}
43931744Shelbig
44031744Shelbigvoid
44131744Shelbigprintyear(int y, int jd_flag)
44231744Shelbig{
44331744Shelbig	struct monthlines year[12];
44431744Shelbig	struct weekdays wds;
44531744Shelbig	char    s[80], t[80];
44631744Shelbig	int     i, j;
44731744Shelbig	int     mpl;
44831744Shelbig	int     mw;
44931744Shelbig
45031744Shelbig	for (i = 0; i != 12; i++)
45131744Shelbig		mkmonth(y, i, jd_flag, year + i);
45231744Shelbig	mkweekdays(&wds);
45331744Shelbig	mpl = jd_flag ? 3 : 4;
45431744Shelbig	mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
45531744Shelbig
45631744Shelbig	sprintf(s, "%d", y);
45731744Shelbig	printf("%s\n", center(t, s, mpl * mw));
45831744Shelbig
45931744Shelbig	for (j = 0; j != 12; j += mpl) {
46031744Shelbig		printf("    %-*s%-*s",
46131744Shelbig		    mw, year[j].name,
46231744Shelbig		    mw, year[j + 1].name);
46331744Shelbig		if (mpl == 3)
46431744Shelbig			printf("%s\n", year[j + 2].name);
46531744Shelbig		else
46631744Shelbig			printf("%-*s%s\n",
46731744Shelbig		    	    mw, year[j + 2].name,
46831744Shelbig		    	    year[j + 3].name);
46931744Shelbig		for (i = 0; i != 7; i++) {
47031744Shelbig			printf("%.2s%-*s%-*s",
47131744Shelbig			    wds.names[i],
47231744Shelbig			    mw, year[j].lines[i],
47331744Shelbig			    mw, year[j + 1].lines[i]);
47431744Shelbig			if (mpl == 3)
47531744Shelbig				printf("%s\n", year[j + 2].lines[i]);
47631744Shelbig			else
47731744Shelbig				printf("%-*s%s\n",
47831744Shelbig			    	    mw, year[j + 2].lines[i],
47931744Shelbig			    	    year[j + 3].lines[i]);
48031744Shelbig		}
48148566Sbillf		if (flag_weeks) {
48231744Shelbig			if (mpl == 3)
48331744Shelbig				printf("  %-*s%-*s%-s\n",
48431744Shelbig				    mw, year[j].weeks,
48531744Shelbig				    mw, year[j + 1].weeks,
48631744Shelbig				    year[j + 2].weeks);
48731744Shelbig			else
48831744Shelbig				printf("  %-*s%-*s%-*s%-s\n",
48931744Shelbig				    mw, year[j].weeks,
49031744Shelbig				    mw, year[j + 1].weeks,
49131744Shelbig				    mw, year[j + 2].weeks,
49231744Shelbig				    year[j + 3].weeks);
49348566Sbillf		}
49431744Shelbig	}
49531744Shelbig}
49631744Shelbig
49731744Shelbigvoid
49831744Shelbigprintyearb(int y, int jd_flag)
49931744Shelbig{
50031744Shelbig	struct monthlines year[12];
50131744Shelbig	struct weekdays wds;
50231744Shelbig	char	s[80], t[80];
50331744Shelbig	int     i, j;
50431744Shelbig	int     mpl;
50531744Shelbig	int     mw;
50631744Shelbig
50731744Shelbig	for (i = 0; i != 12; i++)
50831744Shelbig		mkmonthb(y, i, jd_flag, year + i);
50931744Shelbig	mkweekdays(&wds);
51031744Shelbig	mpl = jd_flag ? 2 : 3;
51131744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
51231744Shelbig
51331744Shelbig	sprintf(s, "%d", y);
51431744Shelbig	printf("%s\n\n", center(t, s, mw * mpl + mpl));
51531744Shelbig
51631744Shelbig	for (j = 0; j != 12; j += mpl) {
51731744Shelbig		printf("%-*s  ", mw, center(s, year[j].name, mw));
51831744Shelbig		if (mpl == 2)
51931744Shelbig			printf("%s\n", center(s, year[j + 1].name, mw));
52031744Shelbig		else
52131744Shelbig			printf("%-*s  %s\n", mw,
52231744Shelbig			    center(s, year[j + 1].name, mw),
52331744Shelbig			    center(t, year[j + 2].name, mw));
52431744Shelbig
52531744Shelbig		if (mpl == 2)
52631744Shelbig			printf(" %s %s %s %s %s %s %s "
52731744Shelbig			       " %s %s %s %s %s %s %.2s\n",
52831744Shelbig				wds.names[6], wds.names[0], wds.names[1],
52931744Shelbig				wds.names[2], wds.names[3], wds.names[4],
53031744Shelbig				wds.names[5],
53131744Shelbig				wds.names[6], wds.names[0], wds.names[1],
53231744Shelbig				wds.names[2], wds.names[3], wds.names[4],
53331744Shelbig				wds.names[5]);
53431744Shelbig		else
53531744Shelbig			printf("%s%s%s%s%s%s%s "
53631744Shelbig				"%s%s%s%s%s%s%s "
53731744Shelbig				"%s%s%s%s%s%s%.2s\n",
53831744Shelbig				wds.names[6], wds.names[0], wds.names[1],
53931744Shelbig				wds.names[2], wds.names[3], wds.names[4],
54031744Shelbig				wds.names[5],
54131744Shelbig				wds.names[6], wds.names[0], wds.names[1],
54231744Shelbig				wds.names[2], wds.names[3], wds.names[4],
54331744Shelbig				wds.names[5],
54431744Shelbig				wds.names[6], wds.names[0], wds.names[1],
54531744Shelbig				wds.names[2], wds.names[3], wds.names[4],
54631744Shelbig				wds.names[5]);
54731744Shelbig		for (i = 0; i != 6; i++) {
54831744Shelbig			if (mpl == 2)
54931744Shelbig				printf("%-*s  %s\n",
55031744Shelbig			    mw, year[j].lines[i]+1,
55131744Shelbig			    year[j + 1].lines[i]+1);
55231744Shelbig			else
55331744Shelbig				printf("%-*s  %-*s  %s\n",
55431744Shelbig			    mw, year[j].lines[i]+1,
55531744Shelbig			    mw, year[j + 1].lines[i]+1,
55631744Shelbig			    year[j + 2].lines[i]+1);
55731744Shelbig
55831744Shelbig		}
55931744Shelbig	}
56031744Shelbig}
56131744Shelbig
56231744Shelbigvoid
56331744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
56431744Shelbig{
56531744Shelbig
56631744Shelbig	struct tm tm;		/* for strftime printing local names of
56731744Shelbig				 * months */
56831744Shelbig	date    dt;		/* handy date */
56931744Shelbig	int     dw;		/* width of numbers */
57031744Shelbig	int     first;		/* first day of month */
57131744Shelbig	int     firstm;		/* first day of first week of month */
57231744Shelbig	int     i, j, k;	/* just indices */
57331744Shelbig	int     last;		/* the first day of next month */
57431744Shelbig	int     jan1 = 0;	/* the first day of this year */
57531744Shelbig	char   *ds;		/* pointer to day strings (daystr or
57631744Shelbig				 * jdaystr) */
57731744Shelbig
57831744Shelbig	/* Set name of month. */
57931744Shelbig	memset(&tm, 0, sizeof(tm));
58031744Shelbig	tm.tm_mon = m;
58151201Sache	strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
58231744Shelbig
58331744Shelbig	/*
58431744Shelbig	 * Set first and last to the day number of the first day of this
58531744Shelbig	 * month and the first day of next month respectively. Set jan1 to
58632282Shelbig	 * the day number of the first day of this year.
58731744Shelbig	 */
58832282Shelbig	first = firstday(y, m + 1);
58932282Shelbig	if (m == 11)
59032282Shelbig		last = firstday(y + 1, 1);
59132282Shelbig	else
59232282Shelbig		last = firstday(y, m + 2);
59331744Shelbig
59432282Shelbig	if (jd_flag)
59532525Shelbig		jan1 = firstday(y, 1);
59631744Shelbig
59731744Shelbig	/*
59831744Shelbig	 * Set firstm to the day number of monday of the first week of
59931744Shelbig	 * this month. (This might be in the last month)
60031744Shelbig	 */
60131744Shelbig	firstm = first - weekday(first);
60231744Shelbig
60331744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
60431744Shelbig	if (jd_flag) {
60531744Shelbig		ds = jdaystr;
60631744Shelbig		dw = 4;
60731744Shelbig	} else {
60831744Shelbig		ds = daystr;
60931744Shelbig		dw = 3;
61031744Shelbig	}
61131744Shelbig
61231744Shelbig	/*
61331744Shelbig	 * Fill the lines with day of month or day of year (julian day)
61431744Shelbig	 * line index: i, each line is one weekday. column index: j, each
61531744Shelbig	 * column is one day number. print column index: k.
61631744Shelbig	 */
61731744Shelbig	for (i = 0; i != 7; i++) {
61831744Shelbig		for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
61931744Shelbig			if (j >= first) {
62031744Shelbig				if (jd_flag)
62131744Shelbig					dt.d = j - jan1 + 1;
62231744Shelbig				else
62331744Shelbig					sdate(j, &dt);
62431744Shelbig				memcpy(mlines->lines[i] + k,
62531744Shelbig				       ds + dt.d * dw, dw);
62631744Shelbig			} else
62731744Shelbig				memcpy(mlines->lines[i] + k, "    ", dw);
62831744Shelbig		mlines->lines[i][k] = '\0';
62931744Shelbig
63031744Shelbig	}
63131744Shelbig
63231744Shelbig	/* fill the weeknumbers */
63331744Shelbig	if (flag_weeks) {
63431744Shelbig		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
63531744Shelbig			if (j <= nswitch)
63631744Shelbig				memset(mlines->weeks + k, ' ', dw);
63731744Shelbig			else
63831744Shelbig				memcpy(mlines->weeks + k,
63931744Shelbig				    ds + week(j, &i)*dw, dw);
64031744Shelbig		mlines->weeks[k] = '\0';
64131744Shelbig	}
64231744Shelbig}
64331744Shelbig
64431744Shelbigvoid
64531744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
64631744Shelbig{
64731744Shelbig
64831744Shelbig	struct tm tm;		/* for strftime printing local names of
64931744Shelbig				 * months */
65031744Shelbig	date    dt;		/* handy date */
65131744Shelbig	int     dw;		/* width of numbers */
65231744Shelbig	int     first;		/* first day of month */
65331744Shelbig	int     firsts;		/* sunday of first week of month */
65431744Shelbig	int     i, j, k;	/* just indices */
65531744Shelbig	int     jan1 = 0;	/* the first day of this year */
65631744Shelbig	int     last;		/* the first day of next month */
65731744Shelbig	char   *ds;		/* pointer to day strings (daystr or
65831744Shelbig				 * jdaystr) */
65931744Shelbig
66031744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
66131744Shelbig	if (jd_flag) {
66231744Shelbig		ds = jdaystr;
66331744Shelbig		dw = 4;
66431744Shelbig	} else {
66531744Shelbig		ds = daystr;
66631744Shelbig		dw = 3;
66731744Shelbig	}
66831744Shelbig
66931744Shelbig	/* Set name of month centered */
67031744Shelbig	memset(&tm, 0, sizeof(tm));
67131744Shelbig	tm.tm_mon = m;
67251201Sache	strftime(mlines->name, sizeof(mlines->name), "%OB", &tm);
67331744Shelbig
67431744Shelbig	/*
67531744Shelbig	 * Set first and last to the day number of the first day of this
67631744Shelbig	 * month and the first day of next month respectively. Set jan1 to
67731744Shelbig	 * the day number of Jan 1st of this year.
67831744Shelbig	 */
67931744Shelbig	dt.y = y;
68031744Shelbig	dt.m = m + 1;
68131744Shelbig	dt.d = 1;
68231744Shelbig	first = sndaysb(&dt);
68331744Shelbig	if (m == 11) {
68431744Shelbig		dt.y = y + 1;
68531744Shelbig		dt.m = 1;
68631744Shelbig		dt.d = 1;
68731744Shelbig	} else {
68831744Shelbig		dt.y = y;
68931744Shelbig		dt.m = m + 2;
69031744Shelbig		dt.d = 1;
69131744Shelbig	}
69231744Shelbig	last = sndaysb(&dt);
69331744Shelbig
69431744Shelbig	if (jd_flag) {
69531744Shelbig		dt.y = y;
69631744Shelbig		dt.m = 1;
69731744Shelbig		dt.d = 1;
69831744Shelbig		jan1 = sndaysb(&dt);
69931744Shelbig	}
70031744Shelbig
70131744Shelbig	/*
70231744Shelbig	 * Set firsts to the day number of sunday of the first week of
70331744Shelbig	 * this month. (This might be in the last month)
70431744Shelbig	 */
70531744Shelbig	firsts = first - (weekday(first)+1) % 7;
70631744Shelbig
70731744Shelbig	/*
70831744Shelbig	 * Fill the lines with day of month or day of year (Julian day)
70931744Shelbig	 * line index: i, each line is one week. column index: j, each
71031744Shelbig	 * column is one day number. print column index: k.
71131744Shelbig	 */
71231744Shelbig	for (i = 0; i != 6; i++) {
71331744Shelbig		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
71431744Shelbig		     j++, k += dw)
71531744Shelbig			if (j >= first) {
71631744Shelbig				if (jd_flag)
71731744Shelbig					dt.d = j - jan1 + 1;
71831744Shelbig				else
71931744Shelbig					sdateb(j, &dt);
72031744Shelbig				memcpy(mlines->lines[i] + k,
72131744Shelbig				       ds + dt.d * dw, dw);
72231744Shelbig			} else
72331744Shelbig				memcpy(mlines->lines[i] + k, "    ", dw);
72431744Shelbig		if (k == 0)
72531744Shelbig			mlines->lines[i][1] = '\0';
72631744Shelbig		else
72731744Shelbig			mlines->lines[i][k] = '\0';
72831744Shelbig	}
72931744Shelbig}
73031744Shelbig
73131744Shelbig/* Put the local names of weekdays into the wds */
73232139Shelbigvoid
73332139Shelbigmkweekdays(struct weekdays *wds)
73431744Shelbig{
73531744Shelbig	int i;
73631744Shelbig	struct tm tm;
73731744Shelbig
73831744Shelbig	memset(&tm, 0, sizeof(tm));
73931744Shelbig
74031744Shelbig	for (i = 0; i != 7; i++) {
74131744Shelbig		tm.tm_wday = (i+1) % 7;
74231744Shelbig		strftime(wds->names[i], 4, "%a", &tm);
74332139Shelbig		wds->names[i][2] = ' ';
74431744Shelbig	}
74531744Shelbig}
74631744Shelbig
74731744Shelbig/*
74845064Shelbig * Compute the day number of the first
74945064Shelbig * existing date after the first day in month.
75045064Shelbig * (the first day in month and even the month might not exist!)
75132282Shelbig */
75232282Shelbigint
75332282Shelbigfirstday(int y, int m)
75432282Shelbig{
75532282Shelbig	date dt;
75632282Shelbig	int nd;
75732282Shelbig
75832282Shelbig	dt.y = y;
75932282Shelbig	dt.m = m;
76032282Shelbig	dt.d = 1;
76145064Shelbig	nd = sndays(&dt);
76245064Shelbig	for (;;) {
76345064Shelbig		sdate(nd, &dt);
76445064Shelbig		if ((dt.m >= m && dt.y == y) || dt.y > y)
76545064Shelbig			return (nd);
76645064Shelbig		else
76745064Shelbig			nd++;
76845064Shelbig	}
76945064Shelbig	/* NEVER REACHED */
77032282Shelbig}
77132282Shelbig
77232282Shelbig/*
77331744Shelbig * Compute the number of days from date, obey the local switch from
77431744Shelbig * Julian to Gregorian if specified by the user.
77531744Shelbig */
77631744Shelbigint
77731744Shelbigsndays(struct date *d)
77831744Shelbig{
77931744Shelbig
78031744Shelbig	if (nswitch != 0)
78131744Shelbig		if (nswitch < ndaysj(d))
78231744Shelbig			return (ndaysg(d));
78331744Shelbig		else
78431744Shelbig			return (ndaysj(d));
78531744Shelbig	else
78631744Shelbig		return ndaysg(d);
78731744Shelbig}
78831744Shelbig
78931744Shelbig/*
79031744Shelbig * Compute the number of days from date, obey the switch from
79131744Shelbig * Julian to Gregorian as used by UK and her colonies.
79231744Shelbig */
79331744Shelbigint
79431744Shelbigsndaysb(struct date *d)
79531744Shelbig{
79631744Shelbig
79731744Shelbig	if (nswitchb < ndaysj(d))
79831744Shelbig		return (ndaysg(d));
79931744Shelbig	else
80031744Shelbig		return (ndaysj(d));
80131744Shelbig}
80231744Shelbig
80331744Shelbig/* Inverse of sndays */
80431744Shelbigstruct date *
80531744Shelbigsdate(int nd, struct date *d)
80631744Shelbig{
80731744Shelbig
80831744Shelbig	if (nswitch < nd)
80931744Shelbig		return (gdate(nd, d));
81031744Shelbig	else
81131744Shelbig		return (jdate(nd, d));
81231744Shelbig}
81331744Shelbig
81431744Shelbig/* Inverse of sndaysb */
81531744Shelbigstruct date *
81631744Shelbigsdateb(int nd, struct date *d)
81731744Shelbig{
81831744Shelbig
81931744Shelbig	if (nswitchb < nd)
82031744Shelbig		return (gdate(nd, d));
82131744Shelbig	else
82231744Shelbig		return (jdate(nd, d));
82331744Shelbig}
82431744Shelbig
82531744Shelbig/* Center string t in string s of length w by putting enough leading blanks */
82631744Shelbigchar *
82731744Shelbigcenter(char *s, char *t, int w)
82831744Shelbig{
82931744Shelbig	char blanks[80];
83031744Shelbig
83131744Shelbig	memset(blanks, ' ', sizeof(blanks));
83231744Shelbig	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
83331744Shelbig	return (s);
83431744Shelbig}
835