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$");
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
42116875Sphantom#include "ldpart.h"
4372165Sphantom#include "setlocale.h"
4472165Sphantom
4572165Sphantomstatic int split_lines(char *, const char *);
4672165Sphantom
4772165Sphantomint
4872165Sphantom__part_load_locale(const char *name,
4988309Sphantom		int *using_locale,
50116274Sjkh		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	 */
72116274Sjkh	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. */
83116875Sphantom
8472165Sphantom	/* Range checking not needed, 'name' size is limited */
8572165Sphantom	strcpy(filename, _PathLocale);
8672165Sphantom	strcat(filename, "/");
8772165Sphantom	strcat(filename, name);
8872165Sphantom	strcat(filename, "/");
8988309Sphantom	strcat(filename, category_filename);
90241046Sjilles	if ((fd = _open(filename, O_RDONLY | O_CLOEXEC)) < 0)
91101498Sache		return (_LDP_ERROR);
9273253Sdeischen	if (_fstat(fd, &st) != 0)
9372165Sphantom		goto bad_locale;
94101307Sache	if (st.st_size <= 0) {
95101307Sache		errno = EFTYPE;
9672165Sphantom		goto bad_locale;
97101307Sache	}
9872165Sphantom	bufsize = namesize + st.st_size;
99101498Sache	if ((lbuf = malloc(bufsize)) == NULL) {
100101498Sache		errno = ENOMEM;
10172165Sphantom		goto bad_locale;
102101498Sache	}
103101470Sache	(void)strcpy(lbuf, name);
10472165Sphantom	p = lbuf + namesize;
10572165Sphantom	plim = p + st.st_size;
10672165Sphantom	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
10772165Sphantom		goto bad_lbuf;
10872165Sphantom	/*
10987658Sphantom	 * Parse the locale file into localebuf.
11087658Sphantom	 */
111101307Sache	if (plim[-1] != '\n') {
112101307Sache		errno = EFTYPE;
11372165Sphantom		goto bad_lbuf;
114101307Sache	}
11572165Sphantom	num_lines = split_lines(p, plim);
11672442Sphantom	if (num_lines >= locale_buf_size_max)
11772442Sphantom		num_lines = locale_buf_size_max;
11872442Sphantom	else if (num_lines >= locale_buf_size_min)
11972442Sphantom		num_lines = locale_buf_size_min;
120101307Sache	else {
121101307Sache		errno = EFTYPE;
122101498Sache		goto bad_lbuf;
123101307Sache	}
124101498Sache	(void)_close(fd);
12572165Sphantom	/*
12688309Sphantom	 * Record the successful parse in the cache.
12788309Sphantom	 */
128116274Sjkh	if (*locale_buf != NULL)
129116274Sjkh		free(*locale_buf);
130116274Sjkh	*locale_buf = lbuf;
131116274Sjkh	for (p = *locale_buf, i = 0; i < num_lines; i++)
132101498Sache		dst_localebuf[i] = (p += strlen(p) + 1);
133101498Sache	for (i = num_lines; i < locale_buf_size_max; i++)
134101498Sache		dst_localebuf[i] = NULL;
13572165Sphantom	*using_locale = 1;
136101470Sache
137101498Sache	return (_LDP_LOADED);
13872165Sphantom
13972165Sphantombad_lbuf:
140101498Sache	saverr = errno;
141101498Sache	free(lbuf);
142101470Sache	errno = saverr;
14372165Sphantombad_locale:
144101498Sache	saverr = errno;
145101498Sache	(void)_close(fd);
146101470Sache	errno = saverr;
147101470Sache
148101498Sache	return (_LDP_ERROR);
14972165Sphantom}
15072165Sphantom
15172165Sphantomstatic int
152101498Sachesplit_lines(char *p, const char *plim)
153101470Sache{
15472165Sphantom	int i;
15572165Sphantom
156128650Sache	i = 0;
157128650Sache	while (p < plim) {
158128650Sache		if (*p == '\n') {
159128650Sache			*p = '\0';
160128650Sache			i++;
161128650Sache		}
162128650Sache		p++;
16372165Sphantom	}
164101470Sache	return (i);
16572165Sphantom}
16672165Sphantom
167