xref: /titanic_51/usr/src/cmd/cal/cal.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
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>
39 
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);
48 
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"
53 
54 static char *months[] = {
55 	"January", "February", "March", "April",
56 	"May", "June", "July", "August",
57 	"September", "October", "November", "December",
58 };
59 
60 static char *short_months[] = {
61 	"Jan", "Feb", "Mar", "Apr",
62 	"May", "Jun", "Jul", "Aug",
63 	"Sep", "Oct", "Nov", "Dec",
64 };
65 
66 static char mon[] = {
67 	0,
68 	31, 29, 31, 30,
69 	31, 30, 31, 31,
70 	30, 31, 30, 31,
71 };
72 
73 static char *myname;
74 static char string[432];
75 static struct tm *thetime;
76 static time_t timbuf;
77 
78 int
79 main(int argc, char *argv[])
80 {
81 	int y, i, j;
82 	int m;
83 	char *time_locale;
84 	char	*ldayw;
85 
86 	myname = argv[0];
87 
88 	(void) setlocale(LC_ALL, "");
89 #if !defined(TEXT_DOMAIN)
90 #define	TEXT_DOMAIN	"SYS_TEST"
91 #endif
92 	(void) textdomain(TEXT_DOMAIN);
93 
94 
95 	while (getopt(argc, argv, "") != EOF)
96 		usage();
97 
98 	argc -= optind;
99 	argv  = &argv[optind];
100 
101 	time_locale = setlocale(LC_TIME, NULL);
102 	if ((time_locale[0] != 'C') || (time_locale[1] != '\0'))
103 		load_months();
104 
105 	/*
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.
110 	 */
111 	ldayw = dcgettext(NULL, DAYW, LC_TIME);
112 
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();
128 	}
129 
130 /*
131  *	print out just month
132  */
133 
134 	if (m < 1 || m > 12)
135 		badmonth();
136 	if (y < 1 || y > 9999)
137 		badyear();
138 	/*
139 	 * TRANSLATION_NOTE
140 	 * This message is to be used for displaying
141 	 * specified month and year.
142 	 */
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);
149 
150 /*
151  *	print out complete year
152  */
153 
154 xlong:
155 	y = number(argv[0]);
156 	if (y < 1 || y > 9999)
157 		badyear();
158 	/*
159 	 * TRANSLATION_NOTE
160 	 * This message is to be used for displaying
161 	 * specified year.
162 	 */
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';
167 		/*
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.
173 		 */
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);
183 	}
184 	(void) printf("\n\n\n");
185 	return (0);
186 }
187 
188 static int
189 number(char *str)
190 {
191 	int n, c;
192 	char *s;
193 
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';
201 	}
202 	return (n);
203 }
204 
205 static void
206 pstr(char *str, const int n)
207 {
208 	int i;
209 	char *s;
210 
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);
222 }
223 
224 static void
225 cal(const int m, const int y, char *p, const int w)
226 {
227 	int d, i;
228 	char *s;
229 
230 	s = (char *)p;
231 	d = jan1(y);
232 	mon[2] = 29;
233 	mon[9] = 30;
234 
235 	switch ((jan1(y+1)+7-d)%7) {
236 
237 	/*
238 	 *	non-leap year
239 	 */
240 	case 1:
241 		mon[2] = 28;
242 		break;
243 
244 	/*
245 	 *	1752
246 	 */
247 	default:
248 		mon[9] = 19;
249 		break;
250 
251 	/*
252 	 *	leap year
253 	 */
254 	case 2:
255 		;
256 	}
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;
265 		}
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;
275 		}
276 	}
277 }
278 
279 /*
280  *	return day of the week
281  *	of jan 1 of given year
282  */
283 
284 static int
285 jan1(const int yr)
286 {
287 	int y, d;
288 
289 /*
290  *	normal gregorian calendar
291  *	one extra day per four years
292  */
293 
294 	y = yr;
295 	d = 4+y+(y+3)/4;
296 
297 /*
298  *	julian calendar
299  *	regular gregorian
300  *	less three days per 400
301  */
302 
303 	if (y > 1800) {
304 		d -= (y-1701)/100;
305 		d += (y-1601)/400;
306 	}
307 
308 /*
309  *	great calendar changeover instant
310  */
311 
312 	if (y > 1752)
313 		d += 3;
314 
315 	return (d%7);
316 }
317 
318 static void
319 load_months(void)
320 {
321 	int month;
322 
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);
327 }
328 
329 static void
330 badmonth()
331 {
332 	(void) fprintf(stderr, gettext("%s: bad month\n"), myname);
333 	usage();
334 }
335 
336 static void
337 badyear()
338 {
339 	(void) fprintf(stderr, gettext("%s: bad year\n"), myname);
340 	usage();
341 }
342 
343 static void
344 usage(void)
345 {
346 	(void) fprintf(stderr, gettext("usage: %s [ [month] year ]\n"), myname);
347 	exit(1);
348 }
349