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 1996 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 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #if !defined(lint) && defined(SCCSIDS)
34*7c478bd9Sstevel@tonic-gate static char *sccsid = "%Z%%M% %I% %E% SMI"; /* from S5R3.1 cftime.c 1.9 */
35*7c478bd9Sstevel@tonic-gate #endif
36*7c478bd9Sstevel@tonic-gate
37*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate #include <locale.h>
40*7c478bd9Sstevel@tonic-gate #include <time.h>
41*7c478bd9Sstevel@tonic-gate #include <string.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
44*7c478bd9Sstevel@tonic-gate
45*7c478bd9Sstevel@tonic-gate static char *getstr(/*char *p, char **strp*/);
46*7c478bd9Sstevel@tonic-gate static char *itoa();
47*7c478bd9Sstevel@tonic-gate extern int stat();
48*7c478bd9Sstevel@tonic-gate extern char *getenv();
49*7c478bd9Sstevel@tonic-gate extern char *malloc();
50*7c478bd9Sstevel@tonic-gate extern int openlocale(/*char *category, int cat_id, char *locale, char *newlocale */);
51*7c478bd9Sstevel@tonic-gate extern void init_statics();
52*7c478bd9Sstevel@tonic-gate
53*7c478bd9Sstevel@tonic-gate extern struct dtconv *_dtconv;
54*7c478bd9Sstevel@tonic-gate extern char _locales[MAXLOCALE + 1][MAXLOCALENAME + 1];
55*7c478bd9Sstevel@tonic-gate extern char _my_time[];
56*7c478bd9Sstevel@tonic-gate
57*7c478bd9Sstevel@tonic-gate char *dtconv_str = NULL;
58*7c478bd9Sstevel@tonic-gate char *getlocale_time();
59*7c478bd9Sstevel@tonic-gate
60*7c478bd9Sstevel@tonic-gate int
strftime(buf,maxsize,format,tm)61*7c478bd9Sstevel@tonic-gate strftime(buf, maxsize, format, tm)
62*7c478bd9Sstevel@tonic-gate char *buf, *format;
63*7c478bd9Sstevel@tonic-gate struct tm *tm;
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate register char *cp, *p, c;
66*7c478bd9Sstevel@tonic-gate int size;
67*7c478bd9Sstevel@tonic-gate int i, temp;
68*7c478bd9Sstevel@tonic-gate register struct dtconv *dtcp;
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate (void) getlocale_time();
71*7c478bd9Sstevel@tonic-gate dtcp = localdtconv(); /* get locale's strings */
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate /* Build date string by parsing format string */
74*7c478bd9Sstevel@tonic-gate cp = buf;
75*7c478bd9Sstevel@tonic-gate size = 0;
76*7c478bd9Sstevel@tonic-gate while ((c = *format++) != '\0') {
77*7c478bd9Sstevel@tonic-gate if (c == '%') {
78*7c478bd9Sstevel@tonic-gate switch (*format++) {
79*7c478bd9Sstevel@tonic-gate
80*7c478bd9Sstevel@tonic-gate case '%': /* Percent sign */
81*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
82*7c478bd9Sstevel@tonic-gate return (0);
83*7c478bd9Sstevel@tonic-gate *cp++ = '%';
84*7c478bd9Sstevel@tonic-gate break;
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate case 'a': /* Abbreviated weekday name */
87*7c478bd9Sstevel@tonic-gate for (p = dtcp->abbrev_weekday_names[tm->tm_wday];
88*7c478bd9Sstevel@tonic-gate *p != '\0'; p++) {
89*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
90*7c478bd9Sstevel@tonic-gate return (0);
91*7c478bd9Sstevel@tonic-gate *cp++ = *p;
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate break;
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate case 'A': /* Weekday name */
96*7c478bd9Sstevel@tonic-gate for (p = dtcp->weekday_names[tm->tm_wday];
97*7c478bd9Sstevel@tonic-gate *p != '\0'; p++) {
98*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
99*7c478bd9Sstevel@tonic-gate return (0);
100*7c478bd9Sstevel@tonic-gate *cp++ = *p;
101*7c478bd9Sstevel@tonic-gate }
102*7c478bd9Sstevel@tonic-gate break;
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate case 'h':
105*7c478bd9Sstevel@tonic-gate case 'b': /* Abbreviated month name */
106*7c478bd9Sstevel@tonic-gate for (p = dtcp->abbrev_month_names[tm->tm_mon];
107*7c478bd9Sstevel@tonic-gate *p != '\0'; p++) {
108*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
109*7c478bd9Sstevel@tonic-gate return (0);
110*7c478bd9Sstevel@tonic-gate *cp++ = *p;
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate break;
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate case 'B': /* Month name */
115*7c478bd9Sstevel@tonic-gate for (p = dtcp->month_names[tm->tm_mon];
116*7c478bd9Sstevel@tonic-gate *p != '\0'; p++) {
117*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
118*7c478bd9Sstevel@tonic-gate return (0);
119*7c478bd9Sstevel@tonic-gate *cp++ = *p;
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate break;
122*7c478bd9Sstevel@tonic-gate
123*7c478bd9Sstevel@tonic-gate case 'c': /* date and time representation */
124*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size, "%x %X", tm);
125*7c478bd9Sstevel@tonic-gate if (i == 0)
126*7c478bd9Sstevel@tonic-gate return (0);
127*7c478bd9Sstevel@tonic-gate cp += i;
128*7c478bd9Sstevel@tonic-gate size += i;
129*7c478bd9Sstevel@tonic-gate break;
130*7c478bd9Sstevel@tonic-gate
131*7c478bd9Sstevel@tonic-gate case 'C': /* long date and time representation */
132*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size,
133*7c478bd9Sstevel@tonic-gate dtcp->ldate_format, tm);
134*7c478bd9Sstevel@tonic-gate if (i == 0)
135*7c478bd9Sstevel@tonic-gate return (0);
136*7c478bd9Sstevel@tonic-gate cp += i;
137*7c478bd9Sstevel@tonic-gate size += i;
138*7c478bd9Sstevel@tonic-gate break;
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate case 'd': /* Day of month, with leading zero */
141*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
142*7c478bd9Sstevel@tonic-gate return (0);
143*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_mday, cp, 2);
144*7c478bd9Sstevel@tonic-gate break;
145*7c478bd9Sstevel@tonic-gate
146*7c478bd9Sstevel@tonic-gate case 'D': /* Shorthand for %m/%d/%y */
147*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size, "%m/%d/%y",
148*7c478bd9Sstevel@tonic-gate tm);
149*7c478bd9Sstevel@tonic-gate if (i == 0)
150*7c478bd9Sstevel@tonic-gate return (0);
151*7c478bd9Sstevel@tonic-gate cp += i;
152*7c478bd9Sstevel@tonic-gate size += i;
153*7c478bd9Sstevel@tonic-gate break;
154*7c478bd9Sstevel@tonic-gate
155*7c478bd9Sstevel@tonic-gate case 'e': /* Day of month without leading zero */
156*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
157*7c478bd9Sstevel@tonic-gate return (0);
158*7c478bd9Sstevel@tonic-gate if (tm->tm_mday < 10) {
159*7c478bd9Sstevel@tonic-gate *cp++ = ' ';
160*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_mday, cp, 1);
161*7c478bd9Sstevel@tonic-gate } else
162*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_mday, cp, 2);
163*7c478bd9Sstevel@tonic-gate break;
164*7c478bd9Sstevel@tonic-gate
165*7c478bd9Sstevel@tonic-gate case 'H': /* Hour (24 hour version) */
166*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
167*7c478bd9Sstevel@tonic-gate return (0);
168*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_hour, cp, 2);
169*7c478bd9Sstevel@tonic-gate break;
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate case 'I': /* Hour (12 hour version) */
172*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
173*7c478bd9Sstevel@tonic-gate return (0);
174*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_hour > 12 ?
175*7c478bd9Sstevel@tonic-gate tm->tm_hour - 12 :
176*7c478bd9Sstevel@tonic-gate (tm->tm_hour == 0 ? 12 : tm->tm_hour),
177*7c478bd9Sstevel@tonic-gate cp, 2);
178*7c478bd9Sstevel@tonic-gate break;
179*7c478bd9Sstevel@tonic-gate
180*7c478bd9Sstevel@tonic-gate case 'j': /* Julian date */
181*7c478bd9Sstevel@tonic-gate if ((size += 3) >= maxsize)
182*7c478bd9Sstevel@tonic-gate return (0);
183*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_yday + 1, cp, 3);
184*7c478bd9Sstevel@tonic-gate break;
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate case 'k': /* Hour (24 hour version) */
187*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
188*7c478bd9Sstevel@tonic-gate return (0);
189*7c478bd9Sstevel@tonic-gate if (tm->tm_hour < 10) {
190*7c478bd9Sstevel@tonic-gate *cp++ = ' ';
191*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_hour, cp, 1);
192*7c478bd9Sstevel@tonic-gate } else
193*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_hour, cp, 2);
194*7c478bd9Sstevel@tonic-gate break;
195*7c478bd9Sstevel@tonic-gate
196*7c478bd9Sstevel@tonic-gate case 'l': /* Hour (12 hour version) */
197*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
198*7c478bd9Sstevel@tonic-gate return (0);
199*7c478bd9Sstevel@tonic-gate temp = tm->tm_hour > 12 ?
200*7c478bd9Sstevel@tonic-gate tm->tm_hour - 12 :
201*7c478bd9Sstevel@tonic-gate (tm->tm_hour == 0 ? 12 : tm->tm_hour);
202*7c478bd9Sstevel@tonic-gate if (temp < 10) {
203*7c478bd9Sstevel@tonic-gate *cp++ = ' ';
204*7c478bd9Sstevel@tonic-gate cp = itoa(temp, cp, 1);
205*7c478bd9Sstevel@tonic-gate } else
206*7c478bd9Sstevel@tonic-gate cp = itoa(temp, cp, 2);
207*7c478bd9Sstevel@tonic-gate break;
208*7c478bd9Sstevel@tonic-gate
209*7c478bd9Sstevel@tonic-gate case 'm': /* Month number */
210*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
211*7c478bd9Sstevel@tonic-gate return (0);
212*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_mon + 1, cp, 2);
213*7c478bd9Sstevel@tonic-gate break;
214*7c478bd9Sstevel@tonic-gate
215*7c478bd9Sstevel@tonic-gate case 'M': /* Minute */
216*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
217*7c478bd9Sstevel@tonic-gate return (0);
218*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_min, cp, 2);
219*7c478bd9Sstevel@tonic-gate break;
220*7c478bd9Sstevel@tonic-gate
221*7c478bd9Sstevel@tonic-gate case 'n': /* Newline */
222*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
223*7c478bd9Sstevel@tonic-gate return (0);
224*7c478bd9Sstevel@tonic-gate *cp++ = '\n';
225*7c478bd9Sstevel@tonic-gate break;
226*7c478bd9Sstevel@tonic-gate
227*7c478bd9Sstevel@tonic-gate case 'p': /* AM or PM */
228*7c478bd9Sstevel@tonic-gate if (tm->tm_hour >= 12)
229*7c478bd9Sstevel@tonic-gate p = dtcp->pm_string;
230*7c478bd9Sstevel@tonic-gate else
231*7c478bd9Sstevel@tonic-gate p = dtcp->am_string;
232*7c478bd9Sstevel@tonic-gate for (; *p != '\0'; p++) {
233*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
234*7c478bd9Sstevel@tonic-gate return (0);
235*7c478bd9Sstevel@tonic-gate *cp++ = *p;
236*7c478bd9Sstevel@tonic-gate }
237*7c478bd9Sstevel@tonic-gate break;
238*7c478bd9Sstevel@tonic-gate
239*7c478bd9Sstevel@tonic-gate case 'r': /* Shorthand for %I:%M:%S AM or PM */
240*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size, "%I:%M:%S %p",
241*7c478bd9Sstevel@tonic-gate tm);
242*7c478bd9Sstevel@tonic-gate if (i == 0)
243*7c478bd9Sstevel@tonic-gate return (0);
244*7c478bd9Sstevel@tonic-gate cp += i;
245*7c478bd9Sstevel@tonic-gate size += i;
246*7c478bd9Sstevel@tonic-gate break;
247*7c478bd9Sstevel@tonic-gate
248*7c478bd9Sstevel@tonic-gate case 'R': /* Time as %H:%M */
249*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size, "%H:%M", tm);
250*7c478bd9Sstevel@tonic-gate if (i == 0)
251*7c478bd9Sstevel@tonic-gate return (0);
252*7c478bd9Sstevel@tonic-gate cp += i;
253*7c478bd9Sstevel@tonic-gate size += i;
254*7c478bd9Sstevel@tonic-gate break;
255*7c478bd9Sstevel@tonic-gate
256*7c478bd9Sstevel@tonic-gate case 'S': /* Seconds */
257*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
258*7c478bd9Sstevel@tonic-gate return (0);
259*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_sec, cp, 2);
260*7c478bd9Sstevel@tonic-gate break;
261*7c478bd9Sstevel@tonic-gate
262*7c478bd9Sstevel@tonic-gate case 't': /* Tab */
263*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
264*7c478bd9Sstevel@tonic-gate return (0);
265*7c478bd9Sstevel@tonic-gate *cp++ = '\t';
266*7c478bd9Sstevel@tonic-gate break;
267*7c478bd9Sstevel@tonic-gate
268*7c478bd9Sstevel@tonic-gate case 'T': /* Shorthand for %H:%M:%S */
269*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size, "%H:%M:%S",
270*7c478bd9Sstevel@tonic-gate tm);
271*7c478bd9Sstevel@tonic-gate if (i == 0)
272*7c478bd9Sstevel@tonic-gate return (0);
273*7c478bd9Sstevel@tonic-gate cp += i;
274*7c478bd9Sstevel@tonic-gate size += i;
275*7c478bd9Sstevel@tonic-gate break;
276*7c478bd9Sstevel@tonic-gate
277*7c478bd9Sstevel@tonic-gate case 'U': /* Weekday number, taking Sunday as
278*7c478bd9Sstevel@tonic-gate * the first day of the week */
279*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
280*7c478bd9Sstevel@tonic-gate return (0);
281*7c478bd9Sstevel@tonic-gate temp = tm->tm_yday - tm->tm_wday;
282*7c478bd9Sstevel@tonic-gate if (temp >= -3 ) {
283*7c478bd9Sstevel@tonic-gate i = (temp + 1) / 7 + 1; /* +1 for - tm->tm_wday */
284*7c478bd9Sstevel@tonic-gate if (temp % 7 >= 4)
285*7c478bd9Sstevel@tonic-gate i++;
286*7c478bd9Sstevel@tonic-gate } else
287*7c478bd9Sstevel@tonic-gate i = 52;
288*7c478bd9Sstevel@tonic-gate cp = itoa(i, cp, 2);
289*7c478bd9Sstevel@tonic-gate break;
290*7c478bd9Sstevel@tonic-gate
291*7c478bd9Sstevel@tonic-gate case 'w': /* Weekday number */
292*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
293*7c478bd9Sstevel@tonic-gate return (0);
294*7c478bd9Sstevel@tonic-gate cp = itoa(tm->tm_wday, cp, 1);
295*7c478bd9Sstevel@tonic-gate break;
296*7c478bd9Sstevel@tonic-gate
297*7c478bd9Sstevel@tonic-gate case 'W': /* Week number of year, taking Monday as
298*7c478bd9Sstevel@tonic-gate * first day of week */
299*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
300*7c478bd9Sstevel@tonic-gate return (0);
301*7c478bd9Sstevel@tonic-gate if (tm->tm_wday == 0)
302*7c478bd9Sstevel@tonic-gate temp = tm->tm_yday - 6;
303*7c478bd9Sstevel@tonic-gate else
304*7c478bd9Sstevel@tonic-gate temp = tm->tm_yday - tm->tm_wday + 1;
305*7c478bd9Sstevel@tonic-gate if (temp >= -3) {
306*7c478bd9Sstevel@tonic-gate i = (temp + 1) / 7 + 1; /* 1 for
307*7c478bd9Sstevel@tonic-gate -tm->tm_wday */
308*7c478bd9Sstevel@tonic-gate if (temp % 7 >= 4)
309*7c478bd9Sstevel@tonic-gate i++;
310*7c478bd9Sstevel@tonic-gate } else
311*7c478bd9Sstevel@tonic-gate i = 52; /* less than 4 days in the first
312*7c478bd9Sstevel@tonic-gate week causes it to belong to
313*7c478bd9Sstevel@tonic-gate the tail of prev year */
314*7c478bd9Sstevel@tonic-gate cp = itoa(i, cp, 2);
315*7c478bd9Sstevel@tonic-gate break;
316*7c478bd9Sstevel@tonic-gate
317*7c478bd9Sstevel@tonic-gate case 'x': /* Localized date format */
318*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size,
319*7c478bd9Sstevel@tonic-gate dtcp->sdate_format, tm);
320*7c478bd9Sstevel@tonic-gate if (i == 0)
321*7c478bd9Sstevel@tonic-gate return (0);
322*7c478bd9Sstevel@tonic-gate cp += i;
323*7c478bd9Sstevel@tonic-gate size += i;
324*7c478bd9Sstevel@tonic-gate break;
325*7c478bd9Sstevel@tonic-gate
326*7c478bd9Sstevel@tonic-gate case 'X': /* Localized time format */
327*7c478bd9Sstevel@tonic-gate i = strftime(cp, maxsize - size,
328*7c478bd9Sstevel@tonic-gate dtcp->time_format, tm);
329*7c478bd9Sstevel@tonic-gate if (i == 0)
330*7c478bd9Sstevel@tonic-gate return (0);
331*7c478bd9Sstevel@tonic-gate cp += i;
332*7c478bd9Sstevel@tonic-gate size += i;
333*7c478bd9Sstevel@tonic-gate break;
334*7c478bd9Sstevel@tonic-gate
335*7c478bd9Sstevel@tonic-gate case 'y': /* Year in the form yy */
336*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
337*7c478bd9Sstevel@tonic-gate return (0);
338*7c478bd9Sstevel@tonic-gate cp = itoa((tm->tm_year% 100), cp, 2);
339*7c478bd9Sstevel@tonic-gate break;
340*7c478bd9Sstevel@tonic-gate
341*7c478bd9Sstevel@tonic-gate case 'Y': /* Year in the form ccyy */
342*7c478bd9Sstevel@tonic-gate if ((size += 4) >= maxsize)
343*7c478bd9Sstevel@tonic-gate return (0);
344*7c478bd9Sstevel@tonic-gate cp = itoa(1900 + tm->tm_year, cp, 4);
345*7c478bd9Sstevel@tonic-gate break;
346*7c478bd9Sstevel@tonic-gate
347*7c478bd9Sstevel@tonic-gate case 'Z': /* Timezone */
348*7c478bd9Sstevel@tonic-gate for(p = tm->tm_zone; *p != '\0'; p++) {
349*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
350*7c478bd9Sstevel@tonic-gate return (0);
351*7c478bd9Sstevel@tonic-gate *cp++ = *p;
352*7c478bd9Sstevel@tonic-gate }
353*7c478bd9Sstevel@tonic-gate break;
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate default:
356*7c478bd9Sstevel@tonic-gate if ((size += 2) >= maxsize)
357*7c478bd9Sstevel@tonic-gate return (0);
358*7c478bd9Sstevel@tonic-gate *cp++ = c;
359*7c478bd9Sstevel@tonic-gate *cp++ = *(format - 1);
360*7c478bd9Sstevel@tonic-gate break;
361*7c478bd9Sstevel@tonic-gate }
362*7c478bd9Sstevel@tonic-gate } else {
363*7c478bd9Sstevel@tonic-gate if (++size >= maxsize)
364*7c478bd9Sstevel@tonic-gate return (0);
365*7c478bd9Sstevel@tonic-gate *cp++ = c;
366*7c478bd9Sstevel@tonic-gate }
367*7c478bd9Sstevel@tonic-gate }
368*7c478bd9Sstevel@tonic-gate *cp = '\0';
369*7c478bd9Sstevel@tonic-gate return(size);
370*7c478bd9Sstevel@tonic-gate }
371*7c478bd9Sstevel@tonic-gate
372*7c478bd9Sstevel@tonic-gate static char *
itoa(i,ptr,dig)373*7c478bd9Sstevel@tonic-gate itoa(i, ptr, dig)
374*7c478bd9Sstevel@tonic-gate register int i;
375*7c478bd9Sstevel@tonic-gate register char *ptr;
376*7c478bd9Sstevel@tonic-gate register int dig;
377*7c478bd9Sstevel@tonic-gate {
378*7c478bd9Sstevel@tonic-gate switch(dig) {
379*7c478bd9Sstevel@tonic-gate case 4:
380*7c478bd9Sstevel@tonic-gate *ptr++ = i / 1000 + '0';
381*7c478bd9Sstevel@tonic-gate i = i - i / 1000 * 1000;
382*7c478bd9Sstevel@tonic-gate case 3:
383*7c478bd9Sstevel@tonic-gate *ptr++ = i / 100 + '0';
384*7c478bd9Sstevel@tonic-gate i = i - i / 100 * 100;
385*7c478bd9Sstevel@tonic-gate case 2:
386*7c478bd9Sstevel@tonic-gate *ptr++ = i / 10 + '0';
387*7c478bd9Sstevel@tonic-gate case 1:
388*7c478bd9Sstevel@tonic-gate *ptr++ = i % 10 + '0';
389*7c478bd9Sstevel@tonic-gate }
390*7c478bd9Sstevel@tonic-gate
391*7c478bd9Sstevel@tonic-gate return(ptr);
392*7c478bd9Sstevel@tonic-gate }
393*7c478bd9Sstevel@tonic-gate
394*7c478bd9Sstevel@tonic-gate char *
getlocale_time()395*7c478bd9Sstevel@tonic-gate getlocale_time()
396*7c478bd9Sstevel@tonic-gate {
397*7c478bd9Sstevel@tonic-gate register int fd;
398*7c478bd9Sstevel@tonic-gate struct stat buf;
399*7c478bd9Sstevel@tonic-gate char *str;
400*7c478bd9Sstevel@tonic-gate register char *p;
401*7c478bd9Sstevel@tonic-gate register int i;
402*7c478bd9Sstevel@tonic-gate struct dtconv dtconvp;
403*7c478bd9Sstevel@tonic-gate char temp[MAXLOCALENAME + 1];
404*7c478bd9Sstevel@tonic-gate
405*7c478bd9Sstevel@tonic-gate if (_locales[0][0] == '\0')
406*7c478bd9Sstevel@tonic-gate init_statics();
407*7c478bd9Sstevel@tonic-gate
408*7c478bd9Sstevel@tonic-gate /* Here we use the string newlocales to set time constants
409*7c478bd9Sstevel@tonic-gate * which should have been saved
410*7c478bd9Sstevel@tonic-gate * from a previous call to setlocale. We deferred the read until now
411*7c478bd9Sstevel@tonic-gate */
412*7c478bd9Sstevel@tonic-gate
413*7c478bd9Sstevel@tonic-gate if (strcmp(_my_time, _locales[LC_TIME -1]) == 0) {
414*7c478bd9Sstevel@tonic-gate if (dtconv_str == NULL) {
415*7c478bd9Sstevel@tonic-gate /*
416*7c478bd9Sstevel@tonic-gate * Below is executed if getlocale_time()
417*7c478bd9Sstevel@tonic-gate * is called when LC_TIME locale is initial
418*7c478bd9Sstevel@tonic-gate * C locale.
419*7c478bd9Sstevel@tonic-gate */
420*7c478bd9Sstevel@tonic-gate strcpy(temp, "C");
421*7c478bd9Sstevel@tonic-gate /*
422*7c478bd9Sstevel@tonic-gate * Just to make openlocale() to read LC_TIME file.
423*7c478bd9Sstevel@tonic-gate */
424*7c478bd9Sstevel@tonic-gate strcat(_locales[LC_TIME-1], temp);
425*7c478bd9Sstevel@tonic-gate goto initial;
426*7c478bd9Sstevel@tonic-gate }
427*7c478bd9Sstevel@tonic-gate return dtconv_str;
428*7c478bd9Sstevel@tonic-gate }
429*7c478bd9Sstevel@tonic-gate strcpy(temp, _locales[LC_TIME - 1]);
430*7c478bd9Sstevel@tonic-gate strcpy(_locales[LC_TIME - 1], _my_time);
431*7c478bd9Sstevel@tonic-gate initial:
432*7c478bd9Sstevel@tonic-gate if ((fd = openlocale("LC_TIME", LC_TIME, temp, _locales[LC_TIME - 1])) < 0)
433*7c478bd9Sstevel@tonic-gate return (NULL);
434*7c478bd9Sstevel@tonic-gate strcpy(_my_time, _locales[LC_TIME - 1]);
435*7c478bd9Sstevel@tonic-gate if (fd == 0)
436*7c478bd9Sstevel@tonic-gate return dtconv_str;
437*7c478bd9Sstevel@tonic-gate if ((fstat(fd, &buf)) != 0)
438*7c478bd9Sstevel@tonic-gate return (NULL);
439*7c478bd9Sstevel@tonic-gate if ((str = malloc((unsigned)buf.st_size + 2)) == NULL) {
440*7c478bd9Sstevel@tonic-gate close(fd);
441*7c478bd9Sstevel@tonic-gate return (NULL);
442*7c478bd9Sstevel@tonic-gate }
443*7c478bd9Sstevel@tonic-gate
444*7c478bd9Sstevel@tonic-gate if ((read(fd, str, (int)buf.st_size)) != buf.st_size) {
445*7c478bd9Sstevel@tonic-gate close(fd);
446*7c478bd9Sstevel@tonic-gate free(str);
447*7c478bd9Sstevel@tonic-gate return (NULL);
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate
450*7c478bd9Sstevel@tonic-gate /* Set last character of str to '\0' */
451*7c478bd9Sstevel@tonic-gate p = &str[buf.st_size];
452*7c478bd9Sstevel@tonic-gate *p++ = '\n';
453*7c478bd9Sstevel@tonic-gate *p = '\0';
454*7c478bd9Sstevel@tonic-gate
455*7c478bd9Sstevel@tonic-gate /* p will "walk thru" str */
456*7c478bd9Sstevel@tonic-gate p = str;
457*7c478bd9Sstevel@tonic-gate
458*7c478bd9Sstevel@tonic-gate for (i = 0; i < 12; i++) {
459*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.abbrev_month_names[i]);
460*7c478bd9Sstevel@tonic-gate if (p == NULL)
461*7c478bd9Sstevel@tonic-gate goto fail;
462*7c478bd9Sstevel@tonic-gate }
463*7c478bd9Sstevel@tonic-gate for (i = 0; i < 12; i++) {
464*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.month_names[i]);
465*7c478bd9Sstevel@tonic-gate if (p == NULL)
466*7c478bd9Sstevel@tonic-gate goto fail;
467*7c478bd9Sstevel@tonic-gate }
468*7c478bd9Sstevel@tonic-gate for (i = 0; i < 7; i++) {
469*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.abbrev_weekday_names[i]);
470*7c478bd9Sstevel@tonic-gate if (p == NULL)
471*7c478bd9Sstevel@tonic-gate goto fail;
472*7c478bd9Sstevel@tonic-gate }
473*7c478bd9Sstevel@tonic-gate for (i = 0; i < 7; i++) {
474*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.weekday_names[i]);
475*7c478bd9Sstevel@tonic-gate if (p == NULL)
476*7c478bd9Sstevel@tonic-gate goto fail;
477*7c478bd9Sstevel@tonic-gate }
478*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.time_format);
479*7c478bd9Sstevel@tonic-gate if (p == NULL)
480*7c478bd9Sstevel@tonic-gate goto fail;
481*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.sdate_format);
482*7c478bd9Sstevel@tonic-gate if (p == NULL)
483*7c478bd9Sstevel@tonic-gate goto fail;
484*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.dtime_format);
485*7c478bd9Sstevel@tonic-gate if (p == NULL)
486*7c478bd9Sstevel@tonic-gate goto fail;
487*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.am_string);
488*7c478bd9Sstevel@tonic-gate if (p == NULL)
489*7c478bd9Sstevel@tonic-gate goto fail;
490*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.pm_string);
491*7c478bd9Sstevel@tonic-gate if (p == NULL)
492*7c478bd9Sstevel@tonic-gate goto fail;
493*7c478bd9Sstevel@tonic-gate p = getstr(p, &dtconvp.ldate_format);
494*7c478bd9Sstevel@tonic-gate if (p == NULL)
495*7c478bd9Sstevel@tonic-gate goto fail;
496*7c478bd9Sstevel@tonic-gate (void) close(fd);
497*7c478bd9Sstevel@tonic-gate
498*7c478bd9Sstevel@tonic-gate /*
499*7c478bd9Sstevel@tonic-gate * set info.
500*7c478bd9Sstevel@tonic-gate */
501*7c478bd9Sstevel@tonic-gate if (dtconv_str != NULL)
502*7c478bd9Sstevel@tonic-gate free(dtconv_str);
503*7c478bd9Sstevel@tonic-gate
504*7c478bd9Sstevel@tonic-gate dtconv_str = str;
505*7c478bd9Sstevel@tonic-gate
506*7c478bd9Sstevel@tonic-gate /* The following is to get space malloc'd for _dtconv */
507*7c478bd9Sstevel@tonic-gate
508*7c478bd9Sstevel@tonic-gate if (_dtconv == 0)
509*7c478bd9Sstevel@tonic-gate (void) localdtconv();
510*7c478bd9Sstevel@tonic-gate memcpy(_dtconv, &dtconvp, sizeof(struct dtconv));
511*7c478bd9Sstevel@tonic-gate return (dtconv_str);
512*7c478bd9Sstevel@tonic-gate
513*7c478bd9Sstevel@tonic-gate fail:
514*7c478bd9Sstevel@tonic-gate (void) close(fd);
515*7c478bd9Sstevel@tonic-gate free(str);
516*7c478bd9Sstevel@tonic-gate return (NULL);
517*7c478bd9Sstevel@tonic-gate }
518*7c478bd9Sstevel@tonic-gate
519*7c478bd9Sstevel@tonic-gate
520*7c478bd9Sstevel@tonic-gate static char *
getstr(p,strp)521*7c478bd9Sstevel@tonic-gate getstr(p, strp)
522*7c478bd9Sstevel@tonic-gate register char *p;
523*7c478bd9Sstevel@tonic-gate char **strp;
524*7c478bd9Sstevel@tonic-gate {
525*7c478bd9Sstevel@tonic-gate *strp = p;
526*7c478bd9Sstevel@tonic-gate p = strchr(p, '\n');
527*7c478bd9Sstevel@tonic-gate if (p == NULL)
528*7c478bd9Sstevel@tonic-gate return (NULL); /* no end-of-line */
529*7c478bd9Sstevel@tonic-gate *p++ = '\0';
530*7c478bd9Sstevel@tonic-gate return (p);
531*7c478bd9Sstevel@tonic-gate }
532