ncal.c revision 78016
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 78016 2001-06-10 21:06:01Z ache $"; 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> 4331744Shelbig 4431744Shelbig/* Width of one month with backward compatibility */ 4531744Shelbig#define MONTH_WIDTH_B_J 27 4631744Shelbig#define MONTH_WIDTH_B 20 4731744Shelbig 4831744Shelbig#define MONTH_WIDTH_J 24 4931744Shelbig#define MONTH_WIDTH 18 5031744Shelbig 5131744Shelbig#define MAX_WIDTH 28 5231744Shelbig 5331744Shelbigtypedef struct date date; 5431744Shelbig 5531744Shelbigstruct monthlines { 5631744Shelbig char name[MAX_WIDTH + 1]; 5731744Shelbig char lines[7][MAX_WIDTH + 1]; 5831744Shelbig char weeks[MAX_WIDTH + 1]; 5931744Shelbig}; 6031744Shelbig 6131744Shelbigstruct weekdays { 6231744Shelbig char names[7][4]; 6331744Shelbig}; 6431744Shelbig 6531744Shelbig/* The switches from Julian to Gregorian in some countries */ 6631744Shelbigstatic struct djswitch { 6731744Shelbig char *cc; /* Country code according to ISO 3166 */ 6831744Shelbig char *nm; /* Name of country */ 6931744Shelbig date dt; /* Last day of Julian calendar */ 7031744Shelbig} switches[] = { 7153963Sache {"AL", "Albania", {1912, 11, 30}}, 7253963Sache {"AT", "Austria", {1583, 10, 5}}, 7353963Sache {"AU", "Australia", {1752, 9, 2}}, 7453963Sache {"BE", "Belgium", {1582, 12, 14}}, 7553963Sache {"BG", "Bulgaria", {1916, 3, 18}}, 7653963Sache {"CA", "Canada", {1752, 9, 2}}, 7753963Sache {"CH", "Switzerland", {1655, 2, 28}}, 7853963Sache {"CN", "China", {1911, 12, 18}}, 7953963Sache {"CZ", "Czech Republic",{1584, 1, 6}}, 8053963Sache {"DE", "Germany", {1700, 2, 18}}, 8153963Sache {"DK", "Denmark", {1700, 2, 18}}, 8253963Sache {"ES", "Spain", {1582, 10, 4}}, 8353963Sache {"FI", "Finland", {1753, 2, 17}}, 8453963Sache {"FR", "France", {1582, 12, 9}}, 8553963Sache {"GB", "United Kingdom",{1752, 9, 2}}, 8653963Sache {"GR", "Greece", {1924, 3, 9}}, 8753963Sache {"HU", "Hungary", {1587, 10, 21}}, 8853963Sache {"IS", "Iceland", {1700, 11, 16}}, 8953963Sache {"IT", "Italy", {1582, 10, 4}}, 9053963Sache {"JP", "Japan", {1918, 12, 18}}, 9153963Sache {"LI", "Lithuania", {1918, 2, 1}}, 9253963Sache {"LN", "Latin", {9999, 05, 31}}, 9353963Sache {"LU", "Luxembourg", {1582, 12, 14}}, 9453963Sache {"LV", "Latvia", {1918, 2, 1}}, 9553963Sache {"NL", "Netherlands", {1582, 12, 14}}, 9653963Sache {"NO", "Norway", {1700, 2, 18}}, 9753963Sache {"PL", "Poland", {1582, 10, 4}}, 9853963Sache {"PT", "Portugal", {1582, 10, 4}}, 9953963Sache {"RO", "Romania", {1919, 3, 31}}, 10053963Sache {"RU", "Russia", {1918, 1, 31}}, 10153963Sache {"SI", "Slovenia", {1919, 3, 4}}, 10253963Sache {"SW", "Sweden", {1753, 2, 17}}, 10353963Sache {"TR", "Turkey", {1926, 12, 18}}, 10453963Sache {"US", "United States", {1752, 9, 2}}, 10553963Sache {"YU", "Yugoslavia", {1919, 3, 4}} 10631744Shelbig}; 10731744Shelbig 10832215Shelbigstruct djswitch *dftswitch = 10932215Shelbig switches + sizeof(switches) / sizeof(struct djswitch) - 2; 11032215Shelbig /* default switch (should be "US") */ 11132215Shelbig 11231744Shelbig/* Table used to print day of month and week numbers */ 11331744Shelbigchar daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15" 11431744Shelbig " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31" 11531744Shelbig " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47" 11631744Shelbig " 48 49 50 51 52 53"; 11731744Shelbig 11831744Shelbig/* Table used to print day of year and week numbers */ 11931744Shelbigchar jdaystr[] = " 1 2 3 4 5 6 7 8 9" 12031744Shelbig " 10 11 12 13 14 15 16 17 18 19" 12131744Shelbig " 20 21 22 23 24 25 26 27 28 29" 12231744Shelbig " 30 31 32 33 34 35 36 37 38 39" 12331744Shelbig " 40 41 42 43 44 45 46 47 48 49" 12431744Shelbig " 50 51 52 53 54 55 56 57 58 59" 12531744Shelbig " 60 61 62 63 64 65 66 67 68 69" 12631744Shelbig " 70 71 72 73 74 75 76 77 78 79" 12731744Shelbig " 80 81 82 83 84 85 86 87 88 89" 12831744Shelbig " 90 91 92 93 94 95 96 97 98 99" 12931744Shelbig " 100 101 102 103 104 105 106 107 108 109" 13031744Shelbig " 110 111 112 113 114 115 116 117 118 119" 13131744Shelbig " 120 121 122 123 124 125 126 127 128 129" 13231744Shelbig " 130 131 132 133 134 135 136 137 138 139" 13331744Shelbig " 140 141 142 143 144 145 146 147 148 149" 13431744Shelbig " 150 151 152 153 154 155 156 157 158 159" 13531744Shelbig " 160 161 162 163 164 165 166 167 168 169" 13631744Shelbig " 170 171 172 173 174 175 176 177 178 179" 13731744Shelbig " 180 181 182 183 184 185 186 187 188 189" 13831744Shelbig " 190 191 192 193 194 195 196 197 198 199" 13931744Shelbig " 200 201 202 203 204 205 206 207 208 209" 14031744Shelbig " 210 211 212 213 214 215 216 217 218 219" 14131744Shelbig " 220 221 222 223 224 225 226 227 228 229" 14231744Shelbig " 230 231 232 233 234 235 236 237 238 239" 14331744Shelbig " 240 241 242 243 244 245 246 247 248 249" 14431744Shelbig " 250 251 252 253 254 255 256 257 258 259" 14531744Shelbig " 260 261 262 263 264 265 266 267 268 269" 14631744Shelbig " 270 271 272 273 274 275 276 277 278 279" 14731744Shelbig " 280 281 282 283 284 285 286 287 288 289" 14831744Shelbig " 290 291 292 293 294 295 296 297 298 299" 14931744Shelbig " 300 301 302 303 304 305 306 307 308 309" 15031744Shelbig " 310 311 312 313 314 315 316 317 318 319" 15131744Shelbig " 320 321 322 323 324 325 326 327 328 329" 15231744Shelbig " 330 331 332 333 334 335 336 337 338 339" 15331744Shelbig " 340 341 342 343 344 345 346 347 348 349" 15431744Shelbig " 350 351 352 353 354 355 356 357 358 359" 15531744Shelbig " 360 361 362 363 364 365 366"; 15631744Shelbig 15732139Shelbigint flag_weeks; /* user wants number of week */ 15831744Shelbigint nswitch; /* user defined switch date */ 15931744Shelbigint nswitchb; /* switch date for backward compatibility */ 16031744Shelbig 16131744Shelbigchar *center(char *s, char *t, int w); 16231744Shelbigvoid mkmonth(int year, int month, int jd_flag, struct monthlines * monthl); 16331744Shelbigvoid mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl); 16431744Shelbigvoid mkweekdays(struct weekdays * wds); 16531744Shelbigvoid printcc(void); 16631744Shelbigvoid printeaster(int year, int julian, int orthodox); 16731744Shelbigvoid printmonth(int year, int month, int jd_flag); 16831744Shelbigvoid printmonthb(int year, int month, int jd_flag); 16931744Shelbigvoid printyear(int year, int jd_flag); 17031744Shelbigvoid printyearb(int year, int jd_flag); 17132282Shelbigint firstday(int y, int m); 17231744Shelbigdate *sdate(int ndays, struct date * d); 17331744Shelbigdate *sdateb(int ndays, struct date * d); 17431744Shelbigint sndays(struct date * d); 17531744Shelbigint sndaysb(struct date * d); 17632310Scharnierstatic void usage(void); 17731744Shelbigint weekdayb(int nd); 17831744Shelbig 17931744Shelbigint 18031744Shelbigmain(int argc, char *argv[]) 18131744Shelbig{ 18231744Shelbig struct djswitch *p, *q; /* to search user defined switch date */ 18331744Shelbig date never = {10000, 1, 1}; /* outside valid range of dates */ 18431744Shelbig date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ 18531744Shelbig int ch; /* holds the option character */ 18631744Shelbig int m = 0; /* month */ 18731744Shelbig int y = 0; /* year */ 18831744Shelbig int flag_backward = 0; /* user called cal--backward compat. */ 18931744Shelbig int flag_hole_year = 0; /* user wants the whole year */ 19031744Shelbig int flag_julian_cal = 0; /* user wants Julian Calendar */ 19131744Shelbig int flag_julian_day = 0; /* user wants the Julian day 19231744Shelbig * numbers */ 19331744Shelbig int flag_orthodox = 0; /* use wants Orthodox easter */ 19431744Shelbig int flag_easter = 0; /* use wants easter date */ 19531744Shelbig char *cp; /* character pointer */ 19632215Shelbig char *locale; /* locale to get country code */ 19731744Shelbig 19832215Shelbig /* 19932215Shelbig * Use locale to determine the country code, 20032215Shelbig * and use the country code to determine the default 20132215Shelbig * switchdate and date format from the switches table. 20232215Shelbig */ 20374612Sache if (setlocale(LC_ALL, "") == NULL) 20432215Shelbig warn("setlocale"); 20574612Sache locale = setlocale(LC_TIME, NULL); 20678016Sache if (locale == NULL || 20778016Sache strcmp(locale, "C") == 0 || 20878016Sache strcmp(locale, "POSIX") == 0) 20932215Shelbig locale = "_US"; 21032215Shelbig q = switches + sizeof(switches) / sizeof(struct djswitch); 21132215Shelbig for (p = switches; p != q; p++) 21232215Shelbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 21332215Shelbig break; 21432215Shelbig if (p == q) { 21532215Shelbig nswitch = ndaysj(&dftswitch->dt); 21632215Shelbig } else { 21732215Shelbig nswitch = ndaysj(&p->dt); 21832215Shelbig dftswitch = p; 21932215Shelbig } 22031744Shelbig 22132215Shelbig 22231744Shelbig /* 22331744Shelbig * Get the filename portion of argv[0] and set flag_backward if 22431744Shelbig * this program is called "cal". 22531744Shelbig */ 22631744Shelbig for (cp = argv[0]; *cp; cp++) 22731744Shelbig ; 22831744Shelbig while (cp >= argv[0] && *cp != '/') 22931744Shelbig cp--; 23031744Shelbig if (strcmp("cal", ++cp) == 0) 23131744Shelbig flag_backward = 1; 23231744Shelbig 23331744Shelbig /* Set the switch date to United Kingdom if backwards compatible */ 23431744Shelbig if (flag_backward) 23531744Shelbig nswitchb = ndaysj(&ukswitch); 23631744Shelbig 23731744Shelbig while ((ch = getopt(argc, argv, "Jejops:wy")) != -1) 23831744Shelbig switch (ch) { 23931744Shelbig case 'J': 24031744Shelbig if (flag_backward) 24131744Shelbig usage(); 24231744Shelbig nswitch = ndaysj(&never); 24331744Shelbig flag_julian_cal = 1; 24431744Shelbig break; 24531744Shelbig case 'e': 24631744Shelbig if (flag_backward) 24731744Shelbig usage(); 24831744Shelbig flag_easter = 1; 24931744Shelbig break; 25031744Shelbig case 'j': 25131744Shelbig flag_julian_day = 1; 25231744Shelbig break; 25331744Shelbig case 'o': 25431744Shelbig if (flag_backward) 25531744Shelbig usage(); 25631744Shelbig flag_orthodox = 1; 25731744Shelbig flag_easter = 1; 25831744Shelbig break; 25931744Shelbig case 'p': 26031744Shelbig if (flag_backward) 26131744Shelbig usage(); 26231744Shelbig printcc(); 26331744Shelbig return (0); 26431744Shelbig break; 26531744Shelbig case 's': 26631744Shelbig if (flag_backward) 26731744Shelbig usage(); 26831744Shelbig q = switches + 26931744Shelbig sizeof(switches) / sizeof(struct djswitch); 27031744Shelbig for (p = switches; 27131744Shelbig p != q && strcmp(p->cc, optarg) != 0; p++) 27231744Shelbig ; 27331744Shelbig if (p == q) 27431744Shelbig errx(EX_USAGE, 27531744Shelbig "%s: invalid country code", optarg); 27631744Shelbig nswitch = ndaysj(&(p->dt)); 27731744Shelbig break; 27831744Shelbig case 'w': 27931744Shelbig if (flag_backward) 28031744Shelbig usage(); 28131744Shelbig flag_weeks = 1; 28231744Shelbig break; 28331744Shelbig case 'y': 28431744Shelbig flag_hole_year = 1; 28531744Shelbig break; 28631744Shelbig default: 28731744Shelbig usage(); 28831744Shelbig } 28931744Shelbig 29031744Shelbig argc -= optind; 29131744Shelbig argv += optind; 29231744Shelbig 29331744Shelbig if (argc == 0) { 29431744Shelbig time_t t; 29531744Shelbig struct tm *tm; 29631744Shelbig 29731744Shelbig t = time(NULL); 29831744Shelbig tm = localtime(&t); 29931744Shelbig y = tm->tm_year + 1900; 30031744Shelbig m = tm->tm_mon + 1; 30131744Shelbig } 30231744Shelbig 30331744Shelbig switch (argc) { 30431744Shelbig case 2: 30531744Shelbig if (flag_easter) 30631744Shelbig usage(); 30731744Shelbig m = atoi(*argv++); 30831744Shelbig if (m < 1 || m > 12) 30931744Shelbig errx(EX_USAGE, "month %d not in range 1..12", m); 31031744Shelbig /* FALLTHROUGH */ 31131744Shelbig case 1: 31231744Shelbig y = atoi(*argv++); 31331744Shelbig if (y < 1 || y > 9999) 31431744Shelbig errx(EX_USAGE, "year %d not in range 1..9999", y); 31531744Shelbig break; 31631744Shelbig case 0: 31731744Shelbig break; 31831744Shelbig default: 31931744Shelbig usage(); 32031744Shelbig } 32131744Shelbig 32231744Shelbig if (flag_easter) 32331744Shelbig printeaster(y, flag_julian_cal, flag_orthodox); 32431744Shelbig else if (argc == 1 || flag_hole_year) 32531744Shelbig if (flag_backward) 32631744Shelbig printyearb(y, flag_julian_day); 32731744Shelbig else 32831744Shelbig printyear(y, flag_julian_day); 32931744Shelbig else 33031744Shelbig if (flag_backward) 33131744Shelbig printmonthb(y, m, flag_julian_day); 33231744Shelbig else 33331744Shelbig printmonth(y, m, flag_julian_day); 33431744Shelbig 33531744Shelbig return (0); 33631744Shelbig} 33731744Shelbig 33832310Scharnierstatic void 33931744Shelbigusage(void) 34031744Shelbig{ 34131744Shelbig 34232310Scharnier fprintf(stderr, "%s\n%s\n%s\n", 34332525Shelbig "usage: cal [-jy] [[month] year]", 34432310Scharnier " ncal [-Jjpwy] [-s country_code] [[month] year]", 34532310Scharnier " ncal [-Jeo] [year]"); 34631744Shelbig exit(EX_USAGE); 34731744Shelbig} 34831744Shelbig 34931744Shelbig/* print the assumed switches for all countries */ 35031744Shelbigvoid 35131744Shelbigprintcc(void) 35231744Shelbig{ 35331744Shelbig struct djswitch *p; 35431744Shelbig int n; /* number of lines to print */ 35531744Shelbig int m; /* offset from left to right table entry on the same line */ 35631744Shelbig 35732215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d" 35832215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 35932215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 36031744Shelbig 36131744Shelbig n = sizeof(switches) / sizeof(struct djswitch); 36231744Shelbig m = (n + 1) / 2; 36331744Shelbig n /= 2; 36432215Shelbig for (p = switches; p != switches + n; p++) 36532215Shelbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 36631744Shelbig if (m != n) 36732215Shelbig printf(FSTR"\n", FSTRARG(p)); 36831744Shelbig} 36931744Shelbig 37031744Shelbig/* print the date of easter sunday */ 37131744Shelbigvoid 37231744Shelbigprinteaster(int y, int julian, int orthodox) 37331744Shelbig{ 37432215Shelbig date dt; 37532215Shelbig struct tm tm; 37632215Shelbig char buf[80]; 37774573Sache static int d_first = -1; 37831744Shelbig 37974573Sache if (d_first < 0) 38074573Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 38131744Shelbig /* force orthodox easter for years before 1583 */ 38231744Shelbig if (y < 1583) 38331744Shelbig orthodox = 1; 38431744Shelbig 38531744Shelbig if (orthodox) 38631744Shelbig if (julian) 38731744Shelbig easteroj(y, &dt); 38831744Shelbig else 38931744Shelbig easterog(y, &dt); 39031744Shelbig else 39131744Shelbig easterg(y, &dt); 39232215Shelbig 39332215Shelbig memset(&tm, 0, sizeof(tm)); 39432215Shelbig tm.tm_year = dt.y - 1900; 39532215Shelbig tm.tm_mon = dt.m - 1; 39632215Shelbig tm.tm_mday = dt.d; 39774573Sache strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 39832215Shelbig printf("%s\n", buf); 39931744Shelbig} 40031744Shelbig 40131744Shelbigvoid 40231744Shelbigprintmonth(int y, int m, int jd_flag) 40331744Shelbig{ 40431744Shelbig struct monthlines month; 40531744Shelbig struct weekdays wds; 40631744Shelbig int i; 40731744Shelbig 40831744Shelbig mkmonth(y, m - 1, jd_flag, &month); 40931744Shelbig mkweekdays(&wds); 41031744Shelbig printf(" %s %d\n", month.name, y); 41131744Shelbig for (i = 0; i != 7; i++) 41231744Shelbig printf("%.2s%s\n", wds.names[i], month.lines[i]); 41331744Shelbig if (flag_weeks) 41431744Shelbig printf(" %s\n", month.weeks); 41531744Shelbig} 41631744Shelbig 41731744Shelbigvoid 41831744Shelbigprintmonthb(int y, int m, int jd_flag) 41931744Shelbig{ 42031744Shelbig struct monthlines month; 42131744Shelbig struct weekdays wds; 42231744Shelbig char s[MAX_WIDTH], t[MAX_WIDTH]; 42331744Shelbig int i; 42431744Shelbig int mw; 42531744Shelbig 42631744Shelbig mkmonthb(y, m - 1, jd_flag, &month); 42731744Shelbig mkweekdays(&wds); 42831744Shelbig 42931744Shelbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 43031744Shelbig 43131744Shelbig sprintf(s, "%s %d", month.name, y); 43231744Shelbig printf("%s\n", center(t, s, mw)); 43331744Shelbig 43431744Shelbig if (jd_flag) 43531744Shelbig printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0], 43631744Shelbig wds.names[1], wds.names[2], wds.names[3], 43731744Shelbig wds.names[4], wds.names[5]); 43831744Shelbig else 43931744Shelbig printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0], 44031744Shelbig wds.names[1], wds.names[2], wds.names[3], 44131744Shelbig wds.names[4], wds.names[5]); 44231744Shelbig 44331744Shelbig for (i = 0; i != 6; i++) 44431744Shelbig printf("%s\n", month.lines[i]+1); 44531744Shelbig} 44631744Shelbig 44731744Shelbigvoid 44831744Shelbigprintyear(int y, int jd_flag) 44931744Shelbig{ 45031744Shelbig struct monthlines year[12]; 45131744Shelbig struct weekdays wds; 45231744Shelbig char s[80], t[80]; 45331744Shelbig int i, j; 45431744Shelbig int mpl; 45531744Shelbig int mw; 45631744Shelbig 45731744Shelbig for (i = 0; i != 12; i++) 45831744Shelbig mkmonth(y, i, jd_flag, year + i); 45931744Shelbig mkweekdays(&wds); 46031744Shelbig mpl = jd_flag ? 3 : 4; 46131744Shelbig mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH; 46231744Shelbig 46331744Shelbig sprintf(s, "%d", y); 46431744Shelbig printf("%s\n", center(t, s, mpl * mw)); 46531744Shelbig 46631744Shelbig for (j = 0; j != 12; j += mpl) { 46731744Shelbig printf(" %-*s%-*s", 46831744Shelbig mw, year[j].name, 46931744Shelbig mw, year[j + 1].name); 47031744Shelbig if (mpl == 3) 47131744Shelbig printf("%s\n", year[j + 2].name); 47231744Shelbig else 47331744Shelbig printf("%-*s%s\n", 47431744Shelbig mw, year[j + 2].name, 47531744Shelbig year[j + 3].name); 47631744Shelbig for (i = 0; i != 7; i++) { 47731744Shelbig printf("%.2s%-*s%-*s", 47831744Shelbig wds.names[i], 47931744Shelbig mw, year[j].lines[i], 48031744Shelbig mw, year[j + 1].lines[i]); 48131744Shelbig if (mpl == 3) 48231744Shelbig printf("%s\n", year[j + 2].lines[i]); 48331744Shelbig else 48431744Shelbig printf("%-*s%s\n", 48531744Shelbig mw, year[j + 2].lines[i], 48631744Shelbig year[j + 3].lines[i]); 48731744Shelbig } 48848566Sbillf if (flag_weeks) { 48931744Shelbig if (mpl == 3) 49031744Shelbig printf(" %-*s%-*s%-s\n", 49131744Shelbig mw, year[j].weeks, 49231744Shelbig mw, year[j + 1].weeks, 49331744Shelbig year[j + 2].weeks); 49431744Shelbig else 49531744Shelbig printf(" %-*s%-*s%-*s%-s\n", 49631744Shelbig mw, year[j].weeks, 49731744Shelbig mw, year[j + 1].weeks, 49831744Shelbig mw, year[j + 2].weeks, 49931744Shelbig year[j + 3].weeks); 50048566Sbillf } 50131744Shelbig } 50231744Shelbig} 50331744Shelbig 50431744Shelbigvoid 50531744Shelbigprintyearb(int y, int jd_flag) 50631744Shelbig{ 50731744Shelbig struct monthlines year[12]; 50831744Shelbig struct weekdays wds; 50931744Shelbig char s[80], t[80]; 51031744Shelbig int i, j; 51131744Shelbig int mpl; 51231744Shelbig int mw; 51331744Shelbig 51431744Shelbig for (i = 0; i != 12; i++) 51531744Shelbig mkmonthb(y, i, jd_flag, year + i); 51631744Shelbig mkweekdays(&wds); 51731744Shelbig mpl = jd_flag ? 2 : 3; 51831744Shelbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 51931744Shelbig 52031744Shelbig sprintf(s, "%d", y); 52131744Shelbig printf("%s\n\n", center(t, s, mw * mpl + mpl)); 52231744Shelbig 52331744Shelbig for (j = 0; j != 12; j += mpl) { 52431744Shelbig printf("%-*s ", mw, center(s, year[j].name, mw)); 52531744Shelbig if (mpl == 2) 52631744Shelbig printf("%s\n", center(s, year[j + 1].name, mw)); 52731744Shelbig else 52831744Shelbig printf("%-*s %s\n", mw, 52931744Shelbig center(s, year[j + 1].name, mw), 53031744Shelbig center(t, year[j + 2].name, mw)); 53131744Shelbig 53231744Shelbig if (mpl == 2) 53331744Shelbig printf(" %s %s %s %s %s %s %s " 53431744Shelbig " %s %s %s %s %s %s %.2s\n", 53531744Shelbig wds.names[6], wds.names[0], wds.names[1], 53631744Shelbig wds.names[2], wds.names[3], wds.names[4], 53731744Shelbig wds.names[5], 53831744Shelbig wds.names[6], wds.names[0], wds.names[1], 53931744Shelbig wds.names[2], wds.names[3], wds.names[4], 54031744Shelbig wds.names[5]); 54131744Shelbig else 54231744Shelbig printf("%s%s%s%s%s%s%s " 54331744Shelbig "%s%s%s%s%s%s%s " 54431744Shelbig "%s%s%s%s%s%s%.2s\n", 54531744Shelbig wds.names[6], wds.names[0], wds.names[1], 54631744Shelbig wds.names[2], wds.names[3], wds.names[4], 54731744Shelbig wds.names[5], 54831744Shelbig wds.names[6], wds.names[0], wds.names[1], 54931744Shelbig wds.names[2], wds.names[3], wds.names[4], 55031744Shelbig wds.names[5], 55131744Shelbig wds.names[6], wds.names[0], wds.names[1], 55231744Shelbig wds.names[2], wds.names[3], wds.names[4], 55331744Shelbig wds.names[5]); 55431744Shelbig for (i = 0; i != 6; i++) { 55531744Shelbig if (mpl == 2) 55631744Shelbig printf("%-*s %s\n", 55731744Shelbig mw, year[j].lines[i]+1, 55831744Shelbig year[j + 1].lines[i]+1); 55931744Shelbig else 56031744Shelbig printf("%-*s %-*s %s\n", 56131744Shelbig mw, year[j].lines[i]+1, 56231744Shelbig mw, year[j + 1].lines[i]+1, 56331744Shelbig year[j + 2].lines[i]+1); 56431744Shelbig 56531744Shelbig } 56631744Shelbig } 56731744Shelbig} 56831744Shelbig 56931744Shelbigvoid 57031744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines) 57131744Shelbig{ 57231744Shelbig 57331744Shelbig struct tm tm; /* for strftime printing local names of 57431744Shelbig * months */ 57531744Shelbig date dt; /* handy date */ 57631744Shelbig int dw; /* width of numbers */ 57731744Shelbig int first; /* first day of month */ 57831744Shelbig int firstm; /* first day of first week of month */ 57931744Shelbig int i, j, k; /* just indices */ 58031744Shelbig int last; /* the first day of next month */ 58131744Shelbig int jan1 = 0; /* the first day of this year */ 58231744Shelbig char *ds; /* pointer to day strings (daystr or 58331744Shelbig * jdaystr) */ 58431744Shelbig 58531744Shelbig /* Set name of month. */ 58631744Shelbig memset(&tm, 0, sizeof(tm)); 58731744Shelbig tm.tm_mon = m; 58851201Sache strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 58974612Sache mlines->name[0] = toupper((unsigned char)mlines->name[0]); 59031744Shelbig 59131744Shelbig /* 59231744Shelbig * Set first and last to the day number of the first day of this 59331744Shelbig * month and the first day of next month respectively. Set jan1 to 59432282Shelbig * the day number of the first day of this year. 59531744Shelbig */ 59632282Shelbig first = firstday(y, m + 1); 59732282Shelbig if (m == 11) 59832282Shelbig last = firstday(y + 1, 1); 59932282Shelbig else 60032282Shelbig last = firstday(y, m + 2); 60131744Shelbig 60232282Shelbig if (jd_flag) 60332525Shelbig jan1 = firstday(y, 1); 60431744Shelbig 60531744Shelbig /* 60631744Shelbig * Set firstm to the day number of monday of the first week of 60731744Shelbig * this month. (This might be in the last month) 60831744Shelbig */ 60931744Shelbig firstm = first - weekday(first); 61031744Shelbig 61131744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 61231744Shelbig if (jd_flag) { 61331744Shelbig ds = jdaystr; 61431744Shelbig dw = 4; 61531744Shelbig } else { 61631744Shelbig ds = daystr; 61731744Shelbig dw = 3; 61831744Shelbig } 61931744Shelbig 62031744Shelbig /* 62131744Shelbig * Fill the lines with day of month or day of year (julian day) 62231744Shelbig * line index: i, each line is one weekday. column index: j, each 62331744Shelbig * column is one day number. print column index: k. 62431744Shelbig */ 62531744Shelbig for (i = 0; i != 7; i++) { 62631744Shelbig for (j = firstm + i, k = 0; j < last; j += 7, k += dw) 62731744Shelbig if (j >= first) { 62831744Shelbig if (jd_flag) 62931744Shelbig dt.d = j - jan1 + 1; 63031744Shelbig else 63131744Shelbig sdate(j, &dt); 63231744Shelbig memcpy(mlines->lines[i] + k, 63331744Shelbig ds + dt.d * dw, dw); 63431744Shelbig } else 63531744Shelbig memcpy(mlines->lines[i] + k, " ", dw); 63631744Shelbig mlines->lines[i][k] = '\0'; 63731744Shelbig 63831744Shelbig } 63931744Shelbig 64031744Shelbig /* fill the weeknumbers */ 64131744Shelbig if (flag_weeks) { 64231744Shelbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 64331744Shelbig if (j <= nswitch) 64431744Shelbig memset(mlines->weeks + k, ' ', dw); 64531744Shelbig else 64631744Shelbig memcpy(mlines->weeks + k, 64731744Shelbig ds + week(j, &i)*dw, dw); 64831744Shelbig mlines->weeks[k] = '\0'; 64931744Shelbig } 65031744Shelbig} 65131744Shelbig 65231744Shelbigvoid 65331744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 65431744Shelbig{ 65531744Shelbig 65631744Shelbig struct tm tm; /* for strftime printing local names of 65731744Shelbig * months */ 65831744Shelbig date dt; /* handy date */ 65931744Shelbig int dw; /* width of numbers */ 66031744Shelbig int first; /* first day of month */ 66131744Shelbig int firsts; /* sunday of first week of month */ 66231744Shelbig int i, j, k; /* just indices */ 66331744Shelbig int jan1 = 0; /* the first day of this year */ 66431744Shelbig int last; /* the first day of next month */ 66531744Shelbig char *ds; /* pointer to day strings (daystr or 66631744Shelbig * jdaystr) */ 66731744Shelbig 66831744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 66931744Shelbig if (jd_flag) { 67031744Shelbig ds = jdaystr; 67131744Shelbig dw = 4; 67231744Shelbig } else { 67331744Shelbig ds = daystr; 67431744Shelbig dw = 3; 67531744Shelbig } 67631744Shelbig 67731744Shelbig /* Set name of month centered */ 67831744Shelbig memset(&tm, 0, sizeof(tm)); 67931744Shelbig tm.tm_mon = m; 68051201Sache strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 68174612Sache mlines->name[0] = toupper((unsigned char)mlines->name[0]); 68231744Shelbig 68331744Shelbig /* 68431744Shelbig * Set first and last to the day number of the first day of this 68531744Shelbig * month and the first day of next month respectively. Set jan1 to 68631744Shelbig * the day number of Jan 1st of this year. 68731744Shelbig */ 68831744Shelbig dt.y = y; 68931744Shelbig dt.m = m + 1; 69031744Shelbig dt.d = 1; 69131744Shelbig first = sndaysb(&dt); 69231744Shelbig if (m == 11) { 69331744Shelbig dt.y = y + 1; 69431744Shelbig dt.m = 1; 69531744Shelbig dt.d = 1; 69631744Shelbig } else { 69731744Shelbig dt.y = y; 69831744Shelbig dt.m = m + 2; 69931744Shelbig dt.d = 1; 70031744Shelbig } 70131744Shelbig last = sndaysb(&dt); 70231744Shelbig 70331744Shelbig if (jd_flag) { 70431744Shelbig dt.y = y; 70531744Shelbig dt.m = 1; 70631744Shelbig dt.d = 1; 70731744Shelbig jan1 = sndaysb(&dt); 70831744Shelbig } 70931744Shelbig 71031744Shelbig /* 71131744Shelbig * Set firsts to the day number of sunday of the first week of 71231744Shelbig * this month. (This might be in the last month) 71331744Shelbig */ 71431744Shelbig firsts = first - (weekday(first)+1) % 7; 71531744Shelbig 71631744Shelbig /* 71731744Shelbig * Fill the lines with day of month or day of year (Julian day) 71831744Shelbig * line index: i, each line is one week. column index: j, each 71931744Shelbig * column is one day number. print column index: k. 72031744Shelbig */ 72131744Shelbig for (i = 0; i != 6; i++) { 72231744Shelbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 72331744Shelbig j++, k += dw) 72431744Shelbig if (j >= first) { 72531744Shelbig if (jd_flag) 72631744Shelbig dt.d = j - jan1 + 1; 72731744Shelbig else 72831744Shelbig sdateb(j, &dt); 72931744Shelbig memcpy(mlines->lines[i] + k, 73031744Shelbig ds + dt.d * dw, dw); 73131744Shelbig } else 73231744Shelbig memcpy(mlines->lines[i] + k, " ", dw); 73331744Shelbig if (k == 0) 73431744Shelbig mlines->lines[i][1] = '\0'; 73531744Shelbig else 73631744Shelbig mlines->lines[i][k] = '\0'; 73731744Shelbig } 73831744Shelbig} 73931744Shelbig 74031744Shelbig/* Put the local names of weekdays into the wds */ 74132139Shelbigvoid 74232139Shelbigmkweekdays(struct weekdays *wds) 74331744Shelbig{ 74474618Sache int i, len; 74531744Shelbig struct tm tm; 74674618Sache char buf[20]; 74731744Shelbig 74831744Shelbig memset(&tm, 0, sizeof(tm)); 74931744Shelbig 75031744Shelbig for (i = 0; i != 7; i++) { 75131744Shelbig tm.tm_wday = (i+1) % 7; 75274618Sache strftime(buf, sizeof(buf), "%a", &tm); 75374618Sache len = strlen(buf); 75474618Sache if (len > 2) 75574618Sache len = 2; 75674618Sache strcpy(wds->names[i], " "); 75774618Sache strncpy(wds->names[i] + 2 - len, buf, len); 75831744Shelbig } 75931744Shelbig} 76031744Shelbig 76131744Shelbig/* 76245064Shelbig * Compute the day number of the first 76345064Shelbig * existing date after the first day in month. 76445064Shelbig * (the first day in month and even the month might not exist!) 76532282Shelbig */ 76632282Shelbigint 76732282Shelbigfirstday(int y, int m) 76832282Shelbig{ 76932282Shelbig date dt; 77032282Shelbig int nd; 77132282Shelbig 77232282Shelbig dt.y = y; 77332282Shelbig dt.m = m; 77432282Shelbig dt.d = 1; 77545064Shelbig nd = sndays(&dt); 77645064Shelbig for (;;) { 77745064Shelbig sdate(nd, &dt); 77845064Shelbig if ((dt.m >= m && dt.y == y) || dt.y > y) 77945064Shelbig return (nd); 78045064Shelbig else 78145064Shelbig nd++; 78245064Shelbig } 78345064Shelbig /* NEVER REACHED */ 78432282Shelbig} 78532282Shelbig 78632282Shelbig/* 78731744Shelbig * Compute the number of days from date, obey the local switch from 78831744Shelbig * Julian to Gregorian if specified by the user. 78931744Shelbig */ 79031744Shelbigint 79131744Shelbigsndays(struct date *d) 79231744Shelbig{ 79331744Shelbig 79431744Shelbig if (nswitch != 0) 79531744Shelbig if (nswitch < ndaysj(d)) 79631744Shelbig return (ndaysg(d)); 79731744Shelbig else 79831744Shelbig return (ndaysj(d)); 79931744Shelbig else 80031744Shelbig return ndaysg(d); 80131744Shelbig} 80231744Shelbig 80331744Shelbig/* 80431744Shelbig * Compute the number of days from date, obey the switch from 80531744Shelbig * Julian to Gregorian as used by UK and her colonies. 80631744Shelbig */ 80731744Shelbigint 80831744Shelbigsndaysb(struct date *d) 80931744Shelbig{ 81031744Shelbig 81131744Shelbig if (nswitchb < ndaysj(d)) 81231744Shelbig return (ndaysg(d)); 81331744Shelbig else 81431744Shelbig return (ndaysj(d)); 81531744Shelbig} 81631744Shelbig 81731744Shelbig/* Inverse of sndays */ 81831744Shelbigstruct date * 81931744Shelbigsdate(int nd, struct date *d) 82031744Shelbig{ 82131744Shelbig 82231744Shelbig if (nswitch < nd) 82331744Shelbig return (gdate(nd, d)); 82431744Shelbig else 82531744Shelbig return (jdate(nd, d)); 82631744Shelbig} 82731744Shelbig 82831744Shelbig/* Inverse of sndaysb */ 82931744Shelbigstruct date * 83031744Shelbigsdateb(int nd, struct date *d) 83131744Shelbig{ 83231744Shelbig 83331744Shelbig if (nswitchb < nd) 83431744Shelbig return (gdate(nd, d)); 83531744Shelbig else 83631744Shelbig return (jdate(nd, d)); 83731744Shelbig} 83831744Shelbig 83931744Shelbig/* Center string t in string s of length w by putting enough leading blanks */ 84031744Shelbigchar * 84131744Shelbigcenter(char *s, char *t, int w) 84231744Shelbig{ 84331744Shelbig char blanks[80]; 84431744Shelbig 84531744Shelbig memset(blanks, ' ', sizeof(blanks)); 84631744Shelbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 84731744Shelbig return (s); 84831744Shelbig} 849