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