Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / cal / cal.c
blob888afa0572128a7c3cabea6da0428329124dd743
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <langinfo.h>
34 #include <locale.h>
35 #include <nl_types.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <time.h>
40 static int number(char *);
41 static int jan1(const int);
42 static void badmonth(void);
43 static void badyear(void);
44 static void usage(void);
45 static void cal(const int, const int, char *, const int);
46 static void load_months(void);
47 static void pstr(char *, const int);
49 #define DAYW " S M Tu W Th F S"
50 #define TITLE " %s %u\n"
51 #define YEAR "\n\n\n\t\t\t\t%u\n\n"
52 #define MONTH "\t%4.3s\t\t\t%.3s\t\t%10.3s\n"
54 static char *months[] = {
55 "January", "February", "March", "April",
56 "May", "June", "July", "August",
57 "September", "October", "November", "December",
60 static char *short_months[] = {
61 "Jan", "Feb", "Mar", "Apr",
62 "May", "Jun", "Jul", "Aug",
63 "Sep", "Oct", "Nov", "Dec",
66 static char mon[] = {
68 31, 29, 31, 30,
69 31, 30, 31, 31,
70 30, 31, 30, 31,
73 static char *myname;
74 static char string[432];
75 static struct tm *thetime;
76 static time_t timbuf;
78 int
79 main(int argc, char *argv[])
81 int y, i, j;
82 int m;
83 char *time_locale;
84 char *ldayw;
86 myname = argv[0];
88 (void) setlocale(LC_ALL, "");
89 #if !defined(TEXT_DOMAIN)
90 #define TEXT_DOMAIN "SYS_TEST"
91 #endif
92 (void) textdomain(TEXT_DOMAIN);
95 while (getopt(argc, argv, "") != EOF)
96 usage();
98 argc -= optind;
99 argv = &argv[optind];
101 time_locale = setlocale(LC_TIME, NULL);
102 if ((time_locale[0] != 'C') || (time_locale[1] != '\0'))
103 load_months();
106 * TRANSLATION_NOTE
107 * This message is to be used for displaying
108 * the names of the seven days, from Sunday to Saturday.
109 * The length of the name of each one should be two or less.
111 ldayw = dcgettext(NULL, DAYW, LC_TIME);
113 switch (argc) {
114 case 0:
115 timbuf = time(&timbuf);
116 thetime = localtime(&timbuf);
117 m = thetime->tm_mon + 1;
118 y = thetime->tm_year + 1900;
119 break;
120 case 1:
121 goto xlong;
122 case 2:
123 m = number(argv[0]);
124 y = number(argv[1]);
125 break;
126 default:
127 usage();
131 * print out just month
134 if (m < 1 || m > 12)
135 badmonth();
136 if (y < 1 || y > 9999)
137 badyear();
139 * TRANSLATION_NOTE
140 * This message is to be used for displaying
141 * specified month and year.
143 (void) printf(dcgettext(NULL, TITLE, LC_TIME), months[m-1], y);
144 (void) printf("%s\n", ldayw);
145 cal(m, y, string, 24);
146 for (i = 0; i < 6*24; i += 24)
147 pstr(string+i, 24);
148 return (0);
151 * print out complete year
154 xlong:
155 y = number(argv[0]);
156 if (y < 1 || y > 9999)
157 badyear();
159 * TRANSLATION_NOTE
160 * This message is to be used for displaying
161 * specified year.
163 (void) printf(dcgettext(NULL, YEAR, LC_TIME), y);
164 for (i = 0; i < 12; i += 3) {
165 for (j = 0; j < 6*72; j++)
166 string[j] = '\0';
168 * TRANSLATION_NOTE
169 * This message is to be used for displaying
170 * names of three months per a line and should be
171 * correctly translated according to the display width
172 * of the names of months.
174 (void) printf(
175 dcgettext(NULL, MONTH, LC_TIME),
176 short_months[i], short_months[i+1], short_months[i+2]);
177 (void) printf("%s %s %s\n", ldayw, ldayw, ldayw);
178 cal(i+1, y, string, 72);
179 cal(i+2, y, string+23, 72);
180 cal(i+3, y, string+46, 72);
181 for (j = 0; j < 6*72; j += 72)
182 pstr(string+j, 72);
184 (void) printf("\n\n\n");
185 return (0);
188 static int
189 number(char *str)
191 int n, c;
192 char *s;
194 n = 0;
195 s = str;
196 /*LINTED*/
197 while (c = *s++) {
198 if (c < '0' || c > '9')
199 return (0);
200 n = n*10 + c-'0';
202 return (n);
205 static void
206 pstr(char *str, const int n)
208 int i;
209 char *s;
211 s = str;
212 i = n;
213 while (i--)
214 if (*s++ == '\0')
215 s[-1] = ' ';
216 i = n+1;
217 while (i--)
218 if (*--s != ' ')
219 break;
220 s[1] = '\0';
221 (void) printf("%s\n", str);
224 static void
225 cal(const int m, const int y, char *p, const int w)
227 int d, i;
228 char *s;
230 s = (char *)p;
231 d = jan1(y);
232 mon[2] = 29;
233 mon[9] = 30;
235 switch ((jan1(y+1)+7-d)%7) {
238 * non-leap year
240 case 1:
241 mon[2] = 28;
242 break;
245 * 1752
247 default:
248 mon[9] = 19;
249 break;
252 * leap year
254 case 2:
257 for (i = 1; i < m; i++)
258 d += mon[i];
259 d %= 7;
260 s += 3*d;
261 for (i = 1; i <= mon[m]; i++) {
262 if (i == 3 && mon[m] == 19) {
263 i += 11;
264 mon[m] += 11;
266 if (i > 9)
267 *s = i/10+'0';
268 s++;
269 *s++ = i%10+'0';
270 s++;
271 if (++d == 7) {
272 d = 0;
273 s = p+w;
274 p = s;
280 * return day of the week
281 * of jan 1 of given year
284 static int
285 jan1(const int yr)
287 int y, d;
290 * normal gregorian calendar
291 * one extra day per four years
294 y = yr;
295 d = 4+y+(y+3)/4;
298 * julian calendar
299 * regular gregorian
300 * less three days per 400
303 if (y > 1800) {
304 d -= (y-1701)/100;
305 d += (y-1601)/400;
309 * great calendar changeover instant
312 if (y > 1752)
313 d += 3;
315 return (d%7);
318 static void
319 load_months(void)
321 int month;
323 for (month = MON_1; month <= MON_12; month++)
324 months[month - MON_1] = nl_langinfo(month);
325 for (month = ABMON_1; month <= ABMON_12; month++)
326 short_months[month - ABMON_1] = nl_langinfo(month);
329 static void
330 badmonth()
332 (void) fprintf(stderr, gettext("%s: bad month\n"), myname);
333 usage();
336 static void
337 badyear()
339 (void) fprintf(stderr, gettext("%s: bad year\n"), myname);
340 usage();
343 static void
344 usage(void)
346 (void) fprintf(stderr, gettext("usage: %s [ [month] year ]\n"), myname);
347 exit(1);