ncal.c revision 194447
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 194447 2009-06-18 16:40:00Z ume $";
3032310Scharnier#endif /* not lint */
3132310Scharnier
3231744Shelbig#include <calendar.h>
3374612Sache#include <ctype.h>
3431744Shelbig#include <err.h>
3574573Sache#include <langinfo.h>
3631744Shelbig#include <locale.h>
3731744Shelbig#include <stdio.h>
3831744Shelbig#include <stdlib.h>
3931744Shelbig#include <string.h>
4031744Shelbig#include <sysexits.h>
4131744Shelbig#include <time.h>
4231744Shelbig#include <unistd.h>
43189804Sdas#include <wchar.h>
44189804Sdas#include <wctype.h>
45191330Srdivacky#include <term.h>
46191330Srdivacky#undef lines			/* term.h defines this */
4731744Shelbig
4831744Shelbig/* Width of one month with backward compatibility */
4931744Shelbig#define MONTH_WIDTH_B_J 27
5031744Shelbig#define MONTH_WIDTH_B 20
5131744Shelbig
5231744Shelbig#define MONTH_WIDTH_J 24
5331744Shelbig#define MONTH_WIDTH 18
5431744Shelbig
55191330Srdivacky#define MAX_WIDTH 64
5631744Shelbig
5731744Shelbigtypedef struct date date;
5831744Shelbig
5931744Shelbigstruct monthlines {
60189804Sdas	wchar_t name[MAX_WIDTH + 1];
6131744Shelbig	char lines[7][MAX_WIDTH + 1];
6231744Shelbig	char weeks[MAX_WIDTH + 1];
6331744Shelbig};
6431744Shelbig
6531744Shelbigstruct weekdays {
66189804Sdas	wchar_t names[7][4];
6731744Shelbig};
6831744Shelbig
6931744Shelbig/* The switches from Julian to Gregorian in some countries */
7031744Shelbigstatic struct djswitch {
7195641Smarkm	const char *cc;	/* Country code according to ISO 3166 */
7295641Smarkm	const char *nm;	/* Name of country */
7331744Shelbig	date dt;	/* Last day of Julian calendar */
7431744Shelbig} switches[] = {
7553963Sache	{"AL", "Albania",       {1912, 11, 30}},
7653963Sache	{"AT", "Austria",       {1583, 10,  5}},
7753963Sache	{"AU", "Australia",     {1752,  9,  2}},
7853963Sache	{"BE", "Belgium",       {1582, 12, 14}},
7953963Sache	{"BG", "Bulgaria",      {1916,  3, 18}},
8053963Sache	{"CA", "Canada",        {1752,  9,  2}},
8153963Sache	{"CH", "Switzerland",   {1655,  2, 28}},
8253963Sache	{"CN", "China",         {1911, 12, 18}},
8353963Sache	{"CZ", "Czech Republic",{1584,  1,  6}},
8453963Sache	{"DE", "Germany",       {1700,  2, 18}},
8553963Sache	{"DK", "Denmark",       {1700,  2, 18}},
8653963Sache	{"ES", "Spain",         {1582, 10,  4}},
8753963Sache	{"FI", "Finland",       {1753,  2, 17}},
8853963Sache	{"FR", "France",        {1582, 12,  9}},
8953963Sache	{"GB", "United Kingdom",{1752,  9,  2}},
9053963Sache	{"GR", "Greece",        {1924,  3,  9}},
9153963Sache	{"HU", "Hungary",       {1587, 10, 21}},
9253963Sache	{"IS", "Iceland",       {1700, 11, 16}},
9353963Sache	{"IT", "Italy",         {1582, 10,  4}},
9453963Sache	{"JP", "Japan",         {1918, 12, 18}},
9553963Sache	{"LI", "Lithuania",     {1918,  2,  1}},
9653963Sache	{"LN", "Latin",         {9999, 05, 31}},
9753963Sache	{"LU", "Luxembourg",    {1582, 12, 14}},
9853963Sache	{"LV", "Latvia",        {1918,  2,  1}},
9953963Sache	{"NL", "Netherlands",   {1582, 12, 14}},
10053963Sache	{"NO", "Norway",        {1700,  2, 18}},
10153963Sache	{"PL", "Poland",        {1582, 10,  4}},
10253963Sache	{"PT", "Portugal",      {1582, 10,  4}},
10353963Sache	{"RO", "Romania",       {1919,  3, 31}},
10453963Sache	{"RU", "Russia",        {1918,  1, 31}},
10553963Sache	{"SI", "Slovenia",      {1919,  3,  4}},
10653963Sache	{"SW", "Sweden",        {1753,  2, 17}},
10753963Sache	{"TR", "Turkey",        {1926, 12, 18}},
10853963Sache	{"US", "United States", {1752,  9,  2}},
10953963Sache	{"YU", "Yugoslavia",    {1919,  3,  4}}
11031744Shelbig};
11131744Shelbig
11232215Shelbigstruct djswitch *dftswitch =
11332215Shelbig    switches + sizeof(switches) / sizeof(struct djswitch) - 2;
11432215Shelbig    /* default switch (should be "US") */
11532215Shelbig
11631744Shelbig/* Table used to print day of month and week numbers */
11731744Shelbigchar daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
11831744Shelbig		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
11931744Shelbig		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
12031744Shelbig		" 48 49 50 51 52 53";
12131744Shelbig
12231744Shelbig/* Table used to print day of year and week numbers */
12331744Shelbigchar jdaystr[] = "       1   2   3   4   5   6   7   8   9"
12431744Shelbig		 "  10  11  12  13  14  15  16  17  18  19"
12531744Shelbig		 "  20  21  22  23  24  25  26  27  28  29"
12631744Shelbig		 "  30  31  32  33  34  35  36  37  38  39"
12731744Shelbig		 "  40  41  42  43  44  45  46  47  48  49"
12831744Shelbig		 "  50  51  52  53  54  55  56  57  58  59"
12931744Shelbig		 "  60  61  62  63  64  65  66  67  68  69"
13031744Shelbig		 "  70  71  72  73  74  75  76  77  78  79"
13131744Shelbig		 "  80  81  82  83  84  85  86  87  88  89"
13231744Shelbig		 "  90  91  92  93  94  95  96  97  98  99"
13331744Shelbig		 " 100 101 102 103 104 105 106 107 108 109"
13431744Shelbig		 " 110 111 112 113 114 115 116 117 118 119"
13531744Shelbig		 " 120 121 122 123 124 125 126 127 128 129"
13631744Shelbig		 " 130 131 132 133 134 135 136 137 138 139"
13731744Shelbig		 " 140 141 142 143 144 145 146 147 148 149"
13831744Shelbig		 " 150 151 152 153 154 155 156 157 158 159"
13931744Shelbig		 " 160 161 162 163 164 165 166 167 168 169"
14031744Shelbig		 " 170 171 172 173 174 175 176 177 178 179"
14131744Shelbig		 " 180 181 182 183 184 185 186 187 188 189"
14231744Shelbig		 " 190 191 192 193 194 195 196 197 198 199"
14331744Shelbig		 " 200 201 202 203 204 205 206 207 208 209"
14431744Shelbig		 " 210 211 212 213 214 215 216 217 218 219"
14531744Shelbig		 " 220 221 222 223 224 225 226 227 228 229"
14631744Shelbig		 " 230 231 232 233 234 235 236 237 238 239"
14731744Shelbig		 " 240 241 242 243 244 245 246 247 248 249"
14831744Shelbig		 " 250 251 252 253 254 255 256 257 258 259"
14931744Shelbig		 " 260 261 262 263 264 265 266 267 268 269"
15031744Shelbig		 " 270 271 272 273 274 275 276 277 278 279"
15131744Shelbig		 " 280 281 282 283 284 285 286 287 288 289"
15231744Shelbig		 " 290 291 292 293 294 295 296 297 298 299"
15331744Shelbig		 " 300 301 302 303 304 305 306 307 308 309"
15431744Shelbig		 " 310 311 312 313 314 315 316 317 318 319"
15531744Shelbig		 " 320 321 322 323 324 325 326 327 328 329"
15631744Shelbig		 " 330 331 332 333 334 335 336 337 338 339"
15731744Shelbig		 " 340 341 342 343 344 345 346 347 348 349"
15831744Shelbig		 " 350 351 352 353 354 355 356 357 358 359"
15931744Shelbig		 " 360 361 362 363 364 365 366";
16031744Shelbig
16132139Shelbigint     flag_weeks;		/* user wants number of week */
16231744Shelbigint     nswitch;		/* user defined switch date */
16331744Shelbigint	nswitchb;		/* switch date for backward compatibility */
164191737Srdivackyconst char	*term_so, *term_se;
165191330Srdivackyint	today;
16631744Shelbig
16731744Shelbigchar   *center(char *s, char *t, int w);
168189804Sdaswchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
16931744Shelbigvoid	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
17031744Shelbigvoid    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
17131744Shelbigvoid    mkweekdays(struct weekdays * wds);
172186401Swollmanint     parsemonth(const char *s, int *m, int *y);
17331744Shelbigvoid    printcc(void);
17431744Shelbigvoid    printeaster(int year, int julian, int orthodox);
17531744Shelbigvoid    printmonth(int year, int month, int jd_flag);
17631744Shelbigvoid    printmonthb(int year, int month, int jd_flag);
17731744Shelbigvoid    printyear(int year, int jd_flag);
17831744Shelbigvoid    printyearb(int year, int jd_flag);
17932282Shelbigint	firstday(int y, int m);
18031744Shelbigdate   *sdate(int ndays, struct date * d);
18131744Shelbigdate   *sdateb(int ndays, struct date * d);
18231744Shelbigint     sndays(struct date * d);
18331744Shelbigint     sndaysb(struct date * d);
18432310Scharnierstatic void usage(void);
18531744Shelbigint     weekdayb(int nd);
18631744Shelbig
18731744Shelbigint
18831744Shelbigmain(int argc, char *argv[])
18931744Shelbig{
19031744Shelbig	struct  djswitch *p, *q;	/* to search user defined switch date */
19131744Shelbig	date	never = {10000, 1, 1};	/* outside valid range of dates */
19231744Shelbig	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
19331744Shelbig	int     ch;			/* holds the option character */
19431744Shelbig	int     m = 0;			/* month */
19531744Shelbig	int	y = 0;			/* year */
19631744Shelbig	int     flag_backward = 0;	/* user called cal--backward compat. */
19731744Shelbig	int     flag_hole_year = 0;	/* user wants the whole year */
19831744Shelbig	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
19931744Shelbig	int     flag_julian_day = 0;	/* user wants the Julian day
20031744Shelbig					 * numbers */
20131744Shelbig	int	flag_orthodox = 0;	/* use wants Orthodox easter */
20231744Shelbig	int	flag_easter = 0;	/* use wants easter date */
20331744Shelbig	char	*cp;			/* character pointer */
204138036Swollman	char	*flag_month = NULL;	/* requested month as string */
20595641Smarkm	const char    *locale;		/* locale to get country code */
206191330Srdivacky	char tbuf[1024], cbuf[512], *b;
207191330Srdivacky	time_t t;
208191330Srdivacky	struct tm *tm1;
20931744Shelbig
210191737Srdivacky	term_se = term_so = NULL;
211191330Srdivacky	today = 0;
212191364Srdivacky	if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
213191330Srdivacky		date	dt;		/* handy date */
214191330Srdivacky
215191330Srdivacky		b = cbuf;
216191737Srdivacky		term_so = tgetstr("so", &b);
217191737Srdivacky		term_se = tgetstr("se", &b);
218191330Srdivacky		t = time(NULL);
219191330Srdivacky		tm1 = localtime(&t);
220191330Srdivacky		dt.y = tm1->tm_year + 1900;
221191330Srdivacky		dt.m = tm1->tm_mon + 1;
222191330Srdivacky		dt.d = tm1->tm_mday;
223191330Srdivacky
224191330Srdivacky		today = sndaysb(&dt);
225191330Srdivacky	}
226191330Srdivacky
22732215Shelbig	/*
22832215Shelbig	 * Use locale to determine the country code,
22932215Shelbig	 * and use the country code to determine the default
23032215Shelbig	 * switchdate and date format from the switches table.
23132215Shelbig	 */
23274612Sache	if (setlocale(LC_ALL, "") == NULL)
23332215Shelbig		warn("setlocale");
23474612Sache	locale = setlocale(LC_TIME, NULL);
23578016Sache	if (locale == NULL ||
23678016Sache	    strcmp(locale, "C") == 0 ||
23778027Sache	    strcmp(locale, "POSIX") == 0 ||
23878027Sache	    strcmp(locale, "ASCII") == 0 ||
23978027Sache	    strcmp(locale, "US-ASCII") == 0)
24032215Shelbig		locale = "_US";
24132215Shelbig	q = switches + sizeof(switches) / sizeof(struct djswitch);
24232215Shelbig	for (p = switches; p != q; p++)
24332215Shelbig		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
24432215Shelbig			break;
24532215Shelbig	if (p == q) {
24632215Shelbig		nswitch = ndaysj(&dftswitch->dt);
24732215Shelbig	} else {
24832215Shelbig		nswitch = ndaysj(&p->dt);
24932215Shelbig		dftswitch = p;
25032215Shelbig	}
25131744Shelbig
25232215Shelbig
25331744Shelbig	/*
25431744Shelbig	 * Get the filename portion of argv[0] and set flag_backward if
25531744Shelbig	 * this program is called "cal".
25631744Shelbig	 */
257138036Swollman	cp = strrchr(argv[0], '/');
258138036Swollman	cp = (cp == NULL) ? argv[0] : cp + 1;
259138036Swollman	if (strcmp("cal", cp) == 0)
26031744Shelbig		flag_backward = 1;
26131744Shelbig
26231744Shelbig	/* Set the switch date to United Kingdom if backwards compatible */
26331744Shelbig	if (flag_backward)
26431744Shelbig		nswitchb = ndaysj(&ukswitch);
26531744Shelbig
266191364Srdivacky	while ((ch = getopt(argc, argv, "Jehjm:ops:wy")) != -1)
26731744Shelbig		switch (ch) {
26831744Shelbig		case 'J':
26931744Shelbig			if (flag_backward)
27031744Shelbig				usage();
27131744Shelbig			nswitch = ndaysj(&never);
27231744Shelbig			flag_julian_cal = 1;
27331744Shelbig			break;
274191364Srdivacky		case 'h':
275191737Srdivacky			term_so = term_se = NULL;
276191364Srdivacky			break;
27731744Shelbig		case 'e':
27831744Shelbig			if (flag_backward)
27931744Shelbig				usage();
28031744Shelbig			flag_easter = 1;
28131744Shelbig			break;
28231744Shelbig		case 'j':
28331744Shelbig			flag_julian_day = 1;
28431744Shelbig			break;
285138036Swollman		case 'm':
286138036Swollman			flag_month = optarg;
287138036Swollman			break;
28831744Shelbig		case 'o':
28931744Shelbig			if (flag_backward)
29031744Shelbig				usage();
29131744Shelbig			flag_orthodox = 1;
29231744Shelbig			flag_easter = 1;
29331744Shelbig			break;
29431744Shelbig		case 'p':
29531744Shelbig			if (flag_backward)
29631744Shelbig				usage();
29731744Shelbig			printcc();
29831744Shelbig			return (0);
29931744Shelbig			break;
30031744Shelbig		case 's':
30131744Shelbig			if (flag_backward)
30231744Shelbig				usage();
30331744Shelbig			q = switches +
30431744Shelbig			    sizeof(switches) / sizeof(struct djswitch);
30531744Shelbig			for (p = switches;
30631744Shelbig			     p != q && strcmp(p->cc, optarg) != 0; p++)
30731744Shelbig				;
30831744Shelbig			if (p == q)
30931744Shelbig				errx(EX_USAGE,
31031744Shelbig				    "%s: invalid country code", optarg);
31131744Shelbig			nswitch = ndaysj(&(p->dt));
31231744Shelbig			break;
31331744Shelbig		case 'w':
31431744Shelbig			if (flag_backward)
31531744Shelbig				usage();
31631744Shelbig			flag_weeks = 1;
31731744Shelbig			break;
31831744Shelbig		case 'y':
31931744Shelbig			flag_hole_year = 1;
32031744Shelbig			break;
32131744Shelbig		default:
32231744Shelbig			usage();
32331744Shelbig		}
32431744Shelbig
32531744Shelbig	argc -= optind;
32631744Shelbig	argv += optind;
32731744Shelbig
32831744Shelbig	switch (argc) {
32931744Shelbig	case 2:
33031744Shelbig		if (flag_easter)
33131744Shelbig			usage();
332138036Swollman		flag_month = *argv++;
33331744Shelbig		/* FALLTHROUGH */
33431744Shelbig	case 1:
33531744Shelbig		y = atoi(*argv++);
33631744Shelbig		if (y < 1 || y > 9999)
33731744Shelbig			errx(EX_USAGE, "year %d not in range 1..9999", y);
33831744Shelbig		break;
33931744Shelbig	case 0:
340138036Swollman		{
341138036Swollman			time_t t;
342138036Swollman			struct tm *tm;
343138036Swollman
344138036Swollman			t = time(NULL);
345138036Swollman			tm = localtime(&t);
346138036Swollman			y = tm->tm_year + 1900;
347138036Swollman			m = tm->tm_mon + 1;
348138036Swollman		}
34931744Shelbig		break;
35031744Shelbig	default:
35131744Shelbig		usage();
35231744Shelbig	}
35331744Shelbig
354138036Swollman	if (flag_month != NULL) {
355186401Swollman		if (parsemonth(flag_month, &m, &y)) {
356138036Swollman			errx(EX_USAGE,
357138036Swollman			    "%s is neither a month number (1..12) nor a name",
358138036Swollman			    flag_month);
359186401Swollman		}
360138036Swollman	}
361138036Swollman
36231744Shelbig	if (flag_easter)
36331744Shelbig		printeaster(y, flag_julian_cal, flag_orthodox);
364191330Srdivacky	else if (argc == 1 || flag_hole_year) {
365191330Srdivacky		/* disable the highlight for now */
366191330Srdivacky		today = 0;
36731744Shelbig		if (flag_backward)
36831744Shelbig			printyearb(y, flag_julian_day);
36931744Shelbig		else
37031744Shelbig			printyear(y, flag_julian_day);
371191330Srdivacky	} else
37231744Shelbig		if (flag_backward)
37331744Shelbig			printmonthb(y, m, flag_julian_day);
37431744Shelbig		else
37531744Shelbig			printmonth(y, m, flag_julian_day);
37631744Shelbig
37731744Shelbig	return (0);
37831744Shelbig}
37931744Shelbig
38032310Scharnierstatic void
38131744Shelbigusage(void)
38231744Shelbig{
38331744Shelbig
384138036Swollman	fputs(
385191737Srdivacky	    "usage: cal [-hjy] [[month] year]\n"
386191737Srdivacky	    "       cal [-hj] [-m month] [year]\n"
387191737Srdivacky	    "       ncal [-hJjpwy] [-s country_code] [[month] year]\n"
388191737Srdivacky	    "       ncal [-hJeo] [year]\n", stderr);
38931744Shelbig	exit(EX_USAGE);
39031744Shelbig}
39131744Shelbig
39231744Shelbig/* print the assumed switches for all countries */
39331744Shelbigvoid
39431744Shelbigprintcc(void)
39531744Shelbig{
39631744Shelbig	struct djswitch *p;
39731744Shelbig	int n;	/* number of lines to print */
39831744Shelbig	int m;	/* offset from left to right table entry on the same line */
39931744Shelbig
40032215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d"
40132215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
40232215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
40331744Shelbig
40431744Shelbig	n = sizeof(switches) / sizeof(struct djswitch);
40531744Shelbig	m = (n + 1) / 2;
40631744Shelbig	n /= 2;
40732215Shelbig	for (p = switches; p != switches + n; p++)
40832215Shelbig		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
40931744Shelbig	if (m != n)
41032215Shelbig		printf(FSTR"\n", FSTRARG(p));
41131744Shelbig}
41231744Shelbig
41331744Shelbig/* print the date of easter sunday */
41431744Shelbigvoid
41531744Shelbigprinteaster(int y, int julian, int orthodox)
41631744Shelbig{
41732215Shelbig	date    dt;
41832215Shelbig	struct tm tm;
41932215Shelbig	char    buf[80];
42074573Sache	static int d_first = -1;
42131744Shelbig
42274573Sache	if (d_first < 0)
42374573Sache		d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
42431744Shelbig	/* force orthodox easter for years before 1583 */
42531744Shelbig	if (y < 1583)
42631744Shelbig		orthodox = 1;
42731744Shelbig
42831744Shelbig	if (orthodox)
42931744Shelbig		if (julian)
43031744Shelbig			easteroj(y, &dt);
43131744Shelbig		else
43231744Shelbig			easterog(y, &dt);
43331744Shelbig	else
43431744Shelbig		easterg(y, &dt);
43532215Shelbig
43632215Shelbig	memset(&tm, 0, sizeof(tm));
43732215Shelbig	tm.tm_year = dt.y - 1900;
43832215Shelbig	tm.tm_mon  = dt.m - 1;
43932215Shelbig	tm.tm_mday = dt.d;
44074573Sache	strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y",  &tm);
44132215Shelbig	printf("%s\n", buf);
44231744Shelbig}
44331744Shelbig
44431744Shelbigvoid
44531744Shelbigprintmonth(int y, int m, int jd_flag)
44631744Shelbig{
44731744Shelbig	struct monthlines month;
44831744Shelbig	struct weekdays wds;
449194366Sume	int i, len;
45031744Shelbig
45131744Shelbig	mkmonth(y, m - 1, jd_flag, &month);
45231744Shelbig	mkweekdays(&wds);
453189804Sdas	printf("    %ls %d\n", month.name, y);
454194366Sume	for (i = 0; i != 7; i++) {
455194366Sume		len = wcslen(wds.names[i]);
456194366Sume		if (wcswidth(wds.names[i], len) == len)
457194366Sume			wprintf(L"%.2ls%s\n", wds.names[i], month.lines[i]);
458194366Sume		else
459194366Sume			wprintf(L"%.1ls%s\n", wds.names[i], month.lines[i]);
460194366Sume	}
46131744Shelbig	if (flag_weeks)
46231744Shelbig		printf("  %s\n", month.weeks);
46331744Shelbig}
46431744Shelbig
46531744Shelbigvoid
46631744Shelbigprintmonthb(int y, int m, int jd_flag)
46731744Shelbig{
46831744Shelbig	struct monthlines month;
46931744Shelbig	struct weekdays wds;
470189804Sdas	wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
47131744Shelbig	int i;
47231744Shelbig	int mw;
47331744Shelbig
47431744Shelbig	mkmonthb(y, m - 1, jd_flag, &month);
47531744Shelbig	mkweekdays(&wds);
47631744Shelbig
47731744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
47831744Shelbig
479189804Sdas	swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
480189804Sdas	wprintf(L"%ls\n", wcenter(t, s, mw));
48131744Shelbig
48231744Shelbig	if (jd_flag)
483189804Sdas		wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
484189804Sdas			wds.names[6], wds.names[0],
48531744Shelbig			wds.names[1], wds.names[2], wds.names[3],
48631744Shelbig			wds.names[4], wds.names[5]);
48731744Shelbig	else
488189804Sdas		wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6],
489189804Sdas			wds.names[0], wds.names[1], wds.names[2], wds.names[3],
49031744Shelbig			wds.names[4], wds.names[5]);
49131744Shelbig
49231744Shelbig	for (i = 0; i != 6; i++)
49331744Shelbig		printf("%s\n", month.lines[i]+1);
49431744Shelbig}
49531744Shelbig
49631744Shelbigvoid
49731744Shelbigprintyear(int y, int jd_flag)
49831744Shelbig{
49931744Shelbig	struct monthlines year[12];
50031744Shelbig	struct weekdays wds;
50131744Shelbig	char    s[80], t[80];
50231744Shelbig	int     i, j;
50331744Shelbig	int     mpl;
50431744Shelbig	int     mw;
50531744Shelbig
50631744Shelbig	for (i = 0; i != 12; i++)
50731744Shelbig		mkmonth(y, i, jd_flag, year + i);
50831744Shelbig	mkweekdays(&wds);
50931744Shelbig	mpl = jd_flag ? 3 : 4;
51031744Shelbig	mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
51131744Shelbig
51231744Shelbig	sprintf(s, "%d", y);
51331744Shelbig	printf("%s\n", center(t, s, mpl * mw));
51431744Shelbig
51531744Shelbig	for (j = 0; j != 12; j += mpl) {
516191690Sru		wprintf(L"    %-*ls%-*ls",
51731744Shelbig		    mw, year[j].name,
51831744Shelbig		    mw, year[j + 1].name);
51931744Shelbig		if (mpl == 3)
520189804Sdas			printf("%ls\n", year[j + 2].name);
52131744Shelbig		else
522191690Sru			wprintf(L"%-*ls%ls\n",
52331744Shelbig		    	    mw, year[j + 2].name,
52431744Shelbig		    	    year[j + 3].name);
52531744Shelbig		for (i = 0; i != 7; i++) {
526191690Sru			wprintf(L"%.2ls%-*s%-*s",
52731744Shelbig			    wds.names[i],
52831744Shelbig			    mw, year[j].lines[i],
52931744Shelbig			    mw, year[j + 1].lines[i]);
53031744Shelbig			if (mpl == 3)
53131744Shelbig				printf("%s\n", year[j + 2].lines[i]);
53231744Shelbig			else
53331744Shelbig				printf("%-*s%s\n",
53431744Shelbig			    	    mw, year[j + 2].lines[i],
53531744Shelbig			    	    year[j + 3].lines[i]);
53631744Shelbig		}
53748566Sbillf		if (flag_weeks) {
53831744Shelbig			if (mpl == 3)
53931744Shelbig				printf("  %-*s%-*s%-s\n",
54031744Shelbig				    mw, year[j].weeks,
54131744Shelbig				    mw, year[j + 1].weeks,
54231744Shelbig				    year[j + 2].weeks);
54331744Shelbig			else
54431744Shelbig				printf("  %-*s%-*s%-*s%-s\n",
54531744Shelbig				    mw, year[j].weeks,
54631744Shelbig				    mw, year[j + 1].weeks,
54731744Shelbig				    mw, year[j + 2].weeks,
54831744Shelbig				    year[j + 3].weeks);
54948566Sbillf		}
55031744Shelbig	}
55131744Shelbig}
55231744Shelbig
55331744Shelbigvoid
55431744Shelbigprintyearb(int y, int jd_flag)
55531744Shelbig{
55631744Shelbig	struct monthlines year[12];
55731744Shelbig	struct weekdays wds;
55831744Shelbig	char	s[80], t[80];
559189804Sdas	wchar_t	ws[80], wt[80];
56031744Shelbig	int     i, j;
56131744Shelbig	int     mpl;
56231744Shelbig	int     mw;
56331744Shelbig
56431744Shelbig	for (i = 0; i != 12; i++)
56531744Shelbig		mkmonthb(y, i, jd_flag, year + i);
56631744Shelbig	mkweekdays(&wds);
56731744Shelbig	mpl = jd_flag ? 2 : 3;
56831744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
56931744Shelbig
57031744Shelbig	sprintf(s, "%d", y);
57131744Shelbig	printf("%s\n\n", center(t, s, mw * mpl + mpl));
57231744Shelbig
57331744Shelbig	for (j = 0; j != 12; j += mpl) {
574191690Sru		wprintf(L"%-*ls  ", mw, wcenter(ws, year[j].name, mw));
57531744Shelbig		if (mpl == 2)
576189804Sdas			printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
57731744Shelbig		else
578191690Sru			wprintf(L"%-*ls  %ls\n", mw,
579189804Sdas			    wcenter(ws, year[j + 1].name, mw),
580189804Sdas			    wcenter(wt, year[j + 2].name, mw));
58131744Shelbig
58231744Shelbig		if (mpl == 2)
583189804Sdas			wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
584189804Sdas				" %ls %ls %ls %ls %ls %ls %.2ls\n",
58531744Shelbig				wds.names[6], wds.names[0], wds.names[1],
58631744Shelbig				wds.names[2], wds.names[3], wds.names[4],
58731744Shelbig				wds.names[5],
58831744Shelbig				wds.names[6], wds.names[0], wds.names[1],
58931744Shelbig				wds.names[2], wds.names[3], wds.names[4],
59031744Shelbig				wds.names[5]);
59131744Shelbig		else
592189804Sdas			wprintf(L"%ls%ls%ls%ls%ls%ls%ls "
593189804Sdas				"%ls%ls%ls%ls%ls%ls%ls "
594189804Sdas				"%ls%ls%ls%ls%ls%ls%.2ls\n",
59531744Shelbig				wds.names[6], wds.names[0], wds.names[1],
59631744Shelbig				wds.names[2], wds.names[3], wds.names[4],
59731744Shelbig				wds.names[5],
59831744Shelbig				wds.names[6], wds.names[0], wds.names[1],
59931744Shelbig				wds.names[2], wds.names[3], wds.names[4],
60031744Shelbig				wds.names[5],
60131744Shelbig				wds.names[6], wds.names[0], wds.names[1],
60231744Shelbig				wds.names[2], wds.names[3], wds.names[4],
60331744Shelbig				wds.names[5]);
60431744Shelbig		for (i = 0; i != 6; i++) {
60531744Shelbig			if (mpl == 2)
60631744Shelbig				printf("%-*s  %s\n",
60731744Shelbig			    mw, year[j].lines[i]+1,
60831744Shelbig			    year[j + 1].lines[i]+1);
60931744Shelbig			else
61031744Shelbig				printf("%-*s  %-*s  %s\n",
61131744Shelbig			    mw, year[j].lines[i]+1,
61231744Shelbig			    mw, year[j + 1].lines[i]+1,
61331744Shelbig			    year[j + 2].lines[i]+1);
61431744Shelbig
61531744Shelbig		}
61631744Shelbig	}
61731744Shelbig}
61831744Shelbig
61931744Shelbigvoid
62031744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
62131744Shelbig{
62231744Shelbig
62331744Shelbig	struct tm tm;		/* for strftime printing local names of
62431744Shelbig				 * months */
62531744Shelbig	date    dt;		/* handy date */
62631744Shelbig	int     dw;		/* width of numbers */
62731744Shelbig	int     first;		/* first day of month */
62831744Shelbig	int     firstm;		/* first day of first week of month */
629191330Srdivacky	int     i, j, k, l;	/* just indices */
63031744Shelbig	int     last;		/* the first day of next month */
63131744Shelbig	int     jan1 = 0;	/* the first day of this year */
63231744Shelbig	char   *ds;		/* pointer to day strings (daystr or
63331744Shelbig				 * jdaystr) */
63431744Shelbig
63531744Shelbig	/* Set name of month. */
63631744Shelbig	memset(&tm, 0, sizeof(tm));
63731744Shelbig	tm.tm_mon = m;
638189804Sdas	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
639189804Sdas		 L"%OB", &tm);
640189804Sdas	mlines->name[0] = towupper(mlines->name[0]);
64131744Shelbig
64231744Shelbig	/*
64331744Shelbig	 * Set first and last to the day number of the first day of this
64431744Shelbig	 * month and the first day of next month respectively. Set jan1 to
64532282Shelbig	 * the day number of the first day of this year.
64631744Shelbig	 */
64732282Shelbig	first = firstday(y, m + 1);
64832282Shelbig	if (m == 11)
64932282Shelbig		last = firstday(y + 1, 1);
65032282Shelbig	else
65132282Shelbig		last = firstday(y, m + 2);
65231744Shelbig
65332282Shelbig	if (jd_flag)
65432525Shelbig		jan1 = firstday(y, 1);
65531744Shelbig
65631744Shelbig	/*
65731744Shelbig	 * Set firstm to the day number of monday of the first week of
65831744Shelbig	 * this month. (This might be in the last month)
65931744Shelbig	 */
66031744Shelbig	firstm = first - weekday(first);
66131744Shelbig
66231744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
66331744Shelbig	if (jd_flag) {
66431744Shelbig		ds = jdaystr;
66531744Shelbig		dw = 4;
66631744Shelbig	} else {
66731744Shelbig		ds = daystr;
66831744Shelbig		dw = 3;
66931744Shelbig	}
67031744Shelbig
67131744Shelbig	/*
67231744Shelbig	 * Fill the lines with day of month or day of year (julian day)
67331744Shelbig	 * line index: i, each line is one weekday. column index: j, each
67431744Shelbig	 * column is one day number. print column index: k.
67531744Shelbig	 */
67631744Shelbig	for (i = 0; i != 7; i++) {
677191330Srdivacky		l = 0;
678191330Srdivacky		for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
679191737Srdivacky			if (j == today && (term_so != NULL && term_se != NULL)) {
680191737Srdivacky				l = strlen(term_so);
681191330Srdivacky				if (jd_flag)
682191330Srdivacky					dt.d = j - jan1 + 1;
683191330Srdivacky				else
684191330Srdivacky					sdateb(j, &dt);
685191330Srdivacky				/* separator */
686191330Srdivacky				mlines->lines[i][k] = ' ';
687191330Srdivacky				/* the actual text */
688191330Srdivacky				memcpy(mlines->lines[i] + k + l,
689191330Srdivacky				    ds + dt.d * dw, dw);
690191330Srdivacky				/* highlight on */
691191737Srdivacky				memcpy(mlines->lines[i] + k + 1, term_so, l);
692191330Srdivacky				/* highlight off */
693191737Srdivacky				memcpy(mlines->lines[i] + k + l + dw, term_se,
694191737Srdivacky				    strlen(term_se));
695191737Srdivacky				l = strlen(term_se) + strlen(term_so);
696191330Srdivacky				continue;
697191330Srdivacky			}
69831744Shelbig			if (j >= first) {
69931744Shelbig				if (jd_flag)
70031744Shelbig					dt.d = j - jan1 + 1;
70131744Shelbig				else
70231744Shelbig					sdate(j, &dt);
703191330Srdivacky				memcpy(mlines->lines[i] + k + l,
70431744Shelbig				       ds + dt.d * dw, dw);
70531744Shelbig			} else
706191330Srdivacky				memcpy(mlines->lines[i] + k + l, "    ", dw);
707191330Srdivacky		}
708191330Srdivacky		mlines->lines[i][k + l] = '\0';
70931744Shelbig
71031744Shelbig	}
71131744Shelbig
71231744Shelbig	/* fill the weeknumbers */
71331744Shelbig	if (flag_weeks) {
71431744Shelbig		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
71531744Shelbig			if (j <= nswitch)
71631744Shelbig				memset(mlines->weeks + k, ' ', dw);
71731744Shelbig			else
71831744Shelbig				memcpy(mlines->weeks + k,
71931744Shelbig				    ds + week(j, &i)*dw, dw);
72031744Shelbig		mlines->weeks[k] = '\0';
72131744Shelbig	}
72231744Shelbig}
72331744Shelbig
72431744Shelbigvoid
72531744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
72631744Shelbig{
72731744Shelbig
72831744Shelbig	struct tm tm;		/* for strftime printing local names of
72931744Shelbig				 * months */
73031744Shelbig	date    dt;		/* handy date */
73131744Shelbig	int     dw;		/* width of numbers */
73231744Shelbig	int     first;		/* first day of month */
73331744Shelbig	int     firsts;		/* sunday of first week of month */
734191330Srdivacky	int     i, j, k, l;	/* just indices */
73531744Shelbig	int     jan1 = 0;	/* the first day of this year */
73631744Shelbig	int     last;		/* the first day of next month */
73731744Shelbig	char   *ds;		/* pointer to day strings (daystr or
73831744Shelbig				 * jdaystr) */
73931744Shelbig
74031744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
74131744Shelbig	if (jd_flag) {
74231744Shelbig		ds = jdaystr;
74331744Shelbig		dw = 4;
74431744Shelbig	} else {
74531744Shelbig		ds = daystr;
74631744Shelbig		dw = 3;
74731744Shelbig	}
74831744Shelbig
74931744Shelbig	/* Set name of month centered */
75031744Shelbig	memset(&tm, 0, sizeof(tm));
75131744Shelbig	tm.tm_mon = m;
752189804Sdas	wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
753189804Sdas		 L"%OB", &tm);
754189804Sdas	mlines->name[0] = towupper(mlines->name[0]);
75531744Shelbig
75631744Shelbig	/*
75731744Shelbig	 * Set first and last to the day number of the first day of this
75831744Shelbig	 * month and the first day of next month respectively. Set jan1 to
75931744Shelbig	 * the day number of Jan 1st of this year.
76031744Shelbig	 */
76131744Shelbig	dt.y = y;
76231744Shelbig	dt.m = m + 1;
76331744Shelbig	dt.d = 1;
76431744Shelbig	first = sndaysb(&dt);
76531744Shelbig	if (m == 11) {
76631744Shelbig		dt.y = y + 1;
76731744Shelbig		dt.m = 1;
76831744Shelbig		dt.d = 1;
76931744Shelbig	} else {
77031744Shelbig		dt.y = y;
77131744Shelbig		dt.m = m + 2;
77231744Shelbig		dt.d = 1;
77331744Shelbig	}
77431744Shelbig	last = sndaysb(&dt);
77531744Shelbig
77631744Shelbig	if (jd_flag) {
77731744Shelbig		dt.y = y;
77831744Shelbig		dt.m = 1;
77931744Shelbig		dt.d = 1;
78031744Shelbig		jan1 = sndaysb(&dt);
78131744Shelbig	}
78231744Shelbig
78331744Shelbig	/*
78431744Shelbig	 * Set firsts to the day number of sunday of the first week of
78531744Shelbig	 * this month. (This might be in the last month)
78631744Shelbig	 */
78731744Shelbig	firsts = first - (weekday(first)+1) % 7;
78831744Shelbig
78931744Shelbig	/*
79031744Shelbig	 * Fill the lines with day of month or day of year (Julian day)
79131744Shelbig	 * line index: i, each line is one week. column index: j, each
79231744Shelbig	 * column is one day number. print column index: k.
79331744Shelbig	 */
79431744Shelbig	for (i = 0; i != 6; i++) {
795191330Srdivacky		l = 0;
79631744Shelbig		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
797191330Srdivacky		    j++, k += dw) {
798191737Srdivacky			if (j == today && (term_so != NULL && term_se != NULL)) {
799191737Srdivacky				l = strlen(term_so);
800191330Srdivacky				if (jd_flag)
801191330Srdivacky					dt.d = j - jan1 + 1;
802191330Srdivacky				else
803191330Srdivacky					sdateb(j, &dt);
804191330Srdivacky				/* separator */
805191330Srdivacky				mlines->lines[i][k] = ' ';
806191330Srdivacky				/* the actual text */
807191330Srdivacky				memcpy(mlines->lines[i] + k + l,
808191330Srdivacky				    ds + dt.d * dw, dw);
809191330Srdivacky				/* highlight on */
810191737Srdivacky				memcpy(mlines->lines[i] + k + 1, term_so, l);
811191330Srdivacky				/* highlight off */
812191737Srdivacky				memcpy(mlines->lines[i] + k + l + dw, term_se,
813191737Srdivacky				    strlen(term_se));
814191737Srdivacky				l = strlen(term_se) + strlen(term_so);
815191330Srdivacky				continue;
816191330Srdivacky			}
81731744Shelbig			if (j >= first) {
81831744Shelbig				if (jd_flag)
81931744Shelbig					dt.d = j - jan1 + 1;
82031744Shelbig				else
82131744Shelbig					sdateb(j, &dt);
822191330Srdivacky				memcpy(mlines->lines[i] + k + l,
82331744Shelbig				       ds + dt.d * dw, dw);
82431744Shelbig			} else
825191330Srdivacky				memcpy(mlines->lines[i] + k + l, "    ", dw);
826191330Srdivacky		}
82731744Shelbig		if (k == 0)
82831744Shelbig			mlines->lines[i][1] = '\0';
82931744Shelbig		else
830191330Srdivacky			mlines->lines[i][k + l] = '\0';
83131744Shelbig	}
83231744Shelbig}
83331744Shelbig
83431744Shelbig/* Put the local names of weekdays into the wds */
83532139Shelbigvoid
83632139Shelbigmkweekdays(struct weekdays *wds)
83731744Shelbig{
838194366Sume	int i, len, width = 0;
83931744Shelbig	struct tm tm;
840189804Sdas	wchar_t buf[20];
84131744Shelbig
84231744Shelbig	memset(&tm, 0, sizeof(tm));
84331744Shelbig
84431744Shelbig	for (i = 0; i != 7; i++) {
84531744Shelbig		tm.tm_wday = (i+1) % 7;
846189804Sdas		wcsftime(buf, sizeof(buf), L"%a", &tm);
847194447Sume		for (len = 2; len > 0; --len) {
848194366Sume			if ((width = wcswidth(buf, len)) <= 2)
849194366Sume				break;
850194366Sume		}
851194366Sume		wmemset(wds->names[i], L'\0', 4);
852194366Sume		if (width == 1)
853194366Sume			wds->names[i][0] = L' ';
854194366Sume		wcsncat(wds->names[i], buf, len);
855194447Sume		wcsncat(wds->names[i], L" ", 1);
85631744Shelbig	}
85731744Shelbig}
85831744Shelbig
85931744Shelbig/*
86045064Shelbig * Compute the day number of the first
86145064Shelbig * existing date after the first day in month.
86245064Shelbig * (the first day in month and even the month might not exist!)
86332282Shelbig */
86432282Shelbigint
86532282Shelbigfirstday(int y, int m)
86632282Shelbig{
86732282Shelbig	date dt;
86832282Shelbig	int nd;
86932282Shelbig
87032282Shelbig	dt.y = y;
87132282Shelbig	dt.m = m;
87232282Shelbig	dt.d = 1;
87345064Shelbig	nd = sndays(&dt);
87445064Shelbig	for (;;) {
87545064Shelbig		sdate(nd, &dt);
87645064Shelbig		if ((dt.m >= m && dt.y == y) || dt.y > y)
87745064Shelbig			return (nd);
87845064Shelbig		else
87945064Shelbig			nd++;
88045064Shelbig	}
88145064Shelbig	/* NEVER REACHED */
88232282Shelbig}
88332282Shelbig
88432282Shelbig/*
88531744Shelbig * Compute the number of days from date, obey the local switch from
88631744Shelbig * Julian to Gregorian if specified by the user.
88731744Shelbig */
88831744Shelbigint
88931744Shelbigsndays(struct date *d)
89031744Shelbig{
89131744Shelbig
89231744Shelbig	if (nswitch != 0)
89331744Shelbig		if (nswitch < ndaysj(d))
89431744Shelbig			return (ndaysg(d));
89531744Shelbig		else
89631744Shelbig			return (ndaysj(d));
89731744Shelbig	else
89831744Shelbig		return ndaysg(d);
89931744Shelbig}
90031744Shelbig
90131744Shelbig/*
90231744Shelbig * Compute the number of days from date, obey the switch from
90331744Shelbig * Julian to Gregorian as used by UK and her colonies.
90431744Shelbig */
90531744Shelbigint
90631744Shelbigsndaysb(struct date *d)
90731744Shelbig{
90831744Shelbig
90931744Shelbig	if (nswitchb < ndaysj(d))
91031744Shelbig		return (ndaysg(d));
91131744Shelbig	else
91231744Shelbig		return (ndaysj(d));
91331744Shelbig}
91431744Shelbig
91531744Shelbig/* Inverse of sndays */
91631744Shelbigstruct date *
91731744Shelbigsdate(int nd, struct date *d)
91831744Shelbig{
91931744Shelbig
92031744Shelbig	if (nswitch < nd)
92131744Shelbig		return (gdate(nd, d));
92231744Shelbig	else
92331744Shelbig		return (jdate(nd, d));
92431744Shelbig}
92531744Shelbig
92631744Shelbig/* Inverse of sndaysb */
92731744Shelbigstruct date *
92831744Shelbigsdateb(int nd, struct date *d)
92931744Shelbig{
93031744Shelbig
93131744Shelbig	if (nswitchb < nd)
93231744Shelbig		return (gdate(nd, d));
93331744Shelbig	else
93431744Shelbig		return (jdate(nd, d));
93531744Shelbig}
93631744Shelbig
93731744Shelbig/* Center string t in string s of length w by putting enough leading blanks */
93831744Shelbigchar *
93931744Shelbigcenter(char *s, char *t, int w)
94031744Shelbig{
94131744Shelbig	char blanks[80];
94231744Shelbig
94331744Shelbig	memset(blanks, ' ', sizeof(blanks));
94431744Shelbig	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
94531744Shelbig	return (s);
94631744Shelbig}
947104369Sroam
948189804Sdas/* Center string t in string s of length w by putting enough leading blanks */
949189804Sdaswchar_t *
950189804Sdaswcenter(wchar_t *s, wchar_t *t, int w)
951189804Sdas{
952189804Sdas	char blanks[80];
953189804Sdas
954189804Sdas	memset(blanks, ' ', sizeof(blanks));
955189804Sdas	swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
956189804Sdas	return (s);
957189804Sdas}
958189804Sdas
959104369Sroamint
960186401Swollmanparsemonth(const char *s, int *m, int *y)
961104369Sroam{
962186401Swollman	int nm, ny;
963104369Sroam	char *cp;
964104369Sroam	struct tm tm;
965104369Sroam
966186401Swollman	nm = (int)strtol(s, &cp, 10);
967186401Swollman	if (cp != s) {
968186401Swollman		ny = *y;
969186401Swollman		if (*cp == '\0') {
970186401Swollman			;	/* no special action */
971186401Swollman		} else if (*cp == 'f' || *cp == 'F') {
972186401Swollman			if (nm <= *m)
973186401Swollman				ny++;
974186401Swollman		} else if (*cp == 'p' || *cp == 'P') {
975186401Swollman			if (nm >= *m)
976186401Swollman				ny--;
977186401Swollman		} else
978186401Swollman			return (1);
979186401Swollman		if (nm < 1 || nm > 12)
980186401Swollman			return 1;
981186401Swollman		*m = nm;
982186401Swollman		*y = ny;
983186401Swollman		return (0);
984186401Swollman	}
985186401Swollman	if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {
986186401Swollman		*m = tm.tm_mon + 1;
987186401Swollman		return (0);
988186401Swollman	}
989186401Swollman	return (1);
990104369Sroam}
991