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