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