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