xref: /freebsd/lib/libc/stdtime/timelocal.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 	** Not used, just compatibility placeholder.
82 	*/
83 	NULL,
84 
85 	/* am */
86 	"AM",
87 
88 	/* pm */
89 	"PM",
90 
91 	/* date_fmt */
92 	"%a %Ef %X %Z %Y",
93 
94 	{
95 		"January", "February", "March", "April", "May", "June",
96 		"July", "August", "September", "October", "November", "December"
97 	},
98 
99 	/* Ef_fmt
100 	** To determine short months / day order
101 	*/
102 	"%b %e",
103 
104 	/* EF_fmt
105 	** To determine long months / day order
106 	*/
107 	"%B %e"
108 };
109 
110 
111 int
112 __time_load_locale(const char *name)
113 {
114 	static char *		locale_buf;
115 	static char		locale_buf_C[] = "C";
116 	static int		num_lines;
117 
118 	int			fd;
119 	char *			lbuf;
120 	char *			p;
121 	const char *		plim;
122 	char                    filename[PATH_MAX];
123 	struct stat		st;
124 	size_t			namesize;
125 	size_t			bufsize;
126 	int                     save_using_locale;
127 
128 	save_using_locale = _time_using_locale;
129 	_time_using_locale = 0;
130 
131 	if (name == NULL)
132 		goto no_locale;
133 
134 	if (!strcmp(name, "C") || !strcmp(name, "POSIX"))
135 		return 0;
136 
137 	/*
138 	** If the locale name is the same as our cache, use the cache.
139 	*/
140 	lbuf = locale_buf;
141 	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
142 		set_from_buf(lbuf, num_lines);
143 		_time_using_locale = 1;
144 		return 0;
145 	}
146 	/*
147 	** Slurp the locale file into the cache.
148 	*/
149 	namesize = strlen(name) + 1;
150 
151 	if (!_PathLocale)
152 		goto no_locale;
153 	/* Range checking not needed, 'name' size is limited */
154 	strcpy(filename, _PathLocale);
155 	strcat(filename, "/");
156 	strcat(filename, name);
157 	strcat(filename, "/LC_TIME");
158 	fd = _open(filename, O_RDONLY);
159 	if (fd < 0)
160 		goto no_locale;
161 	if (fstat(fd, &st) != 0)
162 		goto bad_locale;
163 	if (st.st_size <= 0)
164 		goto bad_locale;
165 	bufsize = namesize + st.st_size;
166 	locale_buf = NULL;
167 	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
168 		malloc(bufsize) : reallocf(lbuf, bufsize);
169 	if (lbuf == NULL)
170 		goto bad_locale;
171 	(void) strcpy(lbuf, name);
172 	p = lbuf + namesize;
173 	plim = p + st.st_size;
174 	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
175 		goto bad_lbuf;
176 	if (_close(fd) != 0)
177 		goto bad_lbuf;
178 	/*
179 	** Parse the locale file into localebuf.
180 	*/
181 	if (plim[-1] != '\n')
182 		goto bad_lbuf;
183 	num_lines = split_lines(p, plim);
184 	if (num_lines >= LCTIME_SIZE_FULL)
185 		num_lines = LCTIME_SIZE_FULL;
186 	else if (num_lines >= LCTIME_SIZE_2)
187 		num_lines = LCTIME_SIZE_2;
188 	else if (num_lines >= LCTIME_SIZE_1)
189 		num_lines = LCTIME_SIZE_1;
190 	else
191 		goto reset_locale;
192 	set_from_buf(lbuf, num_lines);
193 	/*
194 	** Record the successful parse in the cache.
195 	*/
196 	locale_buf = lbuf;
197 
198 	_time_using_locale = 1;
199 	return 0;
200 
201 reset_locale:
202 	/*
203 	 * XXX - This may not be the correct thing to do in this case.
204 	 * setlocale() assumes that we left the old locale alone.
205 	 */
206 	locale_buf = locale_buf_C;
207 	_time_localebuf = _C_time_locale;
208 	save_using_locale = 0;
209 bad_lbuf:
210 	free(lbuf);
211 bad_locale:
212 	(void)_close(fd);
213 no_locale:
214 	_time_using_locale = save_using_locale;
215 	return -1;
216 }
217 
218 static int
219 split_lines(char *p, const char *plim)
220 {
221 	int i;
222 
223 	for (i = 0; p < plim; i++) {
224 		p = strchr(p, '\n');
225 		*p++ = '\0';
226 	}
227 	return i;
228 }
229 
230 static void
231 set_from_buf(const char *p, int num_lines)
232 {
233 	const char **ap;
234 	int i;
235 
236 	for (ap = (const char **) &_time_localebuf, i = 0;
237 	    i < num_lines; ++ap, ++i)
238 		*ap = p += strlen(p) + 1;
239 	if (num_lines >= LCTIME_SIZE_2)
240 		return;
241 	for (i = 0; i < 12; i++)
242 		_time_localebuf.alt_month[i] = _time_localebuf.month[i];
243 }
244