xref: /freebsd/usr.bin/ncal/ncal.c (revision dc60165b73e4c4d829a2cb9fed5cce585e93d9a9)
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
28 static const char rcsid[] =
29   "$FreeBSD$";
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>
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 
55 typedef struct date date;
56 
57 struct monthlines {
58 	wchar_t name[MAX_WIDTH + 1];
59 	char lines[7][MAX_WIDTH + 1];
60 	char weeks[MAX_WIDTH + 1];
61 };
62 
63 struct weekdays {
64 	wchar_t names[7][4];
65 };
66 
67 /* The switches from Julian to Gregorian in some countries */
68 static 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[] = {
73 	{"AL", "Albania",       {1912, 11, 30}},
74 	{"AT", "Austria",       {1583, 10,  5}},
75 	{"AU", "Australia",     {1752,  9,  2}},
76 	{"BE", "Belgium",       {1582, 12, 14}},
77 	{"BG", "Bulgaria",      {1916,  3, 18}},
78 	{"CA", "Canada",        {1752,  9,  2}},
79 	{"CH", "Switzerland",   {1655,  2, 28}},
80 	{"CN", "China",         {1911, 12, 18}},
81 	{"CZ", "Czech Republic",{1584,  1,  6}},
82 	{"DE", "Germany",       {1700,  2, 18}},
83 	{"DK", "Denmark",       {1700,  2, 18}},
84 	{"ES", "Spain",         {1582, 10,  4}},
85 	{"FI", "Finland",       {1753,  2, 17}},
86 	{"FR", "France",        {1582, 12,  9}},
87 	{"GB", "United Kingdom",{1752,  9,  2}},
88 	{"GR", "Greece",        {1924,  3,  9}},
89 	{"HU", "Hungary",       {1587, 10, 21}},
90 	{"IS", "Iceland",       {1700, 11, 16}},
91 	{"IT", "Italy",         {1582, 10,  4}},
92 	{"JP", "Japan",         {1918, 12, 18}},
93 	{"LI", "Lithuania",     {1918,  2,  1}},
94 	{"LN", "Latin",         {9999, 05, 31}},
95 	{"LU", "Luxembourg",    {1582, 12, 14}},
96 	{"LV", "Latvia",        {1918,  2,  1}},
97 	{"NL", "Netherlands",   {1582, 12, 14}},
98 	{"NO", "Norway",        {1700,  2, 18}},
99 	{"PL", "Poland",        {1582, 10,  4}},
100 	{"PT", "Portugal",      {1582, 10,  4}},
101 	{"RO", "Romania",       {1919,  3, 31}},
102 	{"RU", "Russia",        {1918,  1, 31}},
103 	{"SI", "Slovenia",      {1919,  3,  4}},
104 	{"SW", "Sweden",        {1753,  2, 17}},
105 	{"TR", "Turkey",        {1926, 12, 18}},
106 	{"US", "United States", {1752,  9,  2}},
107 	{"YU", "Yugoslavia",    {1919,  3,  4}}
108 };
109 
110 struct djswitch *dftswitch =
111     switches + sizeof(switches) / sizeof(struct djswitch) - 2;
112     /* default switch (should be "US") */
113 
114 /* Table used to print day of month and week numbers */
115 char daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
116 		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
117 		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
118 		" 48 49 50 51 52 53";
119 
120 /* Table used to print day of year and week numbers */
121 char jdaystr[] = "       1   2   3   4   5   6   7   8   9"
122 		 "  10  11  12  13  14  15  16  17  18  19"
123 		 "  20  21  22  23  24  25  26  27  28  29"
124 		 "  30  31  32  33  34  35  36  37  38  39"
125 		 "  40  41  42  43  44  45  46  47  48  49"
126 		 "  50  51  52  53  54  55  56  57  58  59"
127 		 "  60  61  62  63  64  65  66  67  68  69"
128 		 "  70  71  72  73  74  75  76  77  78  79"
129 		 "  80  81  82  83  84  85  86  87  88  89"
130 		 "  90  91  92  93  94  95  96  97  98  99"
131 		 " 100 101 102 103 104 105 106 107 108 109"
132 		 " 110 111 112 113 114 115 116 117 118 119"
133 		 " 120 121 122 123 124 125 126 127 128 129"
134 		 " 130 131 132 133 134 135 136 137 138 139"
135 		 " 140 141 142 143 144 145 146 147 148 149"
136 		 " 150 151 152 153 154 155 156 157 158 159"
137 		 " 160 161 162 163 164 165 166 167 168 169"
138 		 " 170 171 172 173 174 175 176 177 178 179"
139 		 " 180 181 182 183 184 185 186 187 188 189"
140 		 " 190 191 192 193 194 195 196 197 198 199"
141 		 " 200 201 202 203 204 205 206 207 208 209"
142 		 " 210 211 212 213 214 215 216 217 218 219"
143 		 " 220 221 222 223 224 225 226 227 228 229"
144 		 " 230 231 232 233 234 235 236 237 238 239"
145 		 " 240 241 242 243 244 245 246 247 248 249"
146 		 " 250 251 252 253 254 255 256 257 258 259"
147 		 " 260 261 262 263 264 265 266 267 268 269"
148 		 " 270 271 272 273 274 275 276 277 278 279"
149 		 " 280 281 282 283 284 285 286 287 288 289"
150 		 " 290 291 292 293 294 295 296 297 298 299"
151 		 " 300 301 302 303 304 305 306 307 308 309"
152 		 " 310 311 312 313 314 315 316 317 318 319"
153 		 " 320 321 322 323 324 325 326 327 328 329"
154 		 " 330 331 332 333 334 335 336 337 338 339"
155 		 " 340 341 342 343 344 345 346 347 348 349"
156 		 " 350 351 352 353 354 355 356 357 358 359"
157 		 " 360 361 362 363 364 365 366";
158 
159 int     flag_weeks;		/* user wants number of week */
160 int     nswitch;		/* user defined switch date */
161 int	nswitchb;		/* switch date for backward compatibility */
162 
163 char   *center(char *s, char *t, int w);
164 wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
165 void	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
166 void    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
167 void    mkweekdays(struct weekdays * wds);
168 int     parsemonth(const char *s, int *m, int *y);
169 void    printcc(void);
170 void    printeaster(int year, int julian, int orthodox);
171 void    printmonth(int year, int month, int jd_flag);
172 void    printmonthb(int year, int month, int jd_flag);
173 void    printyear(int year, int jd_flag);
174 void    printyearb(int year, int jd_flag);
175 int	firstday(int y, int m);
176 date   *sdate(int ndays, struct date * d);
177 date   *sdateb(int ndays, struct date * d);
178 int     sndays(struct date * d);
179 int     sndaysb(struct date * d);
180 static void usage(void);
181 int     weekdayb(int nd);
182 
183 int
184 main(int argc, char *argv[])
185 {
186 	struct  djswitch *p, *q;	/* to search user defined switch date */
187 	date	never = {10000, 1, 1};	/* outside valid range of dates */
188 	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
189 	int     ch;			/* holds the option character */
190 	int     m = 0;			/* month */
191 	int	y = 0;			/* year */
192 	int     flag_backward = 0;	/* user called cal--backward compat. */
193 	int     flag_hole_year = 0;	/* user wants the whole year */
194 	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
195 	int     flag_julian_day = 0;	/* user wants the Julian day
196 					 * numbers */
197 	int	flag_orthodox = 0;	/* use wants Orthodox easter */
198 	int	flag_easter = 0;	/* use wants easter date */
199 	char	*cp;			/* character pointer */
200 	char	*flag_month = NULL;	/* requested month as string */
201 	const char    *locale;		/* locale to get country code */
202 
203 	/*
204 	 * Use locale to determine the country code,
205 	 * and use the country code to determine the default
206 	 * switchdate and date format from the switches table.
207 	 */
208 	if (setlocale(LC_ALL, "") == NULL)
209 		warn("setlocale");
210 	locale = setlocale(LC_TIME, NULL);
211 	if (locale == NULL ||
212 	    strcmp(locale, "C") == 0 ||
213 	    strcmp(locale, "POSIX") == 0 ||
214 	    strcmp(locale, "ASCII") == 0 ||
215 	    strcmp(locale, "US-ASCII") == 0)
216 		locale = "_US";
217 	q = switches + sizeof(switches) / sizeof(struct djswitch);
218 	for (p = switches; p != q; p++)
219 		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
220 			break;
221 	if (p == q) {
222 		nswitch = ndaysj(&dftswitch->dt);
223 	} else {
224 		nswitch = ndaysj(&p->dt);
225 		dftswitch = p;
226 	}
227 
228 
229 	/*
230 	 * Get the filename portion of argv[0] and set flag_backward if
231 	 * this program is called "cal".
232 	 */
233 	cp = strrchr(argv[0], '/');
234 	cp = (cp == NULL) ? argv[0] : cp + 1;
235 	if (strcmp("cal", cp) == 0)
236 		flag_backward = 1;
237 
238 	/* Set the switch date to United Kingdom if backwards compatible */
239 	if (flag_backward)
240 		nswitchb = ndaysj(&ukswitch);
241 
242 	while ((ch = getopt(argc, argv, "Jejm:ops:wy")) != -1)
243 		switch (ch) {
244 		case 'J':
245 			if (flag_backward)
246 				usage();
247 			nswitch = ndaysj(&never);
248 			flag_julian_cal = 1;
249 			break;
250 		case 'e':
251 			if (flag_backward)
252 				usage();
253 			flag_easter = 1;
254 			break;
255 		case 'j':
256 			flag_julian_day = 1;
257 			break;
258 		case 'm':
259 			flag_month = optarg;
260 			break;
261 		case 'o':
262 			if (flag_backward)
263 				usage();
264 			flag_orthodox = 1;
265 			flag_easter = 1;
266 			break;
267 		case 'p':
268 			if (flag_backward)
269 				usage();
270 			printcc();
271 			return (0);
272 			break;
273 		case 's':
274 			if (flag_backward)
275 				usage();
276 			q = switches +
277 			    sizeof(switches) / sizeof(struct djswitch);
278 			for (p = switches;
279 			     p != q && strcmp(p->cc, optarg) != 0; p++)
280 				;
281 			if (p == q)
282 				errx(EX_USAGE,
283 				    "%s: invalid country code", optarg);
284 			nswitch = ndaysj(&(p->dt));
285 			break;
286 		case 'w':
287 			if (flag_backward)
288 				usage();
289 			flag_weeks = 1;
290 			break;
291 		case 'y':
292 			flag_hole_year = 1;
293 			break;
294 		default:
295 			usage();
296 		}
297 
298 	argc -= optind;
299 	argv += optind;
300 
301 	switch (argc) {
302 	case 2:
303 		if (flag_easter)
304 			usage();
305 		flag_month = *argv++;
306 		/* FALLTHROUGH */
307 	case 1:
308 		y = atoi(*argv++);
309 		if (y < 1 || y > 9999)
310 			errx(EX_USAGE, "year %d not in range 1..9999", y);
311 		break;
312 	case 0:
313 		{
314 			time_t t;
315 			struct tm *tm;
316 
317 			t = time(NULL);
318 			tm = localtime(&t);
319 			y = tm->tm_year + 1900;
320 			m = tm->tm_mon + 1;
321 		}
322 		break;
323 	default:
324 		usage();
325 	}
326 
327 	if (flag_month != NULL) {
328 		if (parsemonth(flag_month, &m, &y)) {
329 			errx(EX_USAGE,
330 			    "%s is neither a month number (1..12) nor a name",
331 			    flag_month);
332 		}
333 	}
334 
335 	if (flag_easter)
336 		printeaster(y, flag_julian_cal, flag_orthodox);
337 	else if (argc == 1 || flag_hole_year)
338 		if (flag_backward)
339 			printyearb(y, flag_julian_day);
340 		else
341 			printyear(y, flag_julian_day);
342 	else
343 		if (flag_backward)
344 			printmonthb(y, m, flag_julian_day);
345 		else
346 			printmonth(y, m, flag_julian_day);
347 
348 	return (0);
349 }
350 
351 static void
352 usage(void)
353 {
354 
355 	fputs(
356 	    "usage: cal [-jy] [[month] year]\n"
357 	    "       cal [-j] [-m month] [year]\n"
358 	    "       ncal [-Jjpwy] [-s country_code] [[month] year]\n"
359 	    "       ncal [-Jeo] [year]\n", stderr);
360 	exit(EX_USAGE);
361 }
362 
363 /* print the assumed switches for all countries */
364 void
365 printcc(void)
366 {
367 	struct djswitch *p;
368 	int n;	/* number of lines to print */
369 	int m;	/* offset from left to right table entry on the same line */
370 
371 #define FSTR "%c%s %-15s%4d-%02d-%02d"
372 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
373 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
374 
375 	n = sizeof(switches) / sizeof(struct djswitch);
376 	m = (n + 1) / 2;
377 	n /= 2;
378 	for (p = switches; p != switches + n; p++)
379 		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
380 	if (m != n)
381 		printf(FSTR"\n", FSTRARG(p));
382 }
383 
384 /* print the date of easter sunday */
385 void
386 printeaster(int y, int julian, int orthodox)
387 {
388 	date    dt;
389 	struct tm tm;
390 	char    buf[80];
391 	static int d_first = -1;
392 
393 	if (d_first < 0)
394 		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
395 	/* force orthodox easter for years before 1583 */
396 	if (y < 1583)
397 		orthodox = 1;
398 
399 	if (orthodox)
400 		if (julian)
401 			easteroj(y, &dt);
402 		else
403 			easterog(y, &dt);
404 	else
405 		easterg(y, &dt);
406 
407 	memset(&tm, 0, sizeof(tm));
408 	tm.tm_year = dt.y - 1900;
409 	tm.tm_mon  = dt.m - 1;
410 	tm.tm_mday = dt.d;
411 	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
412 	printf("%s\n", buf);
413 }
414 
415 void
416 printmonth(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);
424 	printf("    %ls %d\n", month.name, y);
425 	for (i = 0; i != 7; i++)
426 		printf("%.2ls%s\n", wds.names[i], month.lines[i]);
427 	if (flag_weeks)
428 		printf("  %s\n", month.weeks);
429 }
430 
431 void
432 printmonthb(int y, int m, int jd_flag)
433 {
434 	struct monthlines month;
435 	struct weekdays wds;
436 	wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
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 
445 	swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
446 	wprintf(L"%ls\n", wcenter(t, s, mw));
447 
448 	if (jd_flag)
449 		wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
450 			wds.names[6], wds.names[0],
451 			wds.names[1], wds.names[2], wds.names[3],
452 			wds.names[4], wds.names[5]);
453 	else
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],
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 
462 void
463 printyear(int y, int jd_flag)
464 {
465 	struct monthlines year[12];
466 	struct weekdays wds;
467 	char    s[80], t[80];
468 	int     i, j;
469 	int     mpl;
470 	int     mw;
471 
472 	for (i = 0; i != 12; i++)
473 		mkmonth(y, i, jd_flag, year + i);
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) {
482 		printf("    %-*ls%-*ls",
483 		    mw, year[j].name,
484 		    mw, year[j + 1].name);
485 		if (mpl == 3)
486 			printf("%ls\n", year[j + 2].name);
487 		else
488 			printf("%-*ls%ls\n",
489 		    	    mw, year[j + 2].name,
490 		    	    year[j + 3].name);
491 		for (i = 0; i != 7; i++) {
492 			printf("%.2ls%-*s%-*s",
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],
501 			    	    year[j + 3].lines[i]);
502 		}
503 		if (flag_weeks) {
504 			if (mpl == 3)
505 				printf("  %-*s%-*s%-s\n",
506 				    mw, year[j].weeks,
507 				    mw, year[j + 1].weeks,
508 				    year[j + 2].weeks);
509 			else
510 				printf("  %-*s%-*s%-*s%-s\n",
511 				    mw, year[j].weeks,
512 				    mw, year[j + 1].weeks,
513 				    mw, year[j + 2].weeks,
514 				    year[j + 3].weeks);
515 		}
516 	}
517 }
518 
519 void
520 printyearb(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];
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) {
540 		printf("%-*ls  ", mw, wcenter(ws, year[j].name, mw));
541 		if (mpl == 2)
542 			printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
543 		else
544 			printf("%-*ls  %ls\n", mw,
545 			    wcenter(ws, year[j + 1].name, mw),
546 			    wcenter(wt, year[j + 2].name, mw));
547 
548 		if (mpl == 2)
549 			wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
550 				" %ls %ls %ls %ls %ls %ls %.2ls\n",
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
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",
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],
569 				wds.names[5]);
570 		for (i = 0; i != 6; i++) {
571 			if (mpl == 2)
572 				printf("%-*s  %s\n",
573 			    mw, year[j].lines[i]+1,
574 			    year[j + 1].lines[i]+1);
575 			else
576 				printf("%-*s  %-*s  %s\n",
577 			    mw, year[j].lines[i]+1,
578 			    mw, year[j + 1].lines[i]+1,
579 			    year[j + 2].lines[i]+1);
580 
581 		}
582 	}
583 }
584 
585 void
586 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
587 {
588 
589 	struct tm tm;		/* for strftime printing local names of
590 				 * months */
591 	date    dt;		/* handy date */
592 	int     dw;		/* width of numbers */
593 	int     first;		/* first day of month */
594 	int     firstm;		/* first day of first week of month */
595 	int     i, j, k;	/* just indices */
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;
604 	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
605 		 L"%OB", &tm);
606 	mlines->name[0] = towupper(mlines->name[0]);
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)
615 		last = firstday(y + 1, 1);
616 	else
617 		last = firstday(y, m + 2);
618 
619 	if (jd_flag)
620 		jan1 = firstday(y, 1);
621 
622 	/*
623 	 * Set firstm to the day number of monday of the first week of
624 	 * this month. (This might be in the last month)
625 	 */
626 	firstm = first - weekday(first);
627 
628 	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
629 	if (jd_flag) {
630 		ds = jdaystr;
631 		dw = 4;
632 	} else {
633 		ds = daystr;
634 		dw = 3;
635 	}
636 
637 	/*
638 	 * Fill the lines with day of month or day of year (julian day)
639 	 * line index: i, each line is one weekday. column index: j, each
640 	 * column is one day number. print column index: k.
641 	 */
642 	for (i = 0; i != 7; i++) {
643 		for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
644 			if (j >= first) {
645 				if (jd_flag)
646 					dt.d = j - jan1 + 1;
647 				else
648 					sdate(j, &dt);
649 				memcpy(mlines->lines[i] + k,
650 				       ds + dt.d * dw, dw);
651 			} else
652 				memcpy(mlines->lines[i] + k, "    ", dw);
653 		mlines->lines[i][k] = '\0';
654 
655 	}
656 
657 	/* fill the weeknumbers */
658 	if (flag_weeks) {
659 		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
660 			if (j <= nswitch)
661 				memset(mlines->weeks + k, ' ', dw);
662 			else
663 				memcpy(mlines->weeks + k,
664 				    ds + week(j, &i)*dw, dw);
665 		mlines->weeks[k] = '\0';
666 	}
667 }
668 
669 void
670 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
671 {
672 
673 	struct tm tm;		/* for strftime printing local names of
674 				 * months */
675 	date    dt;		/* handy date */
676 	int     dw;		/* width of numbers */
677 	int     first;		/* first day of month */
678 	int     firsts;		/* sunday of first week of month */
679 	int     i, j, k;	/* just indices */
680 	int     jan1 = 0;	/* the first day of this year */
681 	int     last;		/* the first day of next month */
682 	char   *ds;		/* pointer to day strings (daystr or
683 				 * jdaystr) */
684 
685 	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
686 	if (jd_flag) {
687 		ds = jdaystr;
688 		dw = 4;
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;
697 	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
698 		 L"%OB", &tm);
699 	mlines->name[0] = towupper(mlines->name[0]);
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;
708 	dt.d = 1;
709 	first = sndaysb(&dt);
710 	if (m == 11) {
711 		dt.y = y + 1;
712 		dt.m = 1;
713 		dt.d = 1;
714 	} else {
715 		dt.y = y;
716 		dt.m = m + 2;
717 		dt.d = 1;
718 	}
719 	last = sndaysb(&dt);
720 
721 	if (jd_flag) {
722 		dt.y = y;
723 		dt.m = 1;
724 		dt.d = 1;
725 		jan1 = sndaysb(&dt);
726 	}
727 
728 	/*
729 	 * Set firsts to the day number of sunday of the first week of
730 	 * this month. (This might be in the last month)
731 	 */
732 	firsts = first - (weekday(first)+1) % 7;
733 
734 	/*
735 	 * Fill the lines with day of month or day of year (Julian day)
736 	 * line index: i, each line is one week. column index: j, each
737 	 * column is one day number. print column index: k.
738 	 */
739 	for (i = 0; i != 6; i++) {
740 		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
741 		     j++, k += dw)
742 			if (j >= first) {
743 				if (jd_flag)
744 					dt.d = j - jan1 + 1;
745 				else
746 					sdateb(j, &dt);
747 				memcpy(mlines->lines[i] + k,
748 				       ds + dt.d * dw, dw);
749 			} else
750 				memcpy(mlines->lines[i] + k, "    ", dw);
751 		if (k == 0)
752 			mlines->lines[i][1] = '\0';
753 		else
754 			mlines->lines[i][k] = '\0';
755 	}
756 }
757 
758 /* Put the local names of weekdays into the wds */
759 void
760 mkweekdays(struct weekdays *wds)
761 {
762 	int i, len;
763 	struct tm tm;
764 	wchar_t buf[20];
765 
766 	memset(&tm, 0, sizeof(tm));
767 
768 	for (i = 0; i != 7; i++) {
769 		tm.tm_wday = (i+1) % 7;
770 		wcsftime(buf, sizeof(buf), L"%a", &tm);
771 		len = wcslen(buf);
772 		if (len > 2)
773 			len = 2;
774 		wcscpy(wds->names[i], L"   ");
775 		wcsncpy(wds->names[i] + 2 - len, buf, len);
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  */
784 int
785 firstday(int y, int m)
786 {
787 	date dt;
788 	int nd;
789 
790 	dt.y = y;
791 	dt.m = m;
792 	dt.d = 1;
793 	nd = sndays(&dt);
794 	for (;;) {
795 		sdate(nd, &dt);
796 		if ((dt.m >= m && dt.y == y) || dt.y > y)
797 			return (nd);
798 		else
799 			nd++;
800 	}
801 	/* NEVER REACHED */
802 }
803 
804 /*
805  * Compute the number of days from date, obey the local switch from
806  * Julian to Gregorian if specified by the user.
807  */
808 int
809 sndays(struct date *d)
810 {
811 
812 	if (nswitch != 0)
813 		if (nswitch < ndaysj(d))
814 			return (ndaysg(d));
815 		else
816 			return (ndaysj(d));
817 	else
818 		return ndaysg(d);
819 }
820 
821 /*
822  * Compute the number of days from date, obey the switch from
823  * Julian to Gregorian as used by UK and her colonies.
824  */
825 int
826 sndaysb(struct date *d)
827 {
828 
829 	if (nswitchb < ndaysj(d))
830 		return (ndaysg(d));
831 	else
832 		return (ndaysj(d));
833 }
834 
835 /* Inverse of sndays */
836 struct date *
837 sdate(int nd, struct date *d)
838 {
839 
840 	if (nswitch < nd)
841 		return (gdate(nd, d));
842 	else
843 		return (jdate(nd, d));
844 }
845 
846 /* Inverse of sndaysb */
847 struct date *
848 sdateb(int nd, struct date *d)
849 {
850 
851 	if (nswitchb < nd)
852 		return (gdate(nd, d));
853 	else
854 		return (jdate(nd, d));
855 }
856 
857 /* Center string t in string s of length w by putting enough leading blanks */
858 char *
859 center(char *s, char *t, int w)
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 */
869 wchar_t *
870 wcenter(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 
879 int
880 parsemonth(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);
887 	if (cp != s) {
888 		ny = *y;
889 		if (*cp == '\0') {
890 			;	/* no special action */
891 		} else if (*cp == 'f' || *cp == 'F') {
892 			if (nm <= *m)
893 				ny++;
894 		} else if (*cp == 'p' || *cp == 'P') {
895 			if (nm >= *m)
896 				ny--;
897 		} else
898 			return (1);
899 		if (nm < 1 || nm > 12)
900 			return 1;
901 		*m = nm;
902 		*y = ny;
903 		return (0);
904 	}
905 	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
906 		*m = tm.tm_mon + 1;
907 		return (0);
908 	}
909 	return (1);
910 }
911