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