1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #include <langinfo.h> 32 #include <locale.h> 33 #include <nl_types.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <time.h> 37 38 static int number(char *); 39 static int jan1(const int); 40 static void badmonth(void); 41 static void badyear(void); 42 static void usage(void); 43 static void cal(const int, const int, char *, const int); 44 static void load_months(void); 45 static void pstr(char *, const int); 46 47 #define DAYW " S M Tu W Th F S" 48 #define TITLE " %s %u\n" 49 #define YEAR "\n\n\n\t\t\t\t%u\n\n" 50 #define MONTH "\t%4.3s\t\t\t%.3s\t\t%10.3s\n" 51 52 static char *months[] = { 53 "January", "February", "March", "April", 54 "May", "June", "July", "August", 55 "September", "October", "November", "December", 56 }; 57 58 static char *short_months[] = { 59 "Jan", "Feb", "Mar", "Apr", 60 "May", "Jun", "Jul", "Aug", 61 "Sep", "Oct", "Nov", "Dec", 62 }; 63 64 static char mon[] = { 65 0, 66 31, 29, 31, 30, 67 31, 30, 31, 31, 68 30, 31, 30, 31, 69 }; 70 71 static char *myname; 72 static char string[432]; 73 static struct tm *thetime; 74 static time_t timbuf; 75 76 int 77 main(int argc, char *argv[]) 78 { 79 int y, i, j; 80 int m; 81 char *time_locale; 82 char *ldayw; 83 84 myname = argv[0]; 85 86 (void) setlocale(LC_ALL, ""); 87 #if !defined(TEXT_DOMAIN) 88 #define TEXT_DOMAIN "SYS_TEST" 89 #endif 90 (void) textdomain(TEXT_DOMAIN); 91 92 93 while (getopt(argc, argv, "") != EOF) 94 usage(); 95 96 argc -= optind; 97 argv = &argv[optind]; 98 99 time_locale = setlocale(LC_TIME, NULL); 100 if ((time_locale[0] != 'C') || (time_locale[1] != '\0')) 101 load_months(); 102 103 /* 104 * TRANSLATION_NOTE 105 * This message is to be used for displaying 106 * the names of the seven days, from Sunday to Saturday. 107 * The length of the name of each one should be two or less. 108 */ 109 ldayw = dcgettext(NULL, DAYW, LC_TIME); 110 111 switch (argc) { 112 case 0: 113 timbuf = time(&timbuf); 114 thetime = localtime(&timbuf); 115 m = thetime->tm_mon + 1; 116 y = thetime->tm_year + 1900; 117 break; 118 case 1: 119 goto xlong; 120 case 2: 121 m = number(argv[0]); 122 y = number(argv[1]); 123 break; 124 default: 125 usage(); 126 } 127 128 /* 129 * print out just month 130 */ 131 132 if (m < 1 || m > 12) 133 badmonth(); 134 if (y < 1 || y > 9999) 135 badyear(); 136 /* 137 * TRANSLATION_NOTE 138 * This message is to be used for displaying 139 * specified month and year. 140 */ 141 (void) printf(dcgettext(NULL, TITLE, LC_TIME), months[m-1], y); 142 (void) printf("%s\n", ldayw); 143 cal(m, y, string, 24); 144 for (i = 0; i < 6*24; i += 24) 145 pstr(string+i, 24); 146 return (0); 147 148 /* 149 * print out complete year 150 */ 151 152 xlong: 153 y = number(argv[0]); 154 if (y < 1 || y > 9999) 155 badyear(); 156 /* 157 * TRANSLATION_NOTE 158 * This message is to be used for displaying 159 * specified year. 160 */ 161 (void) printf(dcgettext(NULL, YEAR, LC_TIME), y); 162 for (i = 0; i < 12; i += 3) { 163 for (j = 0; j < 6*72; j++) 164 string[j] = '\0'; 165 /* 166 * TRANSLATION_NOTE 167 * This message is to be used for displaying 168 * names of three months per a line and should be 169 * correctly translated according to the display width 170 * of the names of months. 171 */ 172 (void) printf( 173 dcgettext(NULL, MONTH, LC_TIME), 174 short_months[i], short_months[i+1], short_months[i+2]); 175 (void) printf("%s %s %s\n", ldayw, ldayw, ldayw); 176 cal(i+1, y, string, 72); 177 cal(i+2, y, string+23, 72); 178 cal(i+3, y, string+46, 72); 179 for (j = 0; j < 6*72; j += 72) 180 pstr(string+j, 72); 181 } 182 (void) printf("\n\n\n"); 183 return (0); 184 } 185 186 static int 187 number(char *str) 188 { 189 int n, c; 190 char *s; 191 192 n = 0; 193 s = str; 194 /*LINTED*/ 195 while (c = *s++) { 196 if (c < '0' || c > '9') 197 return (0); 198 n = n*10 + c-'0'; 199 } 200 return (n); 201 } 202 203 static void 204 pstr(char *str, const int n) 205 { 206 int i; 207 char *s; 208 209 s = str; 210 i = n; 211 while (i--) 212 if (*s++ == '\0') 213 s[-1] = ' '; 214 i = n+1; 215 while (i--) 216 if (*--s != ' ') 217 break; 218 s[1] = '\0'; 219 (void) printf("%s\n", str); 220 } 221 222 static void 223 cal(const int m, const int y, char *p, const int w) 224 { 225 int d, i; 226 char *s; 227 228 s = (char *)p; 229 d = jan1(y); 230 mon[2] = 29; 231 mon[9] = 30; 232 233 switch ((jan1(y+1)+7-d)%7) { 234 235 /* 236 * non-leap year 237 */ 238 case 1: 239 mon[2] = 28; 240 break; 241 242 /* 243 * 1752 244 */ 245 default: 246 mon[9] = 19; 247 break; 248 249 /* 250 * leap year 251 */ 252 case 2: 253 ; 254 } 255 for (i = 1; i < m; i++) 256 d += mon[i]; 257 d %= 7; 258 s += 3*d; 259 for (i = 1; i <= mon[m]; i++) { 260 if (i == 3 && mon[m] == 19) { 261 i += 11; 262 mon[m] += 11; 263 } 264 if (i > 9) 265 *s = i/10+'0'; 266 s++; 267 *s++ = i%10+'0'; 268 s++; 269 if (++d == 7) { 270 d = 0; 271 s = p+w; 272 p = s; 273 } 274 } 275 } 276 277 /* 278 * return day of the week 279 * of jan 1 of given year 280 */ 281 282 static int 283 jan1(const int yr) 284 { 285 int y, d; 286 287 /* 288 * normal gregorian calendar 289 * one extra day per four years 290 */ 291 292 y = yr; 293 d = 4+y+(y+3)/4; 294 295 /* 296 * julian calendar 297 * regular gregorian 298 * less three days per 400 299 */ 300 301 if (y > 1800) { 302 d -= (y-1701)/100; 303 d += (y-1601)/400; 304 } 305 306 /* 307 * great calendar changeover instant 308 */ 309 310 if (y > 1752) 311 d += 3; 312 313 return (d%7); 314 } 315 316 static void 317 load_months(void) 318 { 319 int month; 320 321 for (month = MON_1; month <= MON_12; month++) 322 months[month - MON_1] = nl_langinfo(month); 323 for (month = ABMON_1; month <= ABMON_12; month++) 324 short_months[month - ABMON_1] = nl_langinfo(month); 325 } 326 327 static void 328 badmonth() 329 { 330 (void) fprintf(stderr, gettext("%s: bad month\n"), myname); 331 usage(); 332 } 333 334 static void 335 badyear() 336 { 337 (void) fprintf(stderr, gettext("%s: bad year\n"), myname); 338 usage(); 339 } 340 341 static void 342 usage(void) 343 { 344 (void) fprintf(stderr, gettext("usage: %s [ [month] year ]\n"), myname); 345 exit(1); 346 } 347