timelocal.c revision 28021
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 *	$Id$
27 */
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/syslimits.h>
32#include <fcntl.h>
33#include <locale.h>
34#include <stdlib.h>
35#include <string.h>
36#include "setlocale.h"
37#include "timelocal.h"
38
39struct lc_time_T _time_localebuf;
40int _time_using_locale;
41
42const struct lc_time_T	_C_time_locale = {
43	{
44		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
45		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
46	}, {
47		"January", "February", "March", "April", "May", "June",
48		"July", "August", "September", "October", "November", "December"
49	}, {
50		"Sun", "Mon", "Tue", "Wed",
51		"Thu", "Fri", "Sat"
52	}, {
53		"Sunday", "Monday", "Tuesday", "Wednesday",
54		"Thursday", "Friday", "Saturday"
55	},
56
57	/* X_fmt */
58	"%H:%M:%S",
59
60	/*
61	** x_fmt
62	** Since the C language standard calls for
63	** "date, using locale's date format," anything goes.
64	** Using just numbers (as here) makes Quakers happier;
65	** it's also compatible with SVR4.
66	*/
67	"%m/%d/%y",
68
69	/*
70	** c_fmt (ctime-compatible)
71	** Note that
72	**	"%a %b %d %H:%M:%S %Y"
73	** is used by Solaris 2.3.
74	*/
75	"%a %b %e %X %Y",
76
77	/* am */
78	"AM",
79
80	/* pm */
81	"PM",
82
83	/* date_fmt */
84	"%a %b %e %X %Z %Y"
85};
86
87
88int
89__time_load_locale(const char *name)
90{
91	static char *		locale_buf;
92	static char		locale_buf_C[] = "C";
93
94	int			fd;
95	char *			lbuf;
96	char *			p;
97	const char **		ap;
98	const char *		plim;
99	char                    filename[PATH_MAX];
100	struct stat		st;
101	size_t			namesize;
102	size_t			bufsize;
103	int                     save_using_locale;
104
105	save_using_locale = _time_using_locale;
106	_time_using_locale = 0;
107
108	if (name == NULL)
109		goto no_locale;
110
111	if (!strcmp(name, "C") || !strcmp(name, "POSIX"))
112		return 0;
113
114	/*
115	** If the locale name is the same as our cache, use the cache.
116	*/
117	lbuf = locale_buf;
118	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
119		p = lbuf;
120		for (ap = (const char **) &_time_localebuf;
121			ap < (const char **) (&_time_localebuf + 1);
122				++ap)
123					*ap = p += strlen(p) + 1;
124		_time_using_locale = 1;
125		return 0;
126	}
127	/*
128	** Slurp the locale file into the cache.
129	*/
130	namesize = strlen(name) + 1;
131
132	if (!_PathLocale)
133		goto no_locale;
134	/* Range checking not needed, 'name' size is limited */
135	strcpy(filename, _PathLocale);
136	strcat(filename, "/");
137	strcat(filename, name);
138	strcat(filename, "/LC_TIME");
139	fd = open(filename, O_RDONLY);
140	if (fd < 0)
141		goto no_locale;
142	if (fstat(fd, &st) != 0)
143		goto bad_locale;
144	if (st.st_size <= 0)
145		goto bad_locale;
146	bufsize = namesize + st.st_size;
147	locale_buf = NULL;
148	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
149		malloc(bufsize) : realloc(lbuf, bufsize);
150	if (lbuf == NULL)
151		goto bad_locale;
152	(void) strcpy(lbuf, name);
153	p = lbuf + namesize;
154	plim = p + st.st_size;
155	if (read(fd, p, (size_t) st.st_size) != st.st_size)
156		goto bad_lbuf;
157	if (close(fd) != 0)
158		goto bad_lbuf;
159	/*
160	** Parse the locale file into localebuf.
161	*/
162	if (plim[-1] != '\n')
163		goto bad_lbuf;
164	for (ap = (const char **) &_time_localebuf;
165		ap < (const char **) (&_time_localebuf + 1);
166			++ap) {
167				if (p == plim)
168					goto reset_locale;
169				*ap = p;
170				while (*p != '\n')
171					++p;
172				*p++ = '\0';
173	}
174	/*
175	** Record the successful parse in the cache.
176	*/
177	locale_buf = lbuf;
178
179	_time_using_locale = 1;
180	return 0;
181
182reset_locale:
183	/*
184	 * XXX - This may not be the correct thing to do in this case.
185	 * setlocale() assumes that we left the old locale alone.
186	 */
187	locale_buf = locale_buf_C;
188	_time_localebuf = _C_time_locale;
189	save_using_locale = 0;
190bad_lbuf:
191	free(lbuf);
192bad_locale:
193	(void) close(fd);
194no_locale:
195	_time_using_locale = save_using_locale;
196	return -1;
197}
198