ldpart.c revision 106053
172165Sphantom/*
287658Sphantom * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
372165Sphantom * All rights reserved.
472165Sphantom *
572165Sphantom * Redistribution and use in source and binary forms, with or without
672165Sphantom * modification, are permitted provided that the following conditions
772165Sphantom * are met:
872165Sphantom * 1. Redistributions of source code must retain the above copyright
972165Sphantom *    notice, this list of conditions and the following disclaimer.
1072165Sphantom * 2. Redistributions in binary form must reproduce the above copyright
1172165Sphantom *    notice, this list of conditions and the following disclaimer in the
1272165Sphantom *    documentation and/or other materials provided with the distribution.
1372165Sphantom *
1472165Sphantom * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1572165Sphantom * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1672165Sphantom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1772165Sphantom * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1872165Sphantom * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1972165Sphantom * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2072165Sphantom * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2172165Sphantom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2272165Sphantom * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2372165Sphantom * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2472165Sphantom * SUCH DAMAGE.
2572165Sphantom */
2672165Sphantom
2792986Sobrien#include <sys/cdefs.h>
2892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/locale/ldpart.c 106053 2002-10-27 17:44:33Z wollman $");
2992986Sobrien
3072331Sphantom#include "namespace.h"
3172165Sphantom#include <sys/types.h>
3272165Sphantom#include <sys/stat.h>
33106053Swollman
34101307Sache#include <errno.h>
3572165Sphantom#include <fcntl.h>
36106053Swollman#include <limits.h>
3772165Sphantom#include <stdlib.h>
3872165Sphantom#include <string.h>
3975367Sdeischen#include <unistd.h>
4075367Sdeischen#include "un-namespace.h"
4187658Sphantom
4272165Sphantom#include "setlocale.h"
4372165Sphantom#include "ldpart.h"
4472165Sphantom
4572165Sphantomstatic int split_lines(char *, const char *);
4672165Sphantom
4772165Sphantomint
4872165Sphantom__part_load_locale(const char *name,
4988309Sphantom		int *using_locale,
5072165Sphantom		char *locale_buf,
5188309Sphantom		const char *category_filename,
5272442Sphantom		int locale_buf_size_max,
5372442Sphantom		int locale_buf_size_min,
54101307Sache		const char **dst_localebuf)
55101307Sache{
56101498Sache	int		saverr, fd, i, num_lines;
57101498Sache	char		*lbuf, *p;
58101498Sache	const char	*plim;
59101498Sache	char		filename[PATH_MAX];
60101470Sache	struct stat	st;
61101498Sache	size_t		namesize, bufsize;
6272165Sphantom
63101307Sache	/* 'name' must be already checked. */
64101498Sache	if (strcmp(name, "C") == 0 || strcmp(name, "POSIX") == 0) {
65101498Sache		*using_locale = 0;
66101498Sache		return (_LDP_CACHE);
67101498Sache	}
6872165Sphantom
6972165Sphantom	/*
7087658Sphantom	 * If the locale name is the same as our cache, use the cache.
7187658Sphantom	 */
72101498Sache	if (locale_buf != NULL && strcmp(name, locale_buf) == 0) {
7372165Sphantom		*using_locale = 1;
74101498Sache		return (_LDP_CACHE);
7572165Sphantom	}
7688309Sphantom
7772165Sphantom	/*
7887658Sphantom	 * Slurp the locale file into the cache.
7987658Sphantom	 */
8072165Sphantom	namesize = strlen(name) + 1;
8172165Sphantom
82101307Sache	/* 'PathLocale' must be already set & checked. */
8372165Sphantom	/* Range checking not needed, 'name' size is limited */
8472165Sphantom	strcpy(filename, _PathLocale);
8572165Sphantom	strcat(filename, "/");
8672165Sphantom	strcat(filename, name);
8772165Sphantom	strcat(filename, "/");
8888309Sphantom	strcat(filename, category_filename);
89101498Sache	if ((fd = _open(filename, O_RDONLY)) < 0)
90101498Sache		return (_LDP_ERROR);
9173253Sdeischen	if (_fstat(fd, &st) != 0)
9272165Sphantom		goto bad_locale;
93101307Sache	if (st.st_size <= 0) {
94101307Sache		errno = EFTYPE;
9572165Sphantom		goto bad_locale;
96101307Sache	}
9772165Sphantom	bufsize = namesize + st.st_size;
98101498Sache	if ((lbuf = malloc(bufsize)) == NULL) {
99101498Sache		errno = ENOMEM;
10072165Sphantom		goto bad_locale;
101101498Sache	}
102101470Sache	(void)strcpy(lbuf, name);
10372165Sphantom	p = lbuf + namesize;
10472165Sphantom	plim = p + st.st_size;
10572165Sphantom	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
10672165Sphantom		goto bad_lbuf;
10772165Sphantom	/*
10887658Sphantom	 * Parse the locale file into localebuf.
10987658Sphantom	 */
110101307Sache	if (plim[-1] != '\n') {
111101307Sache		errno = EFTYPE;
11272165Sphantom		goto bad_lbuf;
113101307Sache	}
11472165Sphantom	num_lines = split_lines(p, plim);
11572442Sphantom	if (num_lines >= locale_buf_size_max)
11672442Sphantom		num_lines = locale_buf_size_max;
11772442Sphantom	else if (num_lines >= locale_buf_size_min)
11872442Sphantom		num_lines = locale_buf_size_min;
119101307Sache	else {
120101307Sache		errno = EFTYPE;
121101498Sache		goto bad_lbuf;
122101307Sache	}
123101498Sache	(void)_close(fd);
12472165Sphantom	/*
12588309Sphantom	 * Record the successful parse in the cache.
12688309Sphantom	 */
127101498Sache	if (locale_buf != NULL)
128101498Sache		free(locale_buf);
12972165Sphantom	locale_buf = lbuf;
130101498Sache	for (p = locale_buf, i = 0; i < num_lines; i++)
131101498Sache		dst_localebuf[i] = (p += strlen(p) + 1);
132101498Sache	for (i = num_lines; i < locale_buf_size_max; i++)
133101498Sache		dst_localebuf[i] = NULL;
13472165Sphantom	*using_locale = 1;
135101470Sache
136101498Sache	return (_LDP_LOADED);
13772165Sphantom
13872165Sphantombad_lbuf:
139101498Sache	saverr = errno;
140101498Sache	free(lbuf);
141101470Sache	errno = saverr;
14272165Sphantombad_locale:
143101498Sache	saverr = errno;
144101498Sache	(void)_close(fd);
145101470Sache	errno = saverr;
146101470Sache
147101498Sache	return (_LDP_ERROR);
14872165Sphantom}
14972165Sphantom
15072165Sphantomstatic int
151101498Sachesplit_lines(char *p, const char *plim)
152101470Sache{
15372165Sphantom	int i;
15472165Sphantom
15572165Sphantom	for (i = 0; p < plim; i++) {
15672165Sphantom		p = strchr(p, '\n');
15772165Sphantom		*p++ = '\0';
15872165Sphantom	}
159101470Sache	return (i);
16072165Sphantom}
16172165Sphantom
162