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