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