Home | History | Annotate | Line # | Download | only in time
strftime.c revision 1.1.1.3
      1 #ifndef lint
      2 #ifndef NOID
      3 static char	elsieid[] = "@(#)strftime.c	8.3";
      4 /*
      5 ** Based on the UCB version with the ID appearing below.
      6 ** This is ANSIish only when "multibyte character == plain character".
      7 */
      8 #endif /* !defined NOID */
      9 #endif /* !defined lint */
     10 
     11 #include "private.h"
     12 
     13 /*
     14 ** Copyright (c) 1989 The Regents of the University of California.
     15 ** All rights reserved.
     16 **
     17 ** Redistribution and use in source and binary forms are permitted
     18 ** provided that the above copyright notice and this paragraph are
     19 ** duplicated in all such forms and that any documentation,
     20 ** advertising materials, and other materials related to such
     21 ** distribution and use acknowledge that the software was developed
     22 ** by the University of California, Berkeley. The name of the
     23 ** University may not be used to endorse or promote products derived
     24 ** from this software without specific prior written permission.
     25 ** THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
     26 ** IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
     27 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
     28 */
     29 
     30 #ifndef LIBC_SCCS
     31 #ifndef lint
     32 static const char	sccsid[] = "@(#)strftime.c	5.4 (Berkeley) 3/14/89";
     33 #endif /* !defined lint */
     34 #endif /* !defined LIBC_SCCS */
     35 
     36 #include "tzfile.h"
     37 #include "fcntl.h"
     38 #include "locale.h"
     39 
     40 struct lc_time_T {
     41 	const char *	mon[MONSPERYEAR];
     42 	const char *	month[MONSPERYEAR];
     43 	const char *	wday[DAYSPERWEEK];
     44 	const char *	weekday[DAYSPERWEEK];
     45 	const char *	X_fmt;
     46 	const char *	x_fmt;
     47 	const char *	c_fmt;
     48 	const char *	am;
     49 	const char *	pm;
     50 	const char *	date_fmt;
     51 };
     52 
     53 #ifdef LOCALE_HOME
     54 #include "sys/stat.h"
     55 static struct lc_time_T		localebuf;
     56 static struct lc_time_T *	_loc(void);
     57 #define Locale	_loc()
     58 #endif /* defined LOCALE_HOME */
     59 #ifndef LOCALE_HOME
     60 #define Locale	(&C_time_locale)
     61 #endif /* !defined LOCALE_HOME */
     62 
     63 static const struct lc_time_T	C_time_locale = {
     64 	{
     65 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
     66 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
     67 	}, {
     68 		"January", "February", "March", "April", "May", "June",
     69 		"July", "August", "September", "October", "November", "December"
     70 	}, {
     71 		"Sun", "Mon", "Tue", "Wed",
     72 		"Thu", "Fri", "Sat"
     73 	}, {
     74 		"Sunday", "Monday", "Tuesday", "Wednesday",
     75 		"Thursday", "Friday", "Saturday"
     76 	},
     77 
     78 	/* X_fmt */
     79 	"%H:%M:%S",
     80 
     81 	/*
     82 	** x_fmt
     83 	** C99 requires this format.
     84 	** Using just numbers (as here) makes Quakers happier;
     85 	** it's also compatible with SVR4.
     86 	*/
     87 	"%m/%d/%y",
     88 
     89 	/*
     90 	** c_fmt
     91 	** C99 requires this format.
     92 	** Previously this code used "%D %X", but we now conform to C99.
     93 	** Note that
     94 	**	"%a %b %d %H:%M:%S %Y"
     95 	** is used by Solaris 2.3.
     96 	*/
     97 	"%a %b %e %T %Y",
     98 
     99 	/* am */
    100 	"AM",
    101 
    102 	/* pm */
    103 	"PM",
    104 
    105 	/* date_fmt */
    106 	"%a %b %e %H:%M:%S %Z %Y"
    107 };
    108 
    109 static char *	_add(const char *, char *, const char *);
    110 static char *	_conv(int, const char *, char *, const char *);
    111 static char *	_fmt(const char *, const struct tm *, char *, const char *,
    112 			int *);
    113 static char *	_yconv(int, int, int, int, char *, const char *);
    114 
    115 extern char *	tzname[];
    116 
    117 #ifndef YEAR_2000_NAME
    118 #define YEAR_2000_NAME	"CHECK_STRFTIME_FORMATS_FOR_TWO_DIGIT_YEARS"
    119 #endif /* !defined YEAR_2000_NAME */
    120 
    121 #define IN_NONE	0
    122 #define IN_SOME	1
    123 #define IN_THIS	2
    124 #define IN_ALL	3
    125 
    126 size_t
    127 strftime(s, maxsize, format, t)
    128 char * const		s;
    129 const size_t		maxsize;
    130 const char * const	format;
    131 const struct tm * const	t;
    132 {
    133 	char *	p;
    134 	int	warn;
    135 
    136 	tzset();
    137 #ifdef LOCALE_HOME
    138 	localebuf.mon[0] = 0;
    139 #endif /* defined LOCALE_HOME */
    140 	warn = IN_NONE;
    141 	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
    142 #ifndef NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU
    143 	if (warn != IN_NONE && getenv(YEAR_2000_NAME) != NULL) {
    144 		(void) fprintf(stderr, "\n");
    145 		if (format == NULL)
    146 			(void) fprintf(stderr, "NULL strftime format ");
    147 		else	(void) fprintf(stderr, "strftime format \"%s\" ",
    148 				format);
    149 		(void) fprintf(stderr, "yields only two digits of years in ");
    150 		if (warn == IN_SOME)
    151 			(void) fprintf(stderr, "some locales");
    152 		else if (warn == IN_THIS)
    153 			(void) fprintf(stderr, "the current locale");
    154 		else	(void) fprintf(stderr, "all locales");
    155 		(void) fprintf(stderr, "\n");
    156 	}
    157 #endif /* !defined NO_RUN_TIME_WARNINGS_ABOUT_YEAR_2000_PROBLEMS_THANK_YOU */
    158 	if (p == s + maxsize)
    159 		return 0;
    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) sprintf(buf, "%ld",
    331 							(long) mkt);
    332 					else	(void) sprintf(buf, "%lu",
    333 							(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) sprintf(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 	char *			lbuf;
    653 	char *			name;
    654 	char *			p;
    655 	const char **		ap;
    656 	const char *		plim;
    657 	char			filename[FILENAME_MAX];
    658 	struct stat		st;
    659 	size_t			namesize;
    660 	size_t			bufsize;
    661 
    662 	/*
    663 	** Use localebuf.mon[0] to signal whether locale is already set up.
    664 	*/
    665 	if (localebuf.mon[0])
    666 		return &localebuf;
    667 	name = setlocale(LC_TIME, (char *) NULL);
    668 	if (name == NULL || *name == '\0')
    669 		goto no_locale;
    670 	/*
    671 	** If the locale name is the same as our cache, use the cache.
    672 	*/
    673 	lbuf = locale_buf;
    674 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
    675 		p = lbuf;
    676 		for (ap = (const char **) &localebuf;
    677 			ap < (const char **) (&localebuf + 1);
    678 				++ap)
    679 					*ap = p += strlen(p) + 1;
    680 		return &localebuf;
    681 	}
    682 	/*
    683 	** Slurp the locale file into the cache.
    684 	*/
    685 	namesize = strlen(name) + 1;
    686 	if (sizeof filename <
    687 		((sizeof locale_home) + namesize + (sizeof lc_time)))
    688 			goto no_locale;
    689 	oldsun = 0;
    690 	(void) sprintf(filename, "%s/%s/%s", locale_home, name, lc_time);
    691 	fd = open(filename, O_RDONLY);
    692 	if (fd < 0) {
    693 		/*
    694 		** Old Sun systems have a different naming and data convention.
    695 		*/
    696 		oldsun = 1;
    697 		(void) sprintf(filename, "%s/%s/%s", locale_home,
    698 			lc_time, name);
    699 		fd = open(filename, O_RDONLY);
    700 		if (fd < 0)
    701 			goto no_locale;
    702 	}
    703 	if (fstat(fd, &st) != 0)
    704 		goto bad_locale;
    705 	if (st.st_size <= 0)
    706 		goto bad_locale;
    707 	bufsize = namesize + st.st_size;
    708 	locale_buf = NULL;
    709 	lbuf = (lbuf == NULL) ? malloc(bufsize) : realloc(lbuf, bufsize);
    710 	if (lbuf == NULL)
    711 		goto bad_locale;
    712 	(void) strcpy(lbuf, name);
    713 	p = lbuf + namesize;
    714 	plim = p + st.st_size;
    715 	if (read(fd, p, (size_t) st.st_size) != st.st_size)
    716 		goto bad_lbuf;
    717 	if (close(fd) != 0)
    718 		goto bad_lbuf;
    719 	/*
    720 	** Parse the locale file into localebuf.
    721 	*/
    722 	if (plim[-1] != '\n')
    723 		goto bad_lbuf;
    724 	for (ap = (const char **) &localebuf;
    725 		ap < (const char **) (&localebuf + 1);
    726 			++ap) {
    727 				if (p == plim)
    728 					goto bad_lbuf;
    729 				*ap = p;
    730 				while (*p != '\n')
    731 					++p;
    732 				*p++ = '\0';
    733 	}
    734 	if (oldsun) {
    735 		/*
    736 		** SunOS 4 used an obsolescent format; see localdtconv(3).
    737 		** c_fmt had the ``short format for dates and times together''
    738 		** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
    739 		** date_fmt had the ``long format for dates''
    740 		** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
    741 		** Discard the latter in favor of the former.
    742 		*/
    743 		localebuf.date_fmt = localebuf.c_fmt;
    744 	}
    745 	/*
    746 	** Record the successful parse in the cache.
    747 	*/
    748 	locale_buf = lbuf;
    749 
    750 	return &localebuf;
    751 
    752 bad_lbuf:
    753 	free(lbuf);
    754 bad_locale:
    755 	(void) close(fd);
    756 no_locale:
    757 	localebuf = C_time_locale;
    758 	locale_buf = NULL;
    759 	return &localebuf;
    760 }
    761 #endif /* defined LOCALE_HOME */
    762