xref: /openbsd-src/lib/libc/time/strftime.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: strftime.c,v 1.21 2012/09/13 11:14:20 millert 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 #ifndef YEAR_2000_NAME
115 #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
116 #endif /* !defined YEAR_2000_NAME */
117 
118 #define IN_NONE	0
119 #define IN_SOME	1
120 #define IN_THIS	2
121 #define IN_ALL	3
122 
123 size_t
124 strftime(s, maxsize, format, t)
125 char * const		s;
126 const size_t		maxsize;
127 const char * const	format;
128 const struct tm * const	t;
129 {
130 	char *	p;
131 	int	warn;
132 
133 	tzset();
134 #ifdef LOCALE_HOME
135 	localebuf.mon[0] = 0;
136 #endif /* defined LOCALE_HOME */
137 	warn = IN_NONE;
138 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
139 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
140 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
141 		(void) fprintf(stderr, "\n");
142 		if (format == NULL)
143 			(void) fprintf(stderr, "NULL strftime format ");
144 		else	(void) fprintf(stderr, "strftime format \"%s\" ",
145 				format);
146 		(void) fprintf(stderr, "yields only two digits of years in ");
147 		if (warn == IN_SOME)
148 			(void) fprintf(stderr, "some locales");
149 		else if (warn == IN_THIS)
150 			(void) fprintf(stderr, "the current locale");
151 		else	(void) fprintf(stderr, "all locales");
152 		(void) fprintf(stderr, "\n");
153 	}
154 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
155 	if (p == s + maxsize) {
156 		if (maxsize > 0)
157 			s[maxsize - 1] = '\0';
158 		return 0;
159 	}
160 	*p = '\0';
161 	return p - s;
162 }
163 
164 static char *
165 _fmt(format, t, pt, ptlim, warnp)
166 const char *		format;
167 const struct tm * const	t;
168 char *			pt;
169 const char * const	ptlim;
170 int *			warnp;
171 {
172 	for ( ; *format; ++format) {
173 		if (*format == '%') {
174 label:
175 			switch (*++format) {
176 			case '\0':
177 				--format;
178 				break;
179 			case 'A':
180 				pt = _add((t->tm_wday < 0 ||
181 					t->tm_wday >= DAYSPERWEEK) ?
182 					"?" : Locale->weekday[t->tm_wday],
183 					pt, ptlim);
184 				continue;
185 			case 'a':
186 				pt = _add((t->tm_wday < 0 ||
187 					t->tm_wday >= DAYSPERWEEK) ?
188 					"?" : Locale->wday[t->tm_wday],
189 					pt, ptlim);
190 				continue;
191 			case 'B':
192 				pt = _add((t->tm_mon < 0 ||
193 					t->tm_mon >= MONSPERYEAR) ?
194 					"?" : Locale->month[t->tm_mon],
195 					pt, ptlim);
196 				continue;
197 			case 'b':
198 			case 'h':
199 				pt = _add((t->tm_mon < 0 ||
200 					t->tm_mon >= MONSPERYEAR) ?
201 					"?" : Locale->mon[t->tm_mon],
202 					pt, ptlim);
203 				continue;
204 			case 'C':
205 				/*
206 				** %C used to do a...
207 				**	_fmt("%a %b %e %X %Y", t);
208 				** ...whereas now POSIX 1003.2 calls for
209 				** something completely different.
210 				** (ado, 1993-05-24)
211 				*/
212 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 0,
213 					pt, ptlim);
214 				continue;
215 			case 'c':
216 				{
217 				int warn2 = IN_SOME;
218 
219 				pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
220 				if (warn2 == IN_ALL)
221 					warn2 = IN_THIS;
222 				if (warn2 > *warnp)
223 					*warnp = warn2;
224 				}
225 				continue;
226 			case 'D':
227 				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
228 				continue;
229 			case 'd':
230 				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
231 				continue;
232 			case 'E':
233 			case 'O':
234 				/*
235 				** C99 locale modifiers.
236 				** The sequences
237 				**	%Ec %EC %Ex %EX %Ey %EY
238 				**	%Od %oe %OH %OI %Om %OM
239 				**	%OS %Ou %OU %OV %Ow %OW %Oy
240 				** are supposed to provide alternate
241 				** representations.
242 				*/
243 				goto label;
244 			case 'e':
245 				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
246 				continue;
247 			case 'F':
248 				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
249 				continue;
250 			case 'H':
251 				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
252 				continue;
253 			case 'I':
254 				pt = _conv((t->tm_hour % 12) ?
255 					(t->tm_hour % 12) : 12,
256 					"%02d", pt, ptlim);
257 				continue;
258 			case 'j':
259 				pt = _conv(t->tm_yday + 1, "%03d", pt, ptlim);
260 				continue;
261 			case 'k':
262 				/*
263 				** This used to be...
264 				**	_conv(t->tm_hour % 12 ?
265 				**		t->tm_hour % 12 : 12, 2, ' ');
266 				** ...and has been changed to the below to
267 				** match SunOS 4.1.1 and Arnold Robbins'
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, "%2d", pt, ptlim);
273 				continue;
274 #ifdef KITCHEN_SINK
275 			case 'K':
276 				/*
277 				** After all this time, still unclaimed!
278 				*/
279 				pt = _add("kitchen sink", pt, ptlim);
280 				continue;
281 #endif /* defined KITCHEN_SINK */
282 			case 'l':
283 				/*
284 				** This used to be...
285 				**	_conv(t->tm_hour, 2, ' ');
286 				** ...and has been changed to the below to
287 				** match SunOS 4.1.1 and Arnold Robbin's
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 % 12) ?
293 					(t->tm_hour % 12) : 12,
294 					"%2d", pt, ptlim);
295 				continue;
296 			case 'M':
297 				pt = _conv(t->tm_min, "%02d", pt, ptlim);
298 				continue;
299 			case 'm':
300 				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
301 				continue;
302 			case 'n':
303 				pt = _add("\n", pt, ptlim);
304 				continue;
305 			case 'p':
306 				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
307 					Locale->pm :
308 					Locale->am,
309 					pt, ptlim);
310 				continue;
311 			case 'R':
312 				pt = _fmt("%H:%M", t, pt, ptlim, warnp);
313 				continue;
314 			case 'r':
315 				pt = _fmt("%I:%M:%S %p", t, pt, ptlim, warnp);
316 				continue;
317 			case 'S':
318 				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
319 				continue;
320 			case 's':
321 				{
322 					struct tm	tm;
323 					char		buf[INT_STRLEN_MAXIMUM(
324 								time_t) + 1];
325 					time_t		mkt;
326 
327 					tm = *t;
328 					mkt = mktime(&tm);
329 					if (TYPE_SIGNED(time_t))
330 						(void) snprintf(buf, sizeof buf,
331 						    "%ld", (long) mkt);
332 					else	(void) snprintf(buf, sizeof buf,
333 						    "%lu", (unsigned long) mkt);
334 					pt = _add(buf, pt, ptlim);
335 				}
336 				continue;
337 			case 'T':
338 				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
339 				continue;
340 			case 't':
341 				pt = _add("\t", pt, ptlim);
342 				continue;
343 			case 'U':
344 				pt = _conv((t->tm_yday + DAYSPERWEEK -
345 					t->tm_wday) / DAYSPERWEEK,
346 					"%02d", pt, ptlim);
347 				continue;
348 			case 'u':
349 				/*
350 				** From Arnold Robbins' strftime version 3.0:
351 				** "ISO 8601: Weekday as a decimal number
352 				** [1 (Monday) - 7]"
353 				** (ado, 1993-05-24)
354 				*/
355 				pt = _conv((t->tm_wday == 0) ?
356 					DAYSPERWEEK : t->tm_wday,
357 					"%d", pt, ptlim);
358 				continue;
359 			case 'V':	/* ISO 8601 week number */
360 			case 'G':	/* ISO 8601 year (four digits) */
361 			case 'g':	/* ISO 8601 year (two digits) */
362 /*
363 ** From Arnold Robbins' strftime version 3.0: "the week number of the
364 ** year (the first Monday as the first day of week 1) as a decimal number
365 ** (01-53)."
366 ** (ado, 1993-05-24)
367 **
368 ** From "http://www.ft.uni-erlangen.de/~mskuhn/iso-time.html" by Markus Kuhn:
369 ** "Week 01 of a year is per definition the first week which has the
370 ** Thursday in this year, which is equivalent to the week which contains
371 ** the fourth day of January. In other words, the first week of a new year
372 ** is the week which has the majority of its days in the new year. Week 01
373 ** might also contain days from the previous year and the week before week
374 ** 01 of a year is the last week (52 or 53) of the previous year even if
375 ** it contains days from the new year. A week starts with Monday (day 1)
376 ** and ends with Sunday (day 7). For example, the first week of the year
377 ** 1997 lasts from 1996-12-30 to 1997-01-05..."
378 ** (ado, 1996-01-02)
379 */
380 				{
381 					int	year;
382 					int	base;
383 					int	yday;
384 					int	wday;
385 					int	w;
386 
387 					year = t->tm_year;
388 					base = TM_YEAR_BASE;
389 					yday = t->tm_yday;
390 					wday = t->tm_wday;
391 					for ( ; ; ) {
392 						int	len;
393 						int	bot;
394 						int	top;
395 
396 						len = isleap_sum(year, base) ?
397 							DAYSPERLYEAR :
398 							DAYSPERNYEAR;
399 						/*
400 						** What yday (-3 ... 3) does
401 						** the ISO year begin on?
402 						*/
403 						bot = ((yday + 11 - wday) %
404 							DAYSPERWEEK) - 3;
405 						/*
406 						** What yday does the NEXT
407 						** ISO year begin on?
408 						*/
409 						top = bot -
410 							(len % DAYSPERWEEK);
411 						if (top < -3)
412 							top += DAYSPERWEEK;
413 						top += len;
414 						if (yday >= top) {
415 							++base;
416 							w = 1;
417 							break;
418 						}
419 						if (yday >= bot) {
420 							w = 1 + ((yday - bot) /
421 								DAYSPERWEEK);
422 							break;
423 						}
424 						--base;
425 						yday += isleap_sum(year, base) ?
426 							DAYSPERLYEAR :
427 							DAYSPERNYEAR;
428 					}
429 #ifdef XPG4_1994_04_09
430 					if ((w == 52 &&
431 						t->tm_mon == TM_JANUARY) ||
432 						(w == 1 &&
433 						t->tm_mon == TM_DECEMBER))
434 							w = 53;
435 #endif /* defined XPG4_1994_04_09 */
436 					if (*format == 'V')
437 						pt = _conv(w, "%02d",
438 							pt, ptlim);
439 					else if (*format == 'g') {
440 						*warnp = IN_ALL;
441 						pt = _yconv(year, base, 0, 1,
442 							pt, ptlim);
443 					} else	pt = _yconv(year, base, 1, 1,
444 							pt, ptlim);
445 				}
446 				continue;
447 			case 'v':
448 				/*
449 				** From Arnold Robbins' strftime version 3.0:
450 				** "date as dd-bbb-YYYY"
451 				** (ado, 1993-05-24)
452 				*/
453 				pt = _fmt("%e-%b-%Y", t, pt, ptlim, warnp);
454 				continue;
455 			case 'W':
456 				pt = _conv((t->tm_yday + DAYSPERWEEK -
457 					(t->tm_wday ?
458 					(t->tm_wday - 1) :
459 					(DAYSPERWEEK - 1))) / DAYSPERWEEK,
460 					"%02d", pt, ptlim);
461 				continue;
462 			case 'w':
463 				pt = _conv(t->tm_wday, "%d", pt, ptlim);
464 				continue;
465 			case 'X':
466 				pt = _fmt(Locale->X_fmt, t, pt, ptlim, warnp);
467 				continue;
468 			case 'x':
469 				{
470 				int	warn2 = IN_SOME;
471 
472 				pt = _fmt(Locale->x_fmt, t, pt, ptlim, &warn2);
473 				if (warn2 == IN_ALL)
474 					warn2 = IN_THIS;
475 				if (warn2 > *warnp)
476 					*warnp = warn2;
477 				}
478 				continue;
479 			case 'y':
480 				*warnp = IN_ALL;
481 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 0, 1,
482 					pt, ptlim);
483 				continue;
484 			case 'Y':
485 				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
486 					pt, ptlim);
487 				continue;
488 			case 'Z':
489 #ifdef TM_ZONE
490 				if (t->TM_ZONE != NULL)
491 					pt = _add(t->TM_ZONE, pt, ptlim);
492 				else
493 #endif /* defined TM_ZONE */
494 				if (t->tm_isdst >= 0)
495 					pt = _add(tzname[t->tm_isdst != 0],
496 						pt, ptlim);
497 				/*
498 				** C99 says that %Z must be replaced by the
499 				** empty string if the time zone is not
500 				** determinable.
501 				*/
502 				continue;
503 			case 'z':
504 				{
505 				int		diff;
506 				char const *	sign;
507 
508 				if (t->tm_isdst < 0)
509 					continue;
510 #ifdef TM_GMTOFF
511 				diff = t->TM_GMTOFF;
512 #else /* !defined TM_GMTOFF */
513 				/*
514 				** C99 says that the UTC offset must
515 				** be computed by looking only at
516 				** tm_isdst. This requirement is
517 				** incorrect, since it means the code
518 				** must rely on magic (in this case
519 				** altzone and timezone), and the
520 				** magic might not have the correct
521 				** offset. Doing things correctly is
522 				** tricky and requires disobeying C99;
523 				** see GNU C strftime for details.
524 				** For now, punt and conform to the
525 				** standard, even though it's incorrect.
526 				**
527 				** C99 says that %z must be replaced by the
528 				** empty string if the time zone is not
529 				** determinable, so output nothing if the
530 				** appropriate variables are not available.
531 				*/
532 				if (t->tm_isdst == 0)
533 #ifdef USG_COMPAT
534 					diff = -timezone;
535 #else /* !defined USG_COMPAT */
536 					continue;
537 #endif /* !defined USG_COMPAT */
538 				else
539 #ifdef ALTZONE
540 					diff = -altzone;
541 #else /* !defined ALTZONE */
542 					continue;
543 #endif /* !defined ALTZONE */
544 #endif /* !defined TM_GMTOFF */
545 				if (diff < 0) {
546 					sign = "-";
547 					diff = -diff;
548 				} else	sign = "+";
549 				pt = _add(sign, pt, ptlim);
550 				diff /= SECSPERMIN;
551 				diff = (diff / MINSPERHOUR) * 100 +
552 					(diff % MINSPERHOUR);
553 				pt = _conv(diff, "%04d", pt, ptlim);
554 				}
555 				continue;
556 			case '+':
557 				pt = _fmt(Locale->date_fmt, t, pt, ptlim,
558 					warnp);
559 				continue;
560 			case '%':
561 			/*
562 			** X311J/88-090 (4.12.3.5): if conversion char is
563 			** undefined, behavior is undefined. Print out the
564 			** character itself as printf(3) also does.
565 			*/
566 			default:
567 				break;
568 			}
569 		}
570 		if (pt == ptlim)
571 			break;
572 		*pt++ = *format;
573 	}
574 	return pt;
575 }
576 
577 static char *
578 _conv(n, format, pt, ptlim)
579 const int		n;
580 const char * const	format;
581 char * const		pt;
582 const char * const	ptlim;
583 {
584 	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
585 
586 	(void) snprintf(buf, sizeof buf, format, n);
587 	return _add(buf, pt, ptlim);
588 }
589 
590 static char *
591 _add(str, pt, ptlim)
592 const char *		str;
593 char *			pt;
594 const char * const	ptlim;
595 {
596 	while (pt < ptlim && (*pt = *str++) != '\0')
597 		++pt;
598 	return pt;
599 }
600 
601 /*
602 ** POSIX and the C Standard are unclear or inconsistent about
603 ** what %C and %y do if the year is negative or exceeds 9999.
604 ** Use the convention that %C concatenated with %y yields the
605 ** same output as %Y, and that %Y contains at least 4 bytes,
606 ** with more only if necessary.
607 */
608 
609 static char *
610 _yconv(a, b, convert_top, convert_yy, pt, ptlim)
611 const int		a;
612 const int		b;
613 const int		convert_top;
614 const int		convert_yy;
615 char *			pt;
616 const char * const	ptlim;
617 {
618 	register int	lead;
619 	register int	trail;
620 
621 #define DIVISOR	100
622 	trail = a % DIVISOR + b % DIVISOR;
623 	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
624 	trail %= DIVISOR;
625 	if (trail < 0 && lead > 0) {
626 		trail += DIVISOR;
627 		--lead;
628 	} else if (lead < 0 && trail > 0) {
629 		trail -= DIVISOR;
630 		++lead;
631 	}
632 	if (convert_top) {
633 		if (lead == 0 && trail < 0)
634 			pt = _add("-0", pt, ptlim);
635 		else	pt = _conv(lead, "%02d", pt, ptlim);
636 	}
637 	if (convert_yy)
638 		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
639 	return pt;
640 }
641 
642 #ifdef LOCALE_HOME
643 static struct lc_time_T *
644 _loc(void)
645 {
646 	static const char	locale_home[] = LOCALE_HOME;
647 	static const char	lc_time[] = "LC_TIME";
648 	static char *		locale_buf;
649 
650 	int			fd;
651 	int			oldsun;	/* "...ain't got nothin' to do..." */
652 	int			len;
653 	char *			lbuf;
654 	char *			nlbuf;
655 	char *			name;
656 	char *			p;
657 	const char **		ap;
658 	const char *		plim;
659 	char			filename[FILENAME_MAX];
660 	struct stat		st;
661 	size_t			namesize;
662 	size_t			bufsize;
663 
664 	/*
665 	** Use localebuf.mon[0] to signal whether locale is already set up.
666 	*/
667 	if (localebuf.mon[0])
668 		return &localebuf;
669 	name = setlocale(LC_TIME, (char *) NULL);
670 	if (name == NULL || *name == '\0')
671 		goto no_locale;
672 	/*
673 	** If the locale name is the same as our cache, use the cache.
674 	*/
675 	lbuf = locale_buf;
676 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
677 		p = lbuf;
678 		for (ap = (const char **) &localebuf;
679 			ap < (const char **) (&localebuf + 1);
680 				++ap)
681 					*ap = p += strlen(p) + 1;
682 		return &localebuf;
683 	}
684 	/*
685 	** Slurp the locale file into the cache.
686 	*/
687 	namesize = strlen(name) + 1;
688 	if (sizeof filename <
689 		((sizeof locale_home) + namesize + (sizeof lc_time)))
690 			goto no_locale;
691 	oldsun = 0;
692 	len = snprintf(filename, sizeof filename, "%s/%s/%s", locale_home,
693 	    name, lc_time);
694 	if (len < 0 || len >= sizeof filename)
695 		goto no_locale;
696 	fd = open(filename, O_RDONLY);
697 	if (fd < 0) {
698 		/*
699 		** Old Sun systems have a different naming and data convention.
700 		*/
701 		oldsun = 1;
702 		len = snprintf(filename, sizeof filename, "%s/%s/%s",
703 			locale_home, lc_time, name);
704 		if (len < 0 || len >= sizeof filename)
705 			goto no_locale;
706 		fd = open(filename, O_RDONLY);
707 		if (fd < 0)
708 			goto no_locale;
709 	}
710 	if (fstat(fd, &st) != 0)
711 		goto bad_locale;
712 	if (st.st_size <= 0)
713 		goto bad_locale;
714 	bufsize = namesize + st.st_size;
715 	locale_buf = NULL;
716 	nlbuf = (lbuf == NULL) ? malloc(bufsize) : realloc(lbuf, bufsize);
717 	if (nlbuf == NULL) {
718 		if (lbuf)
719 			free(lbuf);
720 		lbuf = NULL;
721 		goto bad_locale;
722 	}
723 	lbuf = nlbuf;
724 	(void) strlcpy(lbuf, name, bufsize);
725 	p = lbuf + namesize;
726 	plim = p + st.st_size;
727 	if (read(fd, p, (size_t) st.st_size) != st.st_size)
728 		goto bad_lbuf;
729 	if (close(fd) != 0)
730 		goto bad_lbuf;
731 	/*
732 	** Parse the locale file into localebuf.
733 	*/
734 	if (plim[-1] != '\n')
735 		goto bad_lbuf;
736 	for (ap = (const char **) &localebuf;
737 		ap < (const char **) (&localebuf + 1);
738 			++ap) {
739 				if (p == plim)
740 					goto bad_lbuf;
741 				*ap = p;
742 				while (*p != '\n')
743 					++p;
744 				*p++ = '\0';
745 	}
746 	if (oldsun) {
747 		/*
748 		** SunOS 4 used an obsolescent format; see localdtconv(3).
749 		** c_fmt had the ``short format for dates and times together''
750 		** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
751 		** date_fmt had the ``long format for dates''
752 		** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
753 		** Discard the latter in favor of the former.
754 		*/
755 		localebuf.date_fmt = localebuf.c_fmt;
756 	}
757 	/*
758 	** Record the successful parse in the cache.
759 	*/
760 	locale_buf = lbuf;
761 
762 	return &localebuf;
763 
764 bad_lbuf:
765 	free(lbuf);
766 bad_locale:
767 	(void) close(fd);
768 no_locale:
769 	localebuf = C_time_locale;
770 	locale_buf = NULL;
771 	return &localebuf;
772 }
773 #endif /* defined LOCALE_HOME */
774