GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/time/strftime.c Lines: 72 184 39.1 %
Date: 2017-11-07 Branches: 40 129 31.0 %

Line Branch Exec Source
1
/*	$OpenBSD: strftime.c,v 1.30 2016/09/21 04:38:57 guenther 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
498670
	int	warn;
125
126
249335
	tzset();
127
#ifdef LOCALE_HOME
128
	localebuf.mon[0] = 0;
129
#endif /* defined LOCALE_HOME */
130
249335
	warn = IN_NONE;
131
249335
	p = _fmt(((format == NULL) ? "%c" : format), t, s, s + maxsize, &warn);
132
249335
	if (p == s + maxsize) {
133
		if (maxsize > 0)
134
			s[maxsize - 1] = '\0';
135
		return 0;
136
	}
137
249335
	*p = '\0';
138
249335
	return p - s;
139
249335
}
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
7415735
	for ( ; *format; ++format) {
146
2967151
		if (*format == '%') {
147
label:
148











4697495
			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

968
				pt = _add((t->tm_wday < 0 ||
160
242
					t->tm_wday >= DAYSPERWEEK) ?
161
242
					"?" : Locale->wday[t->tm_wday],
162
					pt, ptlim);
163
242
				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

996476
				pt = _add((t->tm_mon < 0 ||
173
249119
					t->tm_mon >= MONSPERYEAR) ?
174
249119
					"?" : Locale->mon[t->tm_mon],
175
					pt, ptlim);
176
249119
				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
172
				int warn2 = IN_SOME;
191
192
172
				pt = _fmt(Locale->c_fmt, t, pt, ptlim, &warn2);
193
172
				if (warn2 == IN_ALL)
194
					warn2 = IN_THIS;
195
172
				if (warn2 > *warnp)
196
172
					*warnp = warn2;
197
172
				}
198
172
				continue;
199
			case 'D':
200
				pt = _fmt("%m/%d/%y", t, pt, ptlim, warnp);
201
				continue;
202
			case 'd':
203
44
				pt = _conv(t->tm_mday, "%02d", pt, ptlim);
204
44
				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
249119
				pt = _conv(t->tm_mday, "%2d", pt, ptlim);
219
249119
				continue;
220
			case 'F':
221
				pt = _fmt("%Y-%m-%d", t, pt, ptlim, warnp);
222
				continue;
223
			case 'H':
224
249163
				pt = _conv(t->tm_hour, "%02d", pt, ptlim);
225
249163
				continue;
226
			case 'I':
227
150
				pt = _conv((t->tm_hour % 12) ?
228
					(t->tm_hour % 12) : 12,
229
					"%02d", pt, ptlim);
230
50
				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
366
				pt = _conv((t->tm_hour % 12) ?
266
					(t->tm_hour % 12) : 12,
267
					"%2d", pt, ptlim);
268
122
				continue;
269
			case 'M':
270
249285
				pt = _conv(t->tm_min, "%02d", pt, ptlim);
271
249285
				continue;
272
			case 'm':
273
44
				pt = _conv(t->tm_mon + 1, "%02d", pt, ptlim);
274
44
				continue;
275
			case 'n':
276
				pt = _add("\n", pt, ptlim);
277
				continue;
278
			case 'p':
279
172
				pt = _add((t->tm_hour >= (HOURSPERDAY / 2)) ?
280
					Locale->pm :
281
					Locale->am,
282
					pt, ptlim);
283
172
				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
244368
				pt = _conv(t->tm_sec, "%02d", pt, ptlim);
292
244368
				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
244304
				pt = _fmt("%H:%M:%S", t, pt, ptlim, warnp);
309
244304
				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
244368
				pt = _yconv(t->tm_year, TM_YEAR_BASE, 1, 1,
456
					pt, ptlim);
457
244368
				continue;
458
			case 'Z':
459
#ifdef TM_ZONE
460
20
				if (t->TM_ZONE != NULL)
461
20
					pt = _add(t->TM_ZONE, pt, ptlim);
462
				else
463
#endif /* defined TM_ZONE */
464
				if (t->tm_isdst >= 0)
465
					pt = _add(tzname[t->tm_isdst != 0],
466
						pt, ptlim);
467
				/*
468
				** C99 says that %Z must be replaced by the
469
				** empty string if the time zone is not
470
				** determinable.
471
				*/
472
				continue;
473
			case 'z':
474
				{
475
				int		diff;
476
				char const *	sign;
477
478
				if (t->tm_isdst < 0)
479
					continue;
480
#ifdef TM_GMTOFF
481
				diff = t->TM_GMTOFF;
482
#else /* !defined TM_GMTOFF */
483
				/*
484
				** C99 says that the UTC offset must
485
				** be computed by looking only at
486
				** tm_isdst. This requirement is
487
				** incorrect, since it means the code
488
				** must rely on magic (in this case
489
				** altzone and timezone), and the
490
				** magic might not have the correct
491
				** offset. Doing things correctly is
492
				** tricky and requires disobeying C99;
493
				** see GNU C strftime for details.
494
				** For now, punt and conform to the
495
				** standard, even though it's incorrect.
496
				**
497
				** C99 says that %z must be replaced by the
498
				** empty string if the time zone is not
499
				** determinable, so output nothing if the
500
				** appropriate variables are not available.
501
				*/
502
				if (t->tm_isdst == 0)
503
#ifdef USG_COMPAT
504
					diff = -timezone;
505
#else /* !defined USG_COMPAT */
506
					continue;
507
#endif /* !defined USG_COMPAT */
508
				else
509
#ifdef ALTZONE
510
					diff = -altzone;
511
#else /* !defined ALTZONE */
512
					continue;
513
#endif /* !defined ALTZONE */
514
#endif /* !defined TM_GMTOFF */
515
				if (diff < 0) {
516
					sign = "-";
517
					diff = -diff;
518
				} else	sign = "+";
519
				pt = _add(sign, pt, ptlim);
520
				diff /= SECSPERMIN;
521
				diff = (diff / MINSPERHOUR) * 100 +
522
					(diff % MINSPERHOUR);
523
				pt = _conv(diff, "%04d", pt, ptlim);
524
				}
525
				continue;
526
			case '+':
527
				pt = _fmt(Locale->date_fmt, t, pt, ptlim,
528
					warnp);
529
				continue;
530
			case '%':
531
			/*
532
			** X311J/88-090 (4.12.3.5): if conversion char is
533
			** undefined, behavior is undefined. Print out the
534
			** character itself as printf(3) also does.
535
			*/
536
			default:
537
				break;
538
			}
539
		}
540
1236559
		if (pt == ptlim)
541
			break;
542
1236559
		*pt++ = *format;
543
1236559
	}
544
493811
	return pt;
545
}
546
547
static char *
548
_conv(int n, const char *format, char *pt, const char *ptlim)
549
{
550
2961862
	char	buf[INT_STRLEN_MAXIMUM(int) + 1];
551
552
1480931
	(void) snprintf(buf, sizeof buf, format, n);
553
2961862
	return _add(buf, pt, ptlim);
554
1480931
}
555
556
static char *
557
_add(const char *str, char *pt, const char *ptlim)
558
{
559

23493816
	while (pt < ptlim && (*pt = *str++) != '\0')
560
3710349
		++pt;
561
1730484
	return pt;
562
}
563
564
/*
565
** POSIX and the C Standard are unclear or inconsistent about
566
** what %C and %y do if the year is negative or exceeds 9999.
567
** Use the convention that %C concatenated with %y yields the
568
** same output as %Y, and that %Y contains at least 4 bytes,
569
** with more only if necessary.
570
*/
571
572
static char *
573
_yconv(int a, int b, int convert_top, int convert_yy, char *pt, const char *ptlim)
574
{
575
	int	lead;
576
	int	trail;
577
578
#define DIVISOR	100
579
488736
	trail = a % DIVISOR + b % DIVISOR;
580
244368
	lead = a / DIVISOR + b / DIVISOR + trail / DIVISOR;
581
244368
	trail %= DIVISOR;
582
244368
	if (trail < 0 && lead > 0) {
583
		trail += DIVISOR;
584
		--lead;
585
244368
	} else if (lead < 0 && trail > 0) {
586
		trail -= DIVISOR;
587
		++lead;
588
	}
589
244368
	if (convert_top) {
590
244368
		if (lead == 0 && trail < 0)
591
			pt = _add("-0", pt, ptlim);
592
244368
		else	pt = _conv(lead, "%02d", pt, ptlim);
593
	}
594
244368
	if (convert_yy)
595
244368
		pt = _conv(((trail < 0) ? -trail : trail), "%02d", pt, ptlim);
596
244368
	return pt;
597
}
598
599
#ifdef LOCALE_HOME
600
static struct lc_time_T *
601
_loc(void)
602
{
603
	static const char	locale_home[] = LOCALE_HOME;
604
	static const char	lc_time[] = "LC_TIME";
605
	static char *		locale_buf;
606
607
	int			fd;
608
	int			oldsun;	/* "...ain't got nothin' to do..." */
609
	int			len;
610
	char *			lbuf;
611
	char *			nlbuf;
612
	char *			name;
613
	char *			p;
614
	const char **		ap;
615
	const char *		plim;
616
	char			filename[PATH_MAX];
617
	struct stat		st;
618
	size_t			namesize;
619
	size_t			bufsize;
620
621
	/*
622
	** Use localebuf.mon[0] to signal whether locale is already set up.
623
	*/
624
	if (localebuf.mon[0])
625
		return &localebuf;
626
	name = setlocale(LC_TIME, (char *) NULL);
627
	if (name == NULL || *name == '\0')
628
		goto no_locale;
629
	/*
630
	** If the locale name is the same as our cache, use the cache.
631
	*/
632
	lbuf = locale_buf;
633
	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
634
		p = lbuf;
635
		for (ap = (const char **) &localebuf;
636
			ap < (const char **) (&localebuf + 1);
637
				++ap)
638
					*ap = p += strlen(p) + 1;
639
		return &localebuf;
640
	}
641
	/*
642
	** Slurp the locale file into the cache.
643
	*/
644
	namesize = strlen(name) + 1;
645
	if (sizeof filename <
646
		((sizeof locale_home) + namesize + (sizeof lc_time)))
647
			goto no_locale;
648
	oldsun = 0;
649
	len = snprintf(filename, sizeof filename, "%s/%s/%s", locale_home,
650
	    name, lc_time);
651
	if (len < 0 || len >= sizeof filename)
652
		goto no_locale;
653
	fd = open(filename, O_RDONLY);
654
	if (fd < 0) {
655
		/*
656
		** Old Sun systems have a different naming and data convention.
657
		*/
658
		oldsun = 1;
659
		len = snprintf(filename, sizeof filename, "%s/%s/%s",
660
			locale_home, lc_time, name);
661
		if (len < 0 || len >= sizeof filename)
662
			goto no_locale;
663
		fd = open(filename, O_RDONLY);
664
		if (fd < 0)
665
			goto no_locale;
666
	}
667
	if (fstat(fd, &st) != 0)
668
		goto bad_locale;
669
	if (st.st_size <= 0)
670
		goto bad_locale;
671
	bufsize = namesize + st.st_size;
672
	locale_buf = NULL;
673
	nlbuf = realloc(lbuf, bufsize);
674
	if (nlbuf == NULL) {
675
		free(lbuf);
676
		lbuf = NULL;
677
		goto bad_locale;
678
	}
679
	lbuf = nlbuf;
680
	(void) strlcpy(lbuf, name, bufsize);
681
	p = lbuf + namesize;
682
	plim = p + st.st_size;
683
	if (read(fd, p, st.st_size) != st.st_size)
684
		goto bad_lbuf;
685
	if (close(fd) != 0)
686
		goto bad_lbuf;
687
	/*
688
	** Parse the locale file into localebuf.
689
	*/
690
	if (plim[-1] != '\n')
691
		goto bad_lbuf;
692
	for (ap = (const char **) &localebuf;
693
		ap < (const char **) (&localebuf + 1);
694
			++ap) {
695
				if (p == plim)
696
					goto bad_lbuf;
697
				*ap = p;
698
				while (*p != '\n')
699
					++p;
700
				*p++ = '\0';
701
	}
702
	if (oldsun) {
703
		/*
704
		** SunOS 4 used an obsolescent format; see localdtconv(3).
705
		** c_fmt had the ``short format for dates and times together''
706
		** (SunOS 4 date, "%a %b %e %T %Z %Y" in the C locale);
707
		** date_fmt had the ``long format for dates''
708
		** (SunOS 4 strftime %C, "%A, %B %e, %Y" in the C locale).
709
		** Discard the latter in favor of the former.
710
		*/
711
		localebuf.date_fmt = localebuf.c_fmt;
712
	}
713
	/*
714
	** Record the successful parse in the cache.
715
	*/
716
	locale_buf = lbuf;
717
718
	return &localebuf;
719
720
bad_lbuf:
721
	free(lbuf);
722
bad_locale:
723
	(void) close(fd);
724
no_locale:
725
	localebuf = C_time_locale;
726
	locale_buf = NULL;
727
	return &localebuf;
728
}
729
#endif /* defined LOCALE_HOME */