ldpart.c revision 72165
1/*
2 * Copyright (c) 2000, 2001 Alexey Zelkin
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/locale/ldpart.c 72165 2001-02-08 16:58:53Z phantom $
27 */
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/syslimits.h>
32#include <fcntl.h>
33#include <stdlib.h>
34#include <string.h>
35#include "setlocale.h"
36#include "ldpart.h"
37
38static int split_lines(char *, const char *);
39static void set_from_buf(const char *, int, const char **);
40
41int
42__part_load_locale(const char *name,
43		int* using_locale,
44		char *locale_buf,
45		const char *category_name,
46		int locale_buf_size,
47		const char **dst_localebuf) {
48
49	static char		locale_buf_C[] = "C";
50	static int		num_lines;
51
52	int			fd;
53	char *			lbuf;
54	char *			p;
55	const char *		plim;
56	char                    filename[PATH_MAX];
57	struct stat		st;
58	size_t			namesize;
59	size_t			bufsize;
60	int                     save_using_locale;
61
62	save_using_locale = *using_locale;
63	*using_locale = 0;
64
65	if (name == NULL)
66		goto no_locale;
67
68	if (!strcmp(name, "C") || !strcmp(name, "POSIX"))
69		return 0;
70
71	/*
72	** If the locale name is the same as our cache, use the cache.
73	*/
74	lbuf = locale_buf;
75	if (lbuf != NULL && strcmp(name, lbuf) == 0) {
76		set_from_buf(lbuf, num_lines, dst_localebuf);
77		*using_locale = 1;
78		return 0;
79	}
80	/*
81	** Slurp the locale file into the cache.
82	*/
83	namesize = strlen(name) + 1;
84
85	if (!_PathLocale)
86		goto no_locale;
87	/* Range checking not needed, 'name' size is limited */
88	strcpy(filename, _PathLocale);
89	strcat(filename, "/");
90	strcat(filename, name);
91	strcat(filename, "/");
92	strcat(filename, category_name);
93	fd = _open(filename, O_RDONLY);
94	if (fd < 0)
95		goto no_locale;
96	if (fstat(fd, &st) != 0)
97		goto bad_locale;
98	if (st.st_size <= 0)
99		goto bad_locale;
100	bufsize = namesize + st.st_size;
101	locale_buf = NULL;
102	lbuf = (lbuf == NULL || lbuf == locale_buf_C) ?
103		malloc(bufsize) : reallocf(lbuf, bufsize);
104	if (lbuf == NULL)
105		goto bad_locale;
106	(void) strcpy(lbuf, name);
107	p = lbuf + namesize;
108	plim = p + st.st_size;
109	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
110		goto bad_lbuf;
111	if (_close(fd) != 0)
112		goto bad_lbuf;
113	/*
114	** Parse the locale file into localebuf.
115	*/
116	if (plim[-1] != '\n')
117		goto bad_lbuf;
118	num_lines = split_lines(p, plim);
119	if (num_lines >= locale_buf_size)
120		num_lines = locale_buf_size;
121	else
122		goto reset_locale;
123	set_from_buf(lbuf, num_lines, dst_localebuf);
124	/*
125	** Record the successful parse in the cache.
126	*/
127	locale_buf = lbuf;
128
129	*using_locale = 1;
130	return 0;
131
132reset_locale:
133	locale_buf = locale_buf_C;
134	save_using_locale = 0;
135bad_lbuf:
136	free(lbuf);
137bad_locale:
138	(void)_close(fd);
139no_locale:
140	*using_locale = save_using_locale;
141	return -1;
142}
143
144static int
145split_lines(char *p, const char *plim)
146{
147	int i;
148
149	for (i = 0; p < plim; i++) {
150		p = strchr(p, '\n');
151		*p++ = '\0';
152	}
153	return i;
154}
155
156static void
157set_from_buf(const char *p, int num_lines, const char **dst_localebuf)
158{
159	const char **ap;
160	int i;
161
162	for (ap = dst_localebuf, i = 0; i < num_lines; ++ap, ++i)
163		*ap = p += strlen(p) + 1;
164}
165
166