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