timelocal.c revision 71579
1/*-
2 * Copyright (c) 1997 FreeBSD Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/lib/libc/stdtime/timelocal.c 71579 2001-01-24 13:01:12Z deischen $
27 */
28
29#include "namespace.h"
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/syslimits.h>
33#include <fcntl.h>
34#include <locale.h>
35#include <stddef.h>
36#include <stdlib.h>
37#include <string.h>
38#include <unistd.h>
39#include "un-namespace.h"
40
41#include "setlocale.h"
42#include "timelocal.h"
43
44static int split_lines(char *, const char *);
45static void set_from_buf(const char *, int);
46
47struct lc_time_T _time_localebuf;
48int _time_using_locale;
49
50#define	LCTIME_SIZE_FULL (sizeof(struct lc_time_T) / sizeof(char *))
51#define	LCTIME_SIZE_1 \
52	(offsetof(struct lc_time_T, alt_month[0]) / sizeof(char *))
53#define LCTIME_SIZE_2 \
54	(offsetof(struct lc_time_T, Ef_fmt) / sizeof(char *))
55
56const struct lc_time_T	_C_time_locale = {
57	{
58		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
59		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
60	}, {
61		"January", "February", "March", "April", "May", "June",
62		"July", "August", "September", "October", "November", "December"
63	}, {
64		"Sun", "Mon", "Tue", "Wed",
65		"Thu", "Fri", "Sat"
66	}, {
67		"Sunday", "Monday", "Tuesday", "Wednesday",
68		"Thursday", "Friday", "Saturday"
69	},
70
71	/* X_fmt */
72	"%H:%M:%S",
73
74	/*
75	** x_fmt
76	** Since the C language standard calls for
77	** "date, using locale's date format," anything goes.
78	** Using just numbers (as here) makes Quakers happier;
79	** it's also compatible with SVR4.
80	*/
81	"%m/%d/%y",
82
83	/*
84	** c_fmt (ctime-compatible)
85	** Not used, just compatibility placeholder.
86	*/
87	NULL,
88
89	/* am */
90	"AM",
91
92	/* pm */
93	"PM",
94
95	/* date_fmt */
96	"%a %Ef %X %Z %Y",
97
98	{
99		"January", "February", "March", "April", "May", "June",
100		"July", "August", "September", "October", "November", "December"
101	},
102
103	/* Ef_fmt
104	** To determine short months / day order
105	*/
106	"%b %e",
107
108	/* EF_fmt
109	** To determine long months / day order
110	*/
111	"%B %e"
112};
113
114
115int
116__time_load_locale(const char *name)
117{
118	static char *		locale_buf;
119	static char		locale_buf_C[] = "C";
120	static int		num_lines;
121
122	int			fd;
123	char *			lbuf;
124	char *			p;
125	const char *		plim;
126	char                    filename[PATH_MAX];
127	struct stat		st;
128	size_t			namesize;
129	size_t			bufsize;
130	int                     save_using_locale;
131
132	save_using_locale = _time_using_locale;
133	_time_using_locale = 0;
134
135	if (name == NULL)
136		goto no_locale;
137
138	if (!strcmp(name, "C") || !strcmp(name, "POSIX"))
139		return 0;
140
141	/*
142	** If the locale name is the same as our cache, use the cache.
143	*/
144	lbuf = locale_buf;
145	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
146		set_from_buf(lbuf, num_lines);
147		_time_using_locale = 1;
148		return 0;
149	}
150	/*
151	** Slurp the locale file into the cache.
152	*/
153	namesize = strlen(name) + 1;
154
155	if (!_PathLocale)
156		goto no_locale;
157	/* Range checking not needed, 'name' size is limited */
158	strcpy(filename, _PathLocale);
159	strcat(filename, "/");
160	strcat(filename, name);
161	strcat(filename, "/LC_TIME");
162	fd = _open(filename, O_RDONLY);
163	if (fd < 0)
164		goto no_locale;
165	if (_fstat(fd, &st) != 0)
166		goto bad_locale;
167	if (st.st_size <= 0)
168		goto bad_locale;
169	bufsize = namesize + st.st_size;
170	locale_buf = NULL;
171	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
172		malloc(bufsize) : reallocf(lbuf, bufsize);
173	if (lbuf == NULL)
174		goto bad_locale;
175	(void) strcpy(lbuf, name);
176	p = lbuf + namesize;
177	plim = p + st.st_size;
178	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
179		goto bad_lbuf;
180	if (_close(fd) != 0)
181		goto bad_lbuf;
182	/*
183	** Parse the locale file into localebuf.
184	*/
185	if (plim[-1] != '\n')
186		goto bad_lbuf;
187	num_lines = split_lines(p, plim);
188	if (num_lines >= LCTIME_SIZE_FULL)
189		num_lines = LCTIME_SIZE_FULL;
190	else if (num_lines >= LCTIME_SIZE_2)
191		num_lines = LCTIME_SIZE_2;
192	else if (num_lines >= LCTIME_SIZE_1)
193		num_lines = LCTIME_SIZE_1;
194	else
195		goto reset_locale;
196	set_from_buf(lbuf, num_lines);
197	/*
198	** Record the successful parse in the cache.
199	*/
200	locale_buf = lbuf;
201
202	_time_using_locale = 1;
203	return 0;
204
205reset_locale:
206	/*
207	 * XXX - This may not be the correct thing to do in this case.
208	 * setlocale() assumes that we left the old locale alone.
209	 */
210	locale_buf = locale_buf_C;
211	_time_localebuf = _C_time_locale;
212	save_using_locale = 0;
213bad_lbuf:
214	free(lbuf);
215bad_locale:
216	(void)_close(fd);
217no_locale:
218	_time_using_locale = save_using_locale;
219	return -1;
220}
221
222static int
223split_lines(char *p, const char *plim)
224{
225	int i;
226
227	for (i = 0; p < plim; i++) {
228		p = strchr(p, '\n');
229		*p++ = '\0';
230	}
231	return i;
232}
233
234static void
235set_from_buf(const char *p, int num_lines)
236{
237	const char **ap;
238	int i;
239
240	for (ap = (const char **) &_time_localebuf, i = 0;
241	    i < num_lines; ++ap, ++i)
242		*ap = p += strlen(p) + 1;
243	if (num_lines >= LCTIME_SIZE_2)
244		return;
245	for (i = 0; i < 12; i++)
246		_time_localebuf.alt_month[i] = _time_localebuf.month[i];
247}
248