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