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