xref: /freebsd/lib/libc/stdtime/timelocal.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
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$
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 <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "setlocale.h"
38 #include "timelocal.h"
39 
40 static int split_lines(char *, const char *);
41 static void set_from_buf(const char *, int);
42 
43 struct lc_time_T _time_localebuf;
44 int _time_using_locale;
45 
46 #define	LCTIME_SIZE_FULL (sizeof(struct lc_time_T) / sizeof(char *))
47 #define	LCTIME_SIZE_1 \
48 	(offsetof(struct lc_time_T, alt_month[0]) / sizeof(char *))
49 #define LCTIME_SIZE_2 \
50 	(offsetof(struct lc_time_T, Ef_fmt) / sizeof(char *))
51 
52 const struct lc_time_T	_C_time_locale = {
53 	{
54 		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
55 		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
56 	}, {
57 		"January", "February", "March", "April", "May", "June",
58 		"July", "August", "September", "October", "November", "December"
59 	}, {
60 		"Sun", "Mon", "Tue", "Wed",
61 		"Thu", "Fri", "Sat"
62 	}, {
63 		"Sunday", "Monday", "Tuesday", "Wednesday",
64 		"Thursday", "Friday", "Saturday"
65 	},
66 
67 	/* X_fmt */
68 	"%H:%M:%S",
69 
70 	/*
71 	** x_fmt
72 	** Since the C language standard calls for
73 	** "date, using locale's date format," anything goes.
74 	** Using just numbers (as here) makes Quakers happier;
75 	** it's also compatible with SVR4.
76 	*/
77 	"%m/%d/%y",
78 
79 	/*
80 	** c_fmt (ctime-compatible)
81 	** Note that
82 	**	"%a %b %d %H:%M:%S %Y"
83 	** is used by Solaris 2.3.
84 	*/
85 	"%a %Ef %X %Y",
86 
87 	/* am */
88 	"AM",
89 
90 	/* pm */
91 	"PM",
92 
93 	/* date_fmt */
94 	"%a %Ef %X %Z %Y",
95 
96 	{
97 		"January", "February", "March", "April", "May", "June",
98 		"July", "August", "September", "October", "November", "December"
99 	},
100 
101 	/* Ef_fmt
102 	** To determine short months / day order
103 	*/
104 	"%b %e",
105 
106 	/* EF_fmt
107 	** To determine long months / day order
108 	*/
109 	"%B %e"
110 };
111 
112 
113 int
114 __time_load_locale(const char *name)
115 {
116 	static char *		locale_buf;
117 	static char		locale_buf_C[] = "C";
118 	static int		num_lines;
119 
120 	int			fd;
121 	char *			lbuf;
122 	char *			p;
123 	const char *		plim;
124 	char                    filename[PATH_MAX];
125 	struct stat		st;
126 	size_t			namesize;
127 	size_t			bufsize;
128 	int                     save_using_locale;
129 
130 	save_using_locale = _time_using_locale;
131 	_time_using_locale = 0;
132 
133 	if (name == NULL)
134 		goto no_locale;
135 
136 	if (!strcmp(name, "C") || !strcmp(name, "POSIX"))
137 		return 0;
138 
139 	/*
140 	** If the locale name is the same as our cache, use the cache.
141 	*/
142 	lbuf = locale_buf;
143 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
144 		set_from_buf(lbuf, num_lines);
145 		_time_using_locale = 1;
146 		return 0;
147 	}
148 	/*
149 	** Slurp the locale file into the cache.
150 	*/
151 	namesize = strlen(name) + 1;
152 
153 	if (!_PathLocale)
154 		goto no_locale;
155 	/* Range checking not needed, 'name' size is limited */
156 	strcpy(filename, _PathLocale);
157 	strcat(filename, "/");
158 	strcat(filename, name);
159 	strcat(filename, "/LC_TIME");
160 	fd = _open(filename, O_RDONLY);
161 	if (fd < 0)
162 		goto no_locale;
163 	if (fstat(fd, &st) != 0)
164 		goto bad_locale;
165 	if (st.st_size <= 0)
166 		goto bad_locale;
167 	bufsize = namesize + st.st_size;
168 	locale_buf = NULL;
169 	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
170 		malloc(bufsize) : reallocf(lbuf, bufsize);
171 	if (lbuf == NULL)
172 		goto bad_locale;
173 	(void) strcpy(lbuf, name);
174 	p = lbuf + namesize;
175 	plim = p + st.st_size;
176 	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
177 		goto bad_lbuf;
178 	if (_close(fd) != 0)
179 		goto bad_lbuf;
180 	/*
181 	** Parse the locale file into localebuf.
182 	*/
183 	if (plim[-1] != '\n')
184 		goto bad_lbuf;
185 	num_lines = split_lines(p, plim);
186 	if (num_lines >= LCTIME_SIZE_FULL)
187 		num_lines = LCTIME_SIZE_FULL;
188 	else if (num_lines >= LCTIME_SIZE_2)
189 		num_lines = LCTIME_SIZE_2;
190 	else if (num_lines >= LCTIME_SIZE_1)
191 		num_lines = LCTIME_SIZE_1;
192 	else
193 		goto reset_locale;
194 	set_from_buf(lbuf, num_lines);
195 	/*
196 	** Record the successful parse in the cache.
197 	*/
198 	locale_buf = lbuf;
199 
200 	_time_using_locale = 1;
201 	return 0;
202 
203 reset_locale:
204 	/*
205 	 * XXX - This may not be the correct thing to do in this case.
206 	 * setlocale() assumes that we left the old locale alone.
207 	 */
208 	locale_buf = locale_buf_C;
209 	_time_localebuf = _C_time_locale;
210 	save_using_locale = 0;
211 bad_lbuf:
212 	free(lbuf);
213 bad_locale:
214 	(void)_close(fd);
215 no_locale:
216 	_time_using_locale = save_using_locale;
217 	return -1;
218 }
219 
220 static int
221 split_lines(char *p, const char *plim)
222 {
223 	int i;
224 
225 	for (i = 0; p < plim; i++) {
226 		p = strchr(p, '\n');
227 		*p++ = '\0';
228 	}
229 	return i;
230 }
231 
232 static void
233 set_from_buf(const char *p, int num_lines)
234 {
235 	const char **ap;
236 	int i;
237 
238 	for (ap = (const char **) &_time_localebuf, i = 0;
239 	    i < num_lines; ++ap, ++i)
240 		*ap = p += strlen(p) + 1;
241 	if (num_lines == LCTIME_SIZE_FULL)
242 		return;
243 	for (i = 0; i < 12; i++)
244 		_time_localebuf.alt_month[i] = _time_localebuf.month[i];
245 }
246