ncal.c revision 95641
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 95641 2002-04-28 12:25:03Z markm $"; 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 { 6795641Smarkm const char *cc; /* Country code according to ISO 3166 */ 6895641Smarkm const 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 */ 19695641Smarkm const 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 || 20878027Sache strcmp(locale, "POSIX") == 0 || 20978027Sache strcmp(locale, "ASCII") == 0 || 21078027Sache strcmp(locale, "US-ASCII") == 0) 21132215Shelbig locale = "_US"; 21232215Shelbig q = switches + sizeof(switches) / sizeof(struct djswitch); 21332215Shelbig for (p = switches; p != q; p++) 21432215Shelbig if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_') 21532215Shelbig break; 21632215Shelbig if (p == q) { 21732215Shelbig nswitch = ndaysj(&dftswitch->dt); 21832215Shelbig } else { 21932215Shelbig nswitch = ndaysj(&p->dt); 22032215Shelbig dftswitch = p; 22132215Shelbig } 22231744Shelbig 22332215Shelbig 22431744Shelbig /* 22531744Shelbig * Get the filename portion of argv[0] and set flag_backward if 22631744Shelbig * this program is called "cal". 22731744Shelbig */ 22831744Shelbig for (cp = argv[0]; *cp; cp++) 22931744Shelbig ; 23031744Shelbig while (cp >= argv[0] && *cp != '/') 23131744Shelbig cp--; 23231744Shelbig if (strcmp("cal", ++cp) == 0) 23331744Shelbig flag_backward = 1; 23431744Shelbig 23531744Shelbig /* Set the switch date to United Kingdom if backwards compatible */ 23631744Shelbig if (flag_backward) 23731744Shelbig nswitchb = ndaysj(&ukswitch); 23831744Shelbig 23931744Shelbig while ((ch = getopt(argc, argv, "Jejops:wy")) != -1) 24031744Shelbig switch (ch) { 24131744Shelbig case 'J': 24231744Shelbig if (flag_backward) 24331744Shelbig usage(); 24431744Shelbig nswitch = ndaysj(&never); 24531744Shelbig flag_julian_cal = 1; 24631744Shelbig break; 24731744Shelbig case 'e': 24831744Shelbig if (flag_backward) 24931744Shelbig usage(); 25031744Shelbig flag_easter = 1; 25131744Shelbig break; 25231744Shelbig case 'j': 25331744Shelbig flag_julian_day = 1; 25431744Shelbig break; 25531744Shelbig case 'o': 25631744Shelbig if (flag_backward) 25731744Shelbig usage(); 25831744Shelbig flag_orthodox = 1; 25931744Shelbig flag_easter = 1; 26031744Shelbig break; 26131744Shelbig case 'p': 26231744Shelbig if (flag_backward) 26331744Shelbig usage(); 26431744Shelbig printcc(); 26531744Shelbig return (0); 26631744Shelbig break; 26731744Shelbig case 's': 26831744Shelbig if (flag_backward) 26931744Shelbig usage(); 27031744Shelbig q = switches + 27131744Shelbig sizeof(switches) / sizeof(struct djswitch); 27231744Shelbig for (p = switches; 27331744Shelbig p != q && strcmp(p->cc, optarg) != 0; p++) 27431744Shelbig ; 27531744Shelbig if (p == q) 27631744Shelbig errx(EX_USAGE, 27731744Shelbig "%s: invalid country code", optarg); 27831744Shelbig nswitch = ndaysj(&(p->dt)); 27931744Shelbig break; 28031744Shelbig case 'w': 28131744Shelbig if (flag_backward) 28231744Shelbig usage(); 28331744Shelbig flag_weeks = 1; 28431744Shelbig break; 28531744Shelbig case 'y': 28631744Shelbig flag_hole_year = 1; 28731744Shelbig break; 28831744Shelbig default: 28931744Shelbig usage(); 29031744Shelbig } 29131744Shelbig 29231744Shelbig argc -= optind; 29331744Shelbig argv += optind; 29431744Shelbig 29531744Shelbig if (argc == 0) { 29631744Shelbig time_t t; 29731744Shelbig struct tm *tm; 29831744Shelbig 29931744Shelbig t = time(NULL); 30031744Shelbig tm = localtime(&t); 30131744Shelbig y = tm->tm_year + 1900; 30231744Shelbig m = tm->tm_mon + 1; 30331744Shelbig } 30431744Shelbig 30531744Shelbig switch (argc) { 30631744Shelbig case 2: 30731744Shelbig if (flag_easter) 30831744Shelbig usage(); 30931744Shelbig m = atoi(*argv++); 31031744Shelbig if (m < 1 || m > 12) 31131744Shelbig errx(EX_USAGE, "month %d not in range 1..12", m); 31231744Shelbig /* FALLTHROUGH */ 31331744Shelbig case 1: 31431744Shelbig y = atoi(*argv++); 31531744Shelbig if (y < 1 || y > 9999) 31631744Shelbig errx(EX_USAGE, "year %d not in range 1..9999", y); 31731744Shelbig break; 31831744Shelbig case 0: 31931744Shelbig break; 32031744Shelbig default: 32131744Shelbig usage(); 32231744Shelbig } 32331744Shelbig 32431744Shelbig if (flag_easter) 32531744Shelbig printeaster(y, flag_julian_cal, flag_orthodox); 32631744Shelbig else if (argc == 1 || flag_hole_year) 32731744Shelbig if (flag_backward) 32831744Shelbig printyearb(y, flag_julian_day); 32931744Shelbig else 33031744Shelbig printyear(y, flag_julian_day); 33131744Shelbig else 33231744Shelbig if (flag_backward) 33331744Shelbig printmonthb(y, m, flag_julian_day); 33431744Shelbig else 33531744Shelbig printmonth(y, m, flag_julian_day); 33631744Shelbig 33731744Shelbig return (0); 33831744Shelbig} 33931744Shelbig 34032310Scharnierstatic void 34131744Shelbigusage(void) 34231744Shelbig{ 34331744Shelbig 34432310Scharnier fprintf(stderr, "%s\n%s\n%s\n", 34532525Shelbig "usage: cal [-jy] [[month] year]", 34632310Scharnier " ncal [-Jjpwy] [-s country_code] [[month] year]", 34732310Scharnier " ncal [-Jeo] [year]"); 34831744Shelbig exit(EX_USAGE); 34931744Shelbig} 35031744Shelbig 35131744Shelbig/* print the assumed switches for all countries */ 35231744Shelbigvoid 35331744Shelbigprintcc(void) 35431744Shelbig{ 35531744Shelbig struct djswitch *p; 35631744Shelbig int n; /* number of lines to print */ 35731744Shelbig int m; /* offset from left to right table entry on the same line */ 35831744Shelbig 35932215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d" 36032215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ') 36132215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d 36231744Shelbig 36331744Shelbig n = sizeof(switches) / sizeof(struct djswitch); 36431744Shelbig m = (n + 1) / 2; 36531744Shelbig n /= 2; 36632215Shelbig for (p = switches; p != switches + n; p++) 36732215Shelbig printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m)); 36831744Shelbig if (m != n) 36932215Shelbig printf(FSTR"\n", FSTRARG(p)); 37031744Shelbig} 37131744Shelbig 37231744Shelbig/* print the date of easter sunday */ 37331744Shelbigvoid 37431744Shelbigprinteaster(int y, int julian, int orthodox) 37531744Shelbig{ 37632215Shelbig date dt; 37732215Shelbig struct tm tm; 37832215Shelbig char buf[80]; 37974573Sache static int d_first = -1; 38031744Shelbig 38174573Sache if (d_first < 0) 38274573Sache d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); 38331744Shelbig /* force orthodox easter for years before 1583 */ 38431744Shelbig if (y < 1583) 38531744Shelbig orthodox = 1; 38631744Shelbig 38731744Shelbig if (orthodox) 38831744Shelbig if (julian) 38931744Shelbig easteroj(y, &dt); 39031744Shelbig else 39131744Shelbig easterog(y, &dt); 39231744Shelbig else 39331744Shelbig easterg(y, &dt); 39432215Shelbig 39532215Shelbig memset(&tm, 0, sizeof(tm)); 39632215Shelbig tm.tm_year = dt.y - 1900; 39732215Shelbig tm.tm_mon = dt.m - 1; 39832215Shelbig tm.tm_mday = dt.d; 39974573Sache strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm); 40032215Shelbig printf("%s\n", buf); 40131744Shelbig} 40231744Shelbig 40331744Shelbigvoid 40431744Shelbigprintmonth(int y, int m, int jd_flag) 40531744Shelbig{ 40631744Shelbig struct monthlines month; 40731744Shelbig struct weekdays wds; 40831744Shelbig int i; 40931744Shelbig 41031744Shelbig mkmonth(y, m - 1, jd_flag, &month); 41131744Shelbig mkweekdays(&wds); 41231744Shelbig printf(" %s %d\n", month.name, y); 41331744Shelbig for (i = 0; i != 7; i++) 41431744Shelbig printf("%.2s%s\n", wds.names[i], month.lines[i]); 41531744Shelbig if (flag_weeks) 41631744Shelbig printf(" %s\n", month.weeks); 41731744Shelbig} 41831744Shelbig 41931744Shelbigvoid 42031744Shelbigprintmonthb(int y, int m, int jd_flag) 42131744Shelbig{ 42231744Shelbig struct monthlines month; 42331744Shelbig struct weekdays wds; 42431744Shelbig char s[MAX_WIDTH], t[MAX_WIDTH]; 42531744Shelbig int i; 42631744Shelbig int mw; 42731744Shelbig 42831744Shelbig mkmonthb(y, m - 1, jd_flag, &month); 42931744Shelbig mkweekdays(&wds); 43031744Shelbig 43131744Shelbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 43231744Shelbig 43331744Shelbig sprintf(s, "%s %d", month.name, y); 43431744Shelbig printf("%s\n", center(t, s, mw)); 43531744Shelbig 43631744Shelbig if (jd_flag) 43731744Shelbig printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0], 43831744Shelbig wds.names[1], wds.names[2], wds.names[3], 43931744Shelbig wds.names[4], wds.names[5]); 44031744Shelbig else 44131744Shelbig printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0], 44231744Shelbig wds.names[1], wds.names[2], wds.names[3], 44331744Shelbig wds.names[4], wds.names[5]); 44431744Shelbig 44531744Shelbig for (i = 0; i != 6; i++) 44631744Shelbig printf("%s\n", month.lines[i]+1); 44731744Shelbig} 44831744Shelbig 44931744Shelbigvoid 45031744Shelbigprintyear(int y, int jd_flag) 45131744Shelbig{ 45231744Shelbig struct monthlines year[12]; 45331744Shelbig struct weekdays wds; 45431744Shelbig char s[80], t[80]; 45531744Shelbig int i, j; 45631744Shelbig int mpl; 45731744Shelbig int mw; 45831744Shelbig 45931744Shelbig for (i = 0; i != 12; i++) 46031744Shelbig mkmonth(y, i, jd_flag, year + i); 46131744Shelbig mkweekdays(&wds); 46231744Shelbig mpl = jd_flag ? 3 : 4; 46331744Shelbig mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH; 46431744Shelbig 46531744Shelbig sprintf(s, "%d", y); 46631744Shelbig printf("%s\n", center(t, s, mpl * mw)); 46731744Shelbig 46831744Shelbig for (j = 0; j != 12; j += mpl) { 46931744Shelbig printf(" %-*s%-*s", 47031744Shelbig mw, year[j].name, 47131744Shelbig mw, year[j + 1].name); 47231744Shelbig if (mpl == 3) 47331744Shelbig printf("%s\n", year[j + 2].name); 47431744Shelbig else 47531744Shelbig printf("%-*s%s\n", 47631744Shelbig mw, year[j + 2].name, 47731744Shelbig year[j + 3].name); 47831744Shelbig for (i = 0; i != 7; i++) { 47931744Shelbig printf("%.2s%-*s%-*s", 48031744Shelbig wds.names[i], 48131744Shelbig mw, year[j].lines[i], 48231744Shelbig mw, year[j + 1].lines[i]); 48331744Shelbig if (mpl == 3) 48431744Shelbig printf("%s\n", year[j + 2].lines[i]); 48531744Shelbig else 48631744Shelbig printf("%-*s%s\n", 48731744Shelbig mw, year[j + 2].lines[i], 48831744Shelbig year[j + 3].lines[i]); 48931744Shelbig } 49048566Sbillf if (flag_weeks) { 49131744Shelbig if (mpl == 3) 49231744Shelbig printf(" %-*s%-*s%-s\n", 49331744Shelbig mw, year[j].weeks, 49431744Shelbig mw, year[j + 1].weeks, 49531744Shelbig year[j + 2].weeks); 49631744Shelbig else 49731744Shelbig printf(" %-*s%-*s%-*s%-s\n", 49831744Shelbig mw, year[j].weeks, 49931744Shelbig mw, year[j + 1].weeks, 50031744Shelbig mw, year[j + 2].weeks, 50131744Shelbig year[j + 3].weeks); 50248566Sbillf } 50331744Shelbig } 50431744Shelbig} 50531744Shelbig 50631744Shelbigvoid 50731744Shelbigprintyearb(int y, int jd_flag) 50831744Shelbig{ 50931744Shelbig struct monthlines year[12]; 51031744Shelbig struct weekdays wds; 51131744Shelbig char s[80], t[80]; 51231744Shelbig int i, j; 51331744Shelbig int mpl; 51431744Shelbig int mw; 51531744Shelbig 51631744Shelbig for (i = 0; i != 12; i++) 51731744Shelbig mkmonthb(y, i, jd_flag, year + i); 51831744Shelbig mkweekdays(&wds); 51931744Shelbig mpl = jd_flag ? 2 : 3; 52031744Shelbig mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B; 52131744Shelbig 52231744Shelbig sprintf(s, "%d", y); 52331744Shelbig printf("%s\n\n", center(t, s, mw * mpl + mpl)); 52431744Shelbig 52531744Shelbig for (j = 0; j != 12; j += mpl) { 52631744Shelbig printf("%-*s ", mw, center(s, year[j].name, mw)); 52731744Shelbig if (mpl == 2) 52831744Shelbig printf("%s\n", center(s, year[j + 1].name, mw)); 52931744Shelbig else 53031744Shelbig printf("%-*s %s\n", mw, 53131744Shelbig center(s, year[j + 1].name, mw), 53231744Shelbig center(t, year[j + 2].name, mw)); 53331744Shelbig 53431744Shelbig if (mpl == 2) 53531744Shelbig printf(" %s %s %s %s %s %s %s " 53631744Shelbig " %s %s %s %s %s %s %.2s\n", 53731744Shelbig wds.names[6], wds.names[0], wds.names[1], 53831744Shelbig wds.names[2], wds.names[3], wds.names[4], 53931744Shelbig wds.names[5], 54031744Shelbig wds.names[6], wds.names[0], wds.names[1], 54131744Shelbig wds.names[2], wds.names[3], wds.names[4], 54231744Shelbig wds.names[5]); 54331744Shelbig else 54431744Shelbig printf("%s%s%s%s%s%s%s " 54531744Shelbig "%s%s%s%s%s%s%s " 54631744Shelbig "%s%s%s%s%s%s%.2s\n", 54731744Shelbig wds.names[6], wds.names[0], wds.names[1], 54831744Shelbig wds.names[2], wds.names[3], wds.names[4], 54931744Shelbig wds.names[5], 55031744Shelbig wds.names[6], wds.names[0], wds.names[1], 55131744Shelbig wds.names[2], wds.names[3], wds.names[4], 55231744Shelbig wds.names[5], 55331744Shelbig wds.names[6], wds.names[0], wds.names[1], 55431744Shelbig wds.names[2], wds.names[3], wds.names[4], 55531744Shelbig wds.names[5]); 55631744Shelbig for (i = 0; i != 6; i++) { 55731744Shelbig if (mpl == 2) 55831744Shelbig printf("%-*s %s\n", 55931744Shelbig mw, year[j].lines[i]+1, 56031744Shelbig year[j + 1].lines[i]+1); 56131744Shelbig else 56231744Shelbig printf("%-*s %-*s %s\n", 56331744Shelbig mw, year[j].lines[i]+1, 56431744Shelbig mw, year[j + 1].lines[i]+1, 56531744Shelbig year[j + 2].lines[i]+1); 56631744Shelbig 56731744Shelbig } 56831744Shelbig } 56931744Shelbig} 57031744Shelbig 57131744Shelbigvoid 57231744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines) 57331744Shelbig{ 57431744Shelbig 57531744Shelbig struct tm tm; /* for strftime printing local names of 57631744Shelbig * months */ 57731744Shelbig date dt; /* handy date */ 57831744Shelbig int dw; /* width of numbers */ 57931744Shelbig int first; /* first day of month */ 58031744Shelbig int firstm; /* first day of first week of month */ 58131744Shelbig int i, j, k; /* just indices */ 58231744Shelbig int last; /* the first day of next month */ 58331744Shelbig int jan1 = 0; /* the first day of this year */ 58431744Shelbig char *ds; /* pointer to day strings (daystr or 58531744Shelbig * jdaystr) */ 58631744Shelbig 58731744Shelbig /* Set name of month. */ 58831744Shelbig memset(&tm, 0, sizeof(tm)); 58931744Shelbig tm.tm_mon = m; 59051201Sache strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 59174612Sache mlines->name[0] = toupper((unsigned char)mlines->name[0]); 59231744Shelbig 59331744Shelbig /* 59431744Shelbig * Set first and last to the day number of the first day of this 59531744Shelbig * month and the first day of next month respectively. Set jan1 to 59632282Shelbig * the day number of the first day of this year. 59731744Shelbig */ 59832282Shelbig first = firstday(y, m + 1); 59932282Shelbig if (m == 11) 60032282Shelbig last = firstday(y + 1, 1); 60132282Shelbig else 60232282Shelbig last = firstday(y, m + 2); 60331744Shelbig 60432282Shelbig if (jd_flag) 60532525Shelbig jan1 = firstday(y, 1); 60631744Shelbig 60731744Shelbig /* 60831744Shelbig * Set firstm to the day number of monday of the first week of 60931744Shelbig * this month. (This might be in the last month) 61031744Shelbig */ 61131744Shelbig firstm = first - weekday(first); 61231744Shelbig 61331744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 61431744Shelbig if (jd_flag) { 61531744Shelbig ds = jdaystr; 61631744Shelbig dw = 4; 61731744Shelbig } else { 61831744Shelbig ds = daystr; 61931744Shelbig dw = 3; 62031744Shelbig } 62131744Shelbig 62231744Shelbig /* 62331744Shelbig * Fill the lines with day of month or day of year (julian day) 62431744Shelbig * line index: i, each line is one weekday. column index: j, each 62531744Shelbig * column is one day number. print column index: k. 62631744Shelbig */ 62731744Shelbig for (i = 0; i != 7; i++) { 62831744Shelbig for (j = firstm + i, k = 0; j < last; j += 7, k += dw) 62931744Shelbig if (j >= first) { 63031744Shelbig if (jd_flag) 63131744Shelbig dt.d = j - jan1 + 1; 63231744Shelbig else 63331744Shelbig sdate(j, &dt); 63431744Shelbig memcpy(mlines->lines[i] + k, 63531744Shelbig ds + dt.d * dw, dw); 63631744Shelbig } else 63731744Shelbig memcpy(mlines->lines[i] + k, " ", dw); 63831744Shelbig mlines->lines[i][k] = '\0'; 63931744Shelbig 64031744Shelbig } 64131744Shelbig 64231744Shelbig /* fill the weeknumbers */ 64331744Shelbig if (flag_weeks) { 64431744Shelbig for (j = firstm, k = 0; j < last; k += dw, j += 7) 64531744Shelbig if (j <= nswitch) 64631744Shelbig memset(mlines->weeks + k, ' ', dw); 64731744Shelbig else 64831744Shelbig memcpy(mlines->weeks + k, 64931744Shelbig ds + week(j, &i)*dw, dw); 65031744Shelbig mlines->weeks[k] = '\0'; 65131744Shelbig } 65231744Shelbig} 65331744Shelbig 65431744Shelbigvoid 65531744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines) 65631744Shelbig{ 65731744Shelbig 65831744Shelbig struct tm tm; /* for strftime printing local names of 65931744Shelbig * months */ 66031744Shelbig date dt; /* handy date */ 66131744Shelbig int dw; /* width of numbers */ 66231744Shelbig int first; /* first day of month */ 66331744Shelbig int firsts; /* sunday of first week of month */ 66431744Shelbig int i, j, k; /* just indices */ 66531744Shelbig int jan1 = 0; /* the first day of this year */ 66631744Shelbig int last; /* the first day of next month */ 66731744Shelbig char *ds; /* pointer to day strings (daystr or 66831744Shelbig * jdaystr) */ 66931744Shelbig 67031744Shelbig /* Set ds (daystring) and dw (daywidth) according to the jd_flag */ 67131744Shelbig if (jd_flag) { 67231744Shelbig ds = jdaystr; 67331744Shelbig dw = 4; 67431744Shelbig } else { 67531744Shelbig ds = daystr; 67631744Shelbig dw = 3; 67731744Shelbig } 67831744Shelbig 67931744Shelbig /* Set name of month centered */ 68031744Shelbig memset(&tm, 0, sizeof(tm)); 68131744Shelbig tm.tm_mon = m; 68251201Sache strftime(mlines->name, sizeof(mlines->name), "%OB", &tm); 68374612Sache mlines->name[0] = toupper((unsigned char)mlines->name[0]); 68431744Shelbig 68531744Shelbig /* 68631744Shelbig * Set first and last to the day number of the first day of this 68731744Shelbig * month and the first day of next month respectively. Set jan1 to 68831744Shelbig * the day number of Jan 1st of this year. 68931744Shelbig */ 69031744Shelbig dt.y = y; 69131744Shelbig dt.m = m + 1; 69231744Shelbig dt.d = 1; 69331744Shelbig first = sndaysb(&dt); 69431744Shelbig if (m == 11) { 69531744Shelbig dt.y = y + 1; 69631744Shelbig dt.m = 1; 69731744Shelbig dt.d = 1; 69831744Shelbig } else { 69931744Shelbig dt.y = y; 70031744Shelbig dt.m = m + 2; 70131744Shelbig dt.d = 1; 70231744Shelbig } 70331744Shelbig last = sndaysb(&dt); 70431744Shelbig 70531744Shelbig if (jd_flag) { 70631744Shelbig dt.y = y; 70731744Shelbig dt.m = 1; 70831744Shelbig dt.d = 1; 70931744Shelbig jan1 = sndaysb(&dt); 71031744Shelbig } 71131744Shelbig 71231744Shelbig /* 71331744Shelbig * Set firsts to the day number of sunday of the first week of 71431744Shelbig * this month. (This might be in the last month) 71531744Shelbig */ 71631744Shelbig firsts = first - (weekday(first)+1) % 7; 71731744Shelbig 71831744Shelbig /* 71931744Shelbig * Fill the lines with day of month or day of year (Julian day) 72031744Shelbig * line index: i, each line is one week. column index: j, each 72131744Shelbig * column is one day number. print column index: k. 72231744Shelbig */ 72331744Shelbig for (i = 0; i != 6; i++) { 72431744Shelbig for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7; 72531744Shelbig j++, k += dw) 72631744Shelbig if (j >= first) { 72731744Shelbig if (jd_flag) 72831744Shelbig dt.d = j - jan1 + 1; 72931744Shelbig else 73031744Shelbig sdateb(j, &dt); 73131744Shelbig memcpy(mlines->lines[i] + k, 73231744Shelbig ds + dt.d * dw, dw); 73331744Shelbig } else 73431744Shelbig memcpy(mlines->lines[i] + k, " ", dw); 73531744Shelbig if (k == 0) 73631744Shelbig mlines->lines[i][1] = '\0'; 73731744Shelbig else 73831744Shelbig mlines->lines[i][k] = '\0'; 73931744Shelbig } 74031744Shelbig} 74131744Shelbig 74231744Shelbig/* Put the local names of weekdays into the wds */ 74332139Shelbigvoid 74432139Shelbigmkweekdays(struct weekdays *wds) 74531744Shelbig{ 74674618Sache int i, len; 74731744Shelbig struct tm tm; 74874618Sache char buf[20]; 74931744Shelbig 75031744Shelbig memset(&tm, 0, sizeof(tm)); 75131744Shelbig 75231744Shelbig for (i = 0; i != 7; i++) { 75331744Shelbig tm.tm_wday = (i+1) % 7; 75474618Sache strftime(buf, sizeof(buf), "%a", &tm); 75574618Sache len = strlen(buf); 75674618Sache if (len > 2) 75774618Sache len = 2; 75874618Sache strcpy(wds->names[i], " "); 75974618Sache strncpy(wds->names[i] + 2 - len, buf, len); 76031744Shelbig } 76131744Shelbig} 76231744Shelbig 76331744Shelbig/* 76445064Shelbig * Compute the day number of the first 76545064Shelbig * existing date after the first day in month. 76645064Shelbig * (the first day in month and even the month might not exist!) 76732282Shelbig */ 76832282Shelbigint 76932282Shelbigfirstday(int y, int m) 77032282Shelbig{ 77132282Shelbig date dt; 77232282Shelbig int nd; 77332282Shelbig 77432282Shelbig dt.y = y; 77532282Shelbig dt.m = m; 77632282Shelbig dt.d = 1; 77745064Shelbig nd = sndays(&dt); 77845064Shelbig for (;;) { 77945064Shelbig sdate(nd, &dt); 78045064Shelbig if ((dt.m >= m && dt.y == y) || dt.y > y) 78145064Shelbig return (nd); 78245064Shelbig else 78345064Shelbig nd++; 78445064Shelbig } 78545064Shelbig /* NEVER REACHED */ 78632282Shelbig} 78732282Shelbig 78832282Shelbig/* 78931744Shelbig * Compute the number of days from date, obey the local switch from 79031744Shelbig * Julian to Gregorian if specified by the user. 79131744Shelbig */ 79231744Shelbigint 79331744Shelbigsndays(struct date *d) 79431744Shelbig{ 79531744Shelbig 79631744Shelbig if (nswitch != 0) 79731744Shelbig if (nswitch < ndaysj(d)) 79831744Shelbig return (ndaysg(d)); 79931744Shelbig else 80031744Shelbig return (ndaysj(d)); 80131744Shelbig else 80231744Shelbig return ndaysg(d); 80331744Shelbig} 80431744Shelbig 80531744Shelbig/* 80631744Shelbig * Compute the number of days from date, obey the switch from 80731744Shelbig * Julian to Gregorian as used by UK and her colonies. 80831744Shelbig */ 80931744Shelbigint 81031744Shelbigsndaysb(struct date *d) 81131744Shelbig{ 81231744Shelbig 81331744Shelbig if (nswitchb < ndaysj(d)) 81431744Shelbig return (ndaysg(d)); 81531744Shelbig else 81631744Shelbig return (ndaysj(d)); 81731744Shelbig} 81831744Shelbig 81931744Shelbig/* Inverse of sndays */ 82031744Shelbigstruct date * 82131744Shelbigsdate(int nd, struct date *d) 82231744Shelbig{ 82331744Shelbig 82431744Shelbig if (nswitch < nd) 82531744Shelbig return (gdate(nd, d)); 82631744Shelbig else 82731744Shelbig return (jdate(nd, d)); 82831744Shelbig} 82931744Shelbig 83031744Shelbig/* Inverse of sndaysb */ 83131744Shelbigstruct date * 83231744Shelbigsdateb(int nd, struct date *d) 83331744Shelbig{ 83431744Shelbig 83531744Shelbig if (nswitchb < nd) 83631744Shelbig return (gdate(nd, d)); 83731744Shelbig else 83831744Shelbig return (jdate(nd, d)); 83931744Shelbig} 84031744Shelbig 84131744Shelbig/* Center string t in string s of length w by putting enough leading blanks */ 84231744Shelbigchar * 84331744Shelbigcenter(char *s, char *t, int w) 84431744Shelbig{ 84531744Shelbig char blanks[80]; 84631744Shelbig 84731744Shelbig memset(blanks, ' ', sizeof(blanks)); 84831744Shelbig sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t); 84931744Shelbig return (s); 85031744Shelbig} 851