xref: /freebsd/lib/libc/locale/ldpart.c (revision 815b7436a7c6302365b6514194d27d41cb736227)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000, 2001 Alexey Zelkin <phantom@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "namespace.h"
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <limits.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include "un-namespace.h"
41 
42 #include "ldpart.h"
43 #include "setlocale.h"
44 
45 static int split_lines(char *, const char *);
46 
47 int
48 __part_load_locale(const char *name,
49 		int *using_locale,
50 		char **locale_buf,
51 		const char *category_filename,
52 		int locale_buf_size_max,
53 		int locale_buf_size_min,
54 		const char **dst_localebuf)
55 {
56 	int		saverr, fd, i, num_lines;
57 	char		*lbuf, *p;
58 	const char	*plim;
59 	char		filename[PATH_MAX];
60 	struct stat	st;
61 	size_t		namesize, bufsize;
62 
63 	/* 'name' must be already checked. */
64 	if (strcmp(name, "C") == 0 || strcmp(name, "POSIX") == 0 ||
65 	    strncmp(name, "C.", 2) == 0) {
66 		*using_locale = 0;
67 		return (_LDP_CACHE);
68 	}
69 
70 	/*
71 	 * If the locale name is the same as our cache, use the cache.
72 	 */
73 	if (*locale_buf != NULL && strcmp(name, *locale_buf) == 0) {
74 		*using_locale = 1;
75 		return (_LDP_CACHE);
76 	}
77 
78 	/*
79 	 * Slurp the locale file into the cache.
80 	 */
81 	namesize = strlen(name) + 1;
82 
83 	/* 'PathLocale' must be already set & checked. */
84 
85 	/* Range checking not needed, 'name' size is limited */
86 	strcpy(filename, _PathLocale);
87 	strcat(filename, "/");
88 	strcat(filename, name);
89 	strcat(filename, "/");
90 	strcat(filename, category_filename);
91 	if ((fd = _open(filename, O_RDONLY | O_CLOEXEC)) < 0)
92 		return (_LDP_ERROR);
93 	if (_fstat(fd, &st) != 0)
94 		goto bad_locale;
95 	if (st.st_size <= 0) {
96 		errno = EFTYPE;
97 		goto bad_locale;
98 	}
99 	bufsize = namesize + st.st_size;
100 	if ((lbuf = malloc(bufsize)) == NULL) {
101 		errno = ENOMEM;
102 		goto bad_locale;
103 	}
104 	(void)strcpy(lbuf, name);
105 	p = lbuf + namesize;
106 	plim = p + st.st_size;
107 	if (_read(fd, p, (size_t) st.st_size) != st.st_size)
108 		goto bad_lbuf;
109 	/*
110 	 * Parse the locale file into localebuf.
111 	 */
112 	if (plim[-1] != '\n') {
113 		errno = EFTYPE;
114 		goto bad_lbuf;
115 	}
116 	num_lines = split_lines(p, plim);
117 	if (num_lines >= locale_buf_size_max)
118 		num_lines = locale_buf_size_max;
119 	else if (num_lines >= locale_buf_size_min)
120 		num_lines = locale_buf_size_min;
121 	else {
122 		errno = EFTYPE;
123 		goto bad_lbuf;
124 	}
125 	(void)_close(fd);
126 	/*
127 	 * Record the successful parse in the cache.
128 	 */
129 	if (*locale_buf != NULL)
130 		free(*locale_buf);
131 	*locale_buf = lbuf;
132 	for (p = *locale_buf, i = 0; i < num_lines; i++)
133 		dst_localebuf[i] = (p += strlen(p) + 1);
134 	for (i = num_lines; i < locale_buf_size_max; i++)
135 		dst_localebuf[i] = NULL;
136 	*using_locale = 1;
137 
138 	return (_LDP_LOADED);
139 
140 bad_lbuf:
141 	saverr = errno;
142 	free(lbuf);
143 	errno = saverr;
144 bad_locale:
145 	saverr = errno;
146 	(void)_close(fd);
147 	errno = saverr;
148 
149 	return (_LDP_ERROR);
150 }
151 
152 static int
153 split_lines(char *p, const char *plim)
154 {
155 	int i;
156 
157 	i = 0;
158 	while (p < plim) {
159 		if (*p == '\n') {
160 			*p = '\0';
161 			i++;
162 		}
163 		p++;
164 	}
165 	return (i);
166 }
167 
168