xref: /openbsd-src/lib/libc/time/strftime.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 #if defined(LIBC_SCCS) && !defined(lint) && !defined(NOID)
2 static char elsieid[] = "@(#)strftime.c	7.64";
3 static char *rcsid = "$OpenBSD: strftime.c,v 1.11 2002/06/19 14:56:28 millert Exp $";
4 #endif /* LIBC_SCCS and not lint */
5 
6 #include "private.h"
7 
8 /*
9 ** Based on the UCB version with the ID appearing below.
10 ** This is ANSIish only when "multibyte character == plain character".
11 **
12 ** Copyright (c) 1989, 1993
13 **	The Regents of the University of California.  All rights reserved.
14 **
15 ** Redistribution and use in source and binary forms, with or without
16 ** modification, are permitted provided that the following conditions
17 ** are met:
18 ** 1. Redistributions of source code must retain the above copyright
19 **    notice, this list of conditions and the following disclaimer.
20 ** 2. Redistributions in binary form must reproduce the above copyright
21 **    notice, this list of conditions and the following disclaimer in the
22 **    documentation and/or other materials provided with the distribution.
23 ** 3. All advertising materials mentioning features or use of this software
24 **    must display the following acknowledgement:
25 **	This product includes software developed by the University of
26 **	California, Berkeley and its contributors.
27 ** 4. Neither the name of the University nor the names of its contributors
28 **    may be used to endorse or promote products derived from this software
29 **    without specific prior written permission.
30 **
31 ** THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 ** SUCH DAMAGE.
42 */
43 
44 #if 0
45 #ifndef LIBC_SCCS
46 #ifndef lint
47 static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
48 #endif /* !defined lint */
49 #endif /* !defined LIBC_SCCS */
50 #endif
51 
52 #include "tzfile.h"
53 #include "fcntl.h"
54 #include "locale.h"
55 
56 struct lc_time_T {
57 	const char *	mon[MONSPERYEAR];
58 	const char *	month[MONSPERYEAR];
59 	const char *	wday[DAYSPERWEEK];
60 	const char *	weekday[DAYSPERWEEK];
61 	const char *	X_fmt;
62 	const char *	x_fmt;
63 	const char *	c_fmt;
64 	const char *	am;
65 	const char *	pm;
66 	const char *	date_fmt;
67 };
68 
69 #ifdef LOCALE_HOME
70 #include "sys/stat.h"
71 static struct lc_time_T		localebuf;
72 static struct lc_time_T *	_loc P((void));
73 #define Locale	_loc()
74 #endif /* defined LOCALE_HOME */
75 #ifndef LOCALE_HOME
76 #define Locale	(&C_time_locale)
77 #endif /* !defined LOCALE_HOME */
78 
79 static const struct lc_time_T	C_time_locale = {
80 	{
81 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
82 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
83 	}, {
84 		"January", "February", "March", "April", "May", "June",
85 		"July", "August", "September", "October", "November", "December"
86 	}, {
87 		"Sun", "Mon", "Tue", "Wed",
88 		"Thu", "Fri", "Sat"
89 	}, {
90 		"Sunday", "Monday", "Tuesday", "Wednesday",
91 		"Thursday", "Friday", "Saturday"
92 	},
93 
94 	/* X_fmt */
95 	"%H:%M:%S",
96 
97 	/*
98 	** x_fmt
99 	** C99 requires this format.
100 	** Using just numbers (as here) makes Quakers happier;
101 	** it's also compatible with SVR4.
102 	*/
103 	"%m/%d/%y",
104 
105 	/*
106 	** c_fmt
107 	** C99 requires this format.
108 	** Previously this code used "%D %X", but we now conform to C99.
109 	** Note that
110 	**      "%a %b %d %H:%M:%S %Y"
111 	** is used by Solaris 2.3.
112 	*/
113 	"%a %b %e %T %Y",
114 
115 	/* am */
116 	"AM",
117 
118 	/* pm */
119 	"PM",
120 
121 	/* date_fmt */
122 	"%a %b %e %H:%M:%S %Z %Y"
123 };
124 
125 static char *	_add P((const char *, char *, const char *));
126 static char *	_conv P((int, const char *, char *, const char *));
127 static char *	_fmt P((const char *, const struct tm *, char *, const char *, int *));
128 
129 size_t strftime P((char *, size_t, const char *, const struct tm *));
130 
131 extern char *	tzname[];
132 
133 #ifndef YEAR_2000_NAME
134 #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
135 #endif /* !defined YEAR_2000_NAME */
136 
137 
138 #define IN_NONE	0
139 #define IN_SOME	1
140 #define IN_THIS	2
141 #define IN_ALL	3
142 
143 size_t
144 strftime(s, maxsize, format, t)
145 char * const		s;
146 const size_t		maxsize;
147 const char * const	format;
148 const struct tm * const	t;
149 {
150 	char *	p;
151 	int	warn;
152 
153 	tzset();
154 #ifdef LOCALE_HOME
155 	localebuf.mon[0] = 0;
156 #endif /* defined LOCALE_HOME */
157 	warn = IN_NONE;
158 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
159 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
160 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
161 		(void) fprintf(stderr, "\n");
162 		if (format == NULL)
163 			(void) fprintf(stderr, "NULL strftime format ");
164 		else	(void) fprintf(stderr, "strftime format \"%s\" ",
165 				format);
166 		(void) fprintf(stderr, "yields only two digits of years in ");
167 		if (warn == IN_SOME)
168 			(void) fprintf(stderr, "some locales");
169 		else if (warn == IN_THIS)
170 			(void) fprintf(stderr, "the current locale");
171 		else	(void) fprintf(stderr, "all locales");
172 		(void) fprintf(stderr, "\n");
173 	}
174 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
175 	if (p == s + maxsize) {
176 		if (maxsize > 0)
177 			s[maxsize - 1] = '\0';
178 		return 0;
179 	}
180 	*p = '\0';
181 	return p - s;
182 }
183 
184 static char *
185 _fmt(format, t, pt, ptlim, warnp)
186 const char *		format;
187 const struct tm * const	t;
188 char *			pt;
189 const char * const	ptlim;
190 int *			warnp;
191 {
192 	for ( ; *format; ++format) {
193 		if (*format == '%') {
194 label:
195 			switch (*++format) {
196 			case '\0':
197 				--format;
198 				break;
199 			case 'A':
200 				pt = _add((t->tm_wday < 0 ||
201 					t->tm_wday >= DAYSPERWEEK) ?
202 					"?" : Locale->weekday[t->tm_wday],
203 					pt, ptlim);
204 				continue;
205 			case 'a':
206 				pt = _add((t->tm_wday < 0 ||
207 					t->tm_wday >= DAYSPERWEEK) ?
208 					"?" : Locale->wday[t->tm_wday],
209 					pt, ptlim);
210 				continue;
211 			case 'B':
212 				pt = _add((t->tm_mon < 0 ||
213 					t->tm_mon >= MONSPERYEAR) ?
214 					"?" : Locale->month[t->tm_mon],
215 					pt, ptlim);
216 				continue;
217 			case 'b':
218 			case 'h':
219 				pt = _add((t->tm_mon < 0 ||
220 					t->tm_mon >= MONSPERYEAR) ?
221 					"?" : Locale->mon[t->tm_mon],
222 					pt, ptlim);
223 				continue;
224 			case 'C':
225 				/*
226 				** %C used to do a...
227 				**	_fmt("%a %b %e %X %Y", t);
228 				** ...whereas now POSIX 1003.2 calls for
229 				** something completely different.
230 				** (ado, 1993-05-24)
231 				*/
232 				pt = _conv((t->tm_year + TM_YEAR_BASE) / 100,
233 					"%02d", pt, ptlim);
234 				continue;
235 			case 'c':
236 				{
237 				int warn2 = IN_SOME;
238 
239 				pt = _fmt(Locale->c_fmt, t, pt, ptlim, warnp);
240 				if (warn2 == IN_ALL)
241 					warn2 = IN_THIS;
242 				if (warn2 > *warnp)
243 					*warnp = warn2;
244 				}
245 				continue;
246 			case 'D':
247 				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
248 				continue;
249 			case 'd':
250 				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
251 				continue;
252 			case 'E':
253 			case 'O':
254 				/*
255 				** C99 locale modifiers.
256 				** The sequences
257 				**	%Ec %EC %Ex %EX %Ey %EY
258 				**	%Od %oe %OH %OI %Om %OM
259 				**	%OS %Ou %OU %OV %Ow %OW %Oy
260 				** are supposed to provide alternate
261 				** representations.
262 				*/
263 				goto label;
264 			case 'e':
265 				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
266 				continue;
267 			case 'F':
268 				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
269 				continue;
270 			case 'H':
271 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
272 				continue;
273 			case 'I':
274 				pt = _conv((t->tm_hour % 12) ?
275 					(t->tm_hour % 12) : 12,
276 					"%02d", pt, ptlim);
277 				continue;
278 			case 'j':
279 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
280 				continue;
281 			case 'k':
282 				/*
283 				** This used to be...
284 				**	_conv(t->tm_hour % 12 ?
285 				**		t->tm_hour % 12 : 12, 2, ' ');
286 				** ...and has been changed to the below to
287 				** match SunOS 4.1.1 and Arnold Robbins'
288 				** strftime version 3.0.  That is, "%k" and
289 				** "%l" have been swapped.
290 				** (ado, 1993-05-24)
291 				*/
292 				pt = _conv(t->tm_hour, "%2d", pt, ptlim);
293 				continue;
294 #ifdef KITCHEN_SINK
295 			case 'K':
296 				/*
297 				** After all this time, still unclaimed!
298 				*/
299 				pt = _add("kitchen sink", pt, ptlim);
300 				continue;
301 #endif /* defined KITCHEN_SINK */
302 			case 'l':
303 				/*
304 				** This used to be...
305 				**	_conv(t->tm_hour, 2, ' ');
306 				** ...and has been changed to the below to
307 				** match SunOS 4.1.1 and Arnold Robbin's
308 				** strftime version 3.0.  That is, "%k" and
309 				** "%l" have been swapped.
310 				** (ado, 1993-05-24)
311 				*/
312 				pt = _conv((t->tm_hour % 12) ?
313 					(t->tm_hour % 12) : 12,
314 					"%2d", pt, ptlim);
315 				continue;
316 			case 'M':
317 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
318 				continue;
319 			case 'm':
320 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
321 				continue;
322 			case 'n':
323 				pt = _add("\n", pt, ptlim);
324 				continue;
325 			case 'p':
326 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
327 					Locale->pm :
328 					Locale->am,
329 					pt, ptlim);
330 				continue;
331 			case 'R':
332 				pt = _fmt("%H:%M", t, pt, ptlim, warnp);
333 				continue;
334 			case 'r':
335 				pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
336 				continue;
337 			case 'S':
338 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
339 				continue;
340 			case 's':
341 				{
342 					struct tm	tm;
343 					char		buf[INT_STRLEN_MAXIMUM(
344 								time_t) + 1];
345 					time_t		mkt;
346 
347 					tm = *t;
348 					mkt = mktime(&tm);
349 					if (TYPE_SIGNED(time_t))
350 						(void) snprintf(buf, sizeof buf,
351 						    "%ld", (long) mkt);
352 					else	(void) snprintf(buf, sizeof buf,
353 						    "%lu", (unsigned long) mkt);
354 					pt = _add(buf, pt, ptlim);
355 				}
356 				continue;
357 			case 'T':
358 				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
359 				continue;
360 			case 't':
361 				pt = _add("\t", pt, ptlim);
362 				continue;
363 			case 'U':
364 				pt = _conv((t->tm_yday + DAYSPERWEEK -
365 					t->tm_wday) / DAYSPERWEEK,
366 					"%02d", pt, ptlim);
367 				continue;
368 			case 'u':
369 				/*
370 				** From Arnold Robbins' strftime version 3.0:
371 				** "ISO 8601: Weekday as a decimal number
372 				** [1 (Monday) - 7]"
373 				** (ado, 1993-05-24)
374 				*/
375 				pt = _conv((t->tm_wday == 0) ?
376 					DAYSPERWEEK : t->tm_wday,
377 					"%d", pt, ptlim);
378 				continue;
379 			case 'V':	/* ISO 8601 week number */
380 			case 'G':	/* ISO 8601 year (four digits) */
381 			case 'g':	/* ISO 8601 year (two digits) */
382 /*
383 ** From Arnold Robbins' strftime version 3.0:  "the week number of the
384 ** year (the first Monday as the first day of week 1) as a decimal number
385 ** (01-53)."
386 ** (ado, 1993-05-24)
387 **
388 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
389 ** "Week 01 of a year is per definition the first week which has the
390 ** Thursday in this year, which is equivalent to the week which contains
391 ** the fourth day of January. In other words, the first week of a new year
392 ** is the week which has the majority of its days in the new year. Week 01
393 ** might also contain days from the previous year and the week before week
394 ** 01 of a year is the last week (52 or 53) of the previous year even if
395 ** it contains days from the new year. A week starts with Monday (day 1)
396 ** and ends with Sunday (day 7).  For example, the first week of the year
397 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
398 ** (ado, 1996-01-02)
399 */
400 				{
401 					int	year;
402 					int	yday;
403 					int	wday;
404 					int	w;
405 
406 					year = t->tm_year + TM_YEAR_BASE;
407 					yday = t->tm_yday;
408 					wday = t->tm_wday;
409 					for ( ; ; ) {
410 						int	len;
411 						int	bot;
412 						int	top;
413 
414 						len = isleap(year) ?
415 							DAYSPERLYEAR :
416 							DAYSPERNYEAR;
417 						/*
418 						** What yday (-3 ... 3) does
419 						** the ISO year begin on?
420 						*/
421 						bot = ((yday + 11 - wday) %
422 							DAYSPERWEEK) - 3;
423 						/*
424 						** What yday does the NEXT
425 						** ISO year begin on?
426 						*/
427 						top = bot -
428 							(len % DAYSPERWEEK);
429 						if (top < -3)
430 							top += DAYSPERWEEK;
431 						top += len;
432 						if (yday >= top) {
433 							++year;
434 							w = 1;
435 							break;
436 						}
437 						if (yday >= bot) {
438 							w = 1 + ((yday - bot) /
439 								DAYSPERWEEK);
440 							break;
441 						}
442 						--year;
443 						yday += isleap(year) ?
444 							DAYSPERLYEAR :
445 							DAYSPERNYEAR;
446 					}
447 #ifdef XPG4_1994_04_09
448 					if ((w == 52
449 					     && t->tm_mon == TM_JANUARY)
450 					    || (w == 1
451 						&& t->tm_mon == TM_DECEMBER))
452 						w = 53;
453 #endif /* defined XPG4_1994_04_09 */
454 					if (*format == 'V')
455 						pt = _conv(w, "%02d",
456 							pt, ptlim);
457 					else if (*format == 'g') {
458 						*warnp = IN_ALL;
459 						pt = _conv(year % 100, "%02d",
460 							pt, ptlim);
461 					} else	pt = _conv(year, "%04d",
462 							pt, ptlim);
463 				}
464 				continue;
465 			case 'v':
466 				/*
467 				** From Arnold Robbins' strftime version 3.0:
468 				** "date as dd-bbb-YYYY"
469 				** (ado, 1993-05-24)
470 				*/
471 				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
472 				continue;
473 			case 'W':
474 				pt = _conv((t->tm_yday + DAYSPERWEEK -
475 					(t->tm_wday ?
476 					(t->tm_wday - 1) :
477 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
478 					"%02d", pt, ptlim);
479 				continue;
480 			case 'w':
481 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
482 				continue;
483 			case 'X':
484 				pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
485 				continue;
486 			case 'x':
487 				{
488 				int	warn2 = IN_SOME;
489 
490 				pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
491 				if (warn2 == IN_ALL)
492 					warn2 = IN_THIS;
493 				if (warn2 > *warnp)
494 					*warnp = warn2;
495 				}
496 				continue;
497 			case 'y':
498 				*warnp = IN_ALL;
499 				pt = _conv((t->tm_year + TM_YEAR_BASE) % 100,
500 					"%02d", pt, ptlim);
501 				continue;
502 			case 'Y':
503 				pt = _conv(t->tm_year + TM_YEAR_BASE, "%04d",
504 					pt, ptlim);
505 				continue;
506 			case 'Z':
507 #ifdef TM_ZONE
508 				if (t->TM_ZONE != NULL)
509 					pt = _add(t->TM_ZONE, pt, ptlim);
510 				else
511 #endif /* defined TM_ZONE */
512 				if (t->tm_isdst >= 0)
513 					pt = _add(tzname[t->tm_isdst != 0],
514 						pt, ptlim);
515 				/*
516 				** C99 says that %Z must be replaced by the
517 				** empty string if the time zone is not
518 				** determinable.
519 				*/
520 				continue;
521 			case 'z':
522 				{
523 				int		diff;
524 				char const *	sign;
525 
526 				if (t->tm_isdst < 0)
527 					continue;
528 #ifdef TM_GMTOFF
529 				diff = t->TM_GMTOFF;
530 #else /* !defined TM_GMTOFF */
531 				/*
532 				** C99 says that the UTC offset must
533 				** be computed by looking only at
534 				** tm_isdst.  This requirement is
535 				** incorrect, since it means the code
536 				** must rely on magic (in this case
537 				** altzone and timezone), and the
538 				** magic might not have the correct
539 				** offset.  Doing things correctly is
540 				** tricky and requires disobeying C99;
541 				** see GNU C strftime for details.
542 				** For now, punt and conform to the
543 				** standard, even though it's incorrect.
544 				**
545 				** C99 says that %z must be replaced by the
546 				** empty string if the time zone is not
547 				** determinable, so output nothing if the
548 				** appropriate variables are not available.
549 				*/
550 				if (t->tm_isdst == 0)
551 #ifdef USG_COMPAT
552 					diff = -timezone;
553 #else /* !defined USG_COMPAT */
554 					continue;
555 #endif /* !defined USG_COMPAT */
556 				else
557 #ifdef ALTZONE
558 					diff = -altzone;
559 #else /* !defined ALTZONE */
560 					continue;
561 #endif /* !defined ALTZONE */
562 #endif /* !defined TM_GMTOFF */
563 				if (diff < 0) {
564 					sign = "-";
565 					diff = -diff;
566 				} else	sign = "+";
567 				pt = _add(sign, pt, ptlim);
568 				diff /= 60;
569 				pt = _conv((diff/60)*100 + diff%60,
570 					"%04d", pt, ptlim);
571 				}
572 				continue;
573 			case '+':
574 				pt = _fmt(Locale->date_fmt, t, pt, ptlim,
575 					warnp);
576 				continue;
577 			case '%':
578 			/*
579 			** X311J/88-090 (4.12.3.5): if conversion char is
580 			** undefined, behavior is undefined.  Print out the
581 			** character itself as printf(3) also does.
582 			*/
583 			default:
584 				break;
585 			}
586 		}
587 		if (pt == ptlim)
588 			break;
589 		*pt++ = *format;
590 	}
591 	return pt;
592 }
593 
594 static char *
595 _conv(n, format, pt, ptlim)
596 const int		n;
597 const char * const	format;
598 char * const		pt;
599 const char * const	ptlim;
600 {
601 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
602 
603 	(void) snprintf(buf, sizeof buf, format, n);
604 	return _add(buf, pt, ptlim);
605 }
606 
607 static char *
608 _add(str, pt, ptlim)
609 const char *		str;
610 char *			pt;
611 const char * const	ptlim;
612 {
613 	while (pt < ptlim && (*pt = *str++) != '\0')
614 		++pt;
615 	return pt;
616 }
617 
618 #ifdef LOCALE_HOME
619 static struct lc_time_T *
620 _loc P((void))
621 {
622 	static const char	locale_home[] = LOCALE_HOME;
623 	static const char	lc_time[] = "LC_TIME";
624 	static char *		locale_buf;
625 
626 	int			fd;
627 	int			oldsun;	/* "...ain't got nothin' to do..." */
628 	char *			lbuf;
629 	char *			nlbuf;
630 	char *			name;
631 	char *			p;
632 	const char **		ap;
633 	const char *		plim;
634 	char			filename[FILENAME_MAX];
635 	struct stat		st;
636 	size_t			namesize;
637 	size_t			bufsize;
638 
639 	/*
640 	** Use localebuf.mon[0] to signal whether locale is already set up.
641 	*/
642 	if (localebuf.mon[0])
643 		return &localebuf;
644 	name = setlocale(LC_TIME, (char *) NULL);
645 	if (name == NULL || *name == '\0')
646 		goto no_locale;
647 	/*
648 	** If the locale name is the same as our cache, use the cache.
649 	*/
650 	lbuf = locale_buf;
651 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
652 		p = lbuf;
653 		for (ap = (const char **) &localebuf;
654 			ap < (const char **) (&localebuf + 1);
655 				++ap)
656 					*ap = p += strlen(p) + 1;
657 		return &localebuf;
658 	}
659 	/*
660 	** Slurp the locale file into the cache.
661 	*/
662 	namesize = strlen(name) + 1;
663 	if (sizeof filename  <
664 		((sizeof locale_home) + namesize + (sizeof lc_time)))
665 			goto no_locale;
666 	oldsun = 0;
667 	(void) snprintf(filename, sizeof filename, "%s/%s/%s", locale_home,
668 	    name, lc_time);
669 	fd = open(filename, O_RDONLY);
670 	if (fd < 0) {
671 		/*
672 		** Old Sun systems have a different naming and data convention.
673 		*/
674 		oldsun = 1;
675 		(void) snprintf(filename, sizeof filename, "%s/%s/%s",
676 			locale_home, lc_time, name);
677 		fd = open(filename, O_RDONLY);
678 		if (fd < 0)
679 			goto no_locale;
680 	}
681 	if (fstat(fd, &st) != 0)
682 		goto bad_locale;
683 	if (st.st_size <= 0)
684 		goto bad_locale;
685 	bufsize = namesize + st.st_size;
686 	locale_buf = NULL;
687 	nlbuf = (lbuf == NULL) ? malloc(bufsize) : realloc(lbuf, bufsize);
688 	if (nlbuf == NULL) {
689 		if (lbuf)
690 			free(lbuf);
691 		lbuf = NULL;
692 		goto bad_locale;
693 	}
694 	lbuf = nlbuf;
695 	(void) strcpy(lbuf, name);
696 	p = lbuf + namesize;
697 	plim = p + st.st_size;
698 	if (read(fd, p, (size_t) st.st_size) != st.st_size)
699 		goto bad_lbuf;
700 	if (close(fd) != 0)
701 		goto bad_lbuf;
702 	/*
703 	** Parse the locale file into localebuf.
704 	*/
705 	if (plim[-1] != '\n')
706 		goto bad_lbuf;
707 	for (ap = (const char **) &localebuf;
708 		ap < (const char **) (&localebuf + 1);
709 			++ap) {
710 				if (p == plim)
711 					goto bad_lbuf;
712 				*ap = p;
713 				while (*p != '\n')
714 					++p;
715 				*p++ = '\0';
716 	}
717 	if (oldsun) {
718 		/*
719 		** SunOS 4 used an obsolescent format; see localdtconv(3).
720 		** c_fmt had the ``short format for dates and times together''
721 		** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
722 		** date_fmt had the ``long format for dates''
723 		** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
724 		** Discard the latter in favor of the former.
725 		*/
726 		localebuf.date_fmt = localebuf.c_fmt;
727 	}
728 	/*
729 	** Record the successful parse in the cache.
730 	*/
731 	locale_buf = lbuf;
732 
733 	return &localebuf;
734 
735 bad_lbuf:
736 	free(lbuf);
737 bad_locale:
738 	(void) close(fd);
739 no_locale:
740 	localebuf = C_time_locale;
741 	locale_buf = NULL;
742 	return &localebuf;
743 }
744 #endif /* defined LOCALE_HOME */
745