2 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3 * at Electronni Visti IA, Kiev, Ukraine.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/lib/libc/locale/collate.c,v 1.21.2.4 2002/10/11 10:36:47 ache Exp $
28 * $DragonFly: src/lib/libc/locale/collate.c,v 1.2 2003/06/17 04:26:43 dillon Exp $
31 #include <arpa/inet.h>
41 #include "setlocale.h"
44 int __collate_load_error = 1;
45 int __collate_substitute_nontrivial;
47 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
48 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
49 struct __collate_st_chain_pri *__collate_chain_pri_table;
51 void __collate_err(int ex, const char *f) __dead2;
54 __collate_load_tables(const char *encoding)
57 int i, saverr, chains;
59 char strbuf[STR_LEN], buf[PATH_MAX];
60 void *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table;
61 static char collate_encoding[ENCODING_LEN + 1];
63 /* 'encoding' must be already checked. */
64 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) {
65 __collate_load_error = 1;
70 * If the locale name is the same as our cache, use the cache.
72 if (strcmp(encoding, collate_encoding) == 0) {
73 __collate_load_error = 0;
78 * Slurp the locale file into the cache.
81 /* 'PathLocale' must be already set & checked. */
82 /* Range checking not needed, encoding has fixed size */
83 (void)strcpy(buf, _PathLocale);
84 (void)strcat(buf, "/");
85 (void)strcat(buf, encoding);
86 (void)strcat(buf, "/LC_COLLATE");
87 if ((fp = fopen(buf, "r")) == NULL)
90 if (fread(strbuf, sizeof(strbuf), 1, fp) != 1) {
97 if (strcmp(strbuf, COLLATE_VERSION) == 0)
99 else if (strcmp(strbuf, COLLATE_VERSION1_1) == 0)
107 if (fread(&u32, sizeof(u32), 1, fp) != 1) {
113 if ((chains = (int)ntohl(u32)) < 1) {
121 if ((TMP_substitute_table =
122 malloc(sizeof(__collate_substitute_table))) == NULL) {
128 if ((TMP_char_pri_table =
129 malloc(sizeof(__collate_char_pri_table))) == NULL) {
131 free(TMP_substitute_table);
136 if ((TMP_chain_pri_table =
137 malloc(sizeof(*__collate_chain_pri_table) * chains)) == NULL) {
139 free(TMP_substitute_table);
140 free(TMP_char_pri_table);
146 #define FREAD(a, b, c, d) \
148 if (fread(a, b, c, d) != c) { \
150 free(TMP_substitute_table); \
151 free(TMP_char_pri_table); \
152 free(TMP_chain_pri_table); \
155 return (_LDP_ERROR); \
159 FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp);
160 FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp);
161 FREAD(TMP_chain_pri_table,
162 sizeof(*__collate_chain_pri_table), chains, fp);
165 (void)strcpy(collate_encoding, encoding);
166 if (__collate_substitute_table_ptr != NULL)
167 free(__collate_substitute_table_ptr);
168 __collate_substitute_table_ptr = TMP_substitute_table;
169 if (__collate_char_pri_table_ptr != NULL)
170 free(__collate_char_pri_table_ptr);
171 __collate_char_pri_table_ptr = TMP_char_pri_table;
172 if (__collate_chain_pri_table != NULL)
173 free(__collate_chain_pri_table);
174 __collate_chain_pri_table = TMP_chain_pri_table;
176 __collate_substitute_nontrivial = 0;
177 for (i = 0; i < UCHAR_MAX + 1; i++) {
178 if (__collate_substitute_table[i][0] != i ||
179 __collate_substitute_table[i][1] != 0) {
180 __collate_substitute_nontrivial = 1;
184 __collate_load_error = 0;
186 return (_LDP_LOADED);
190 __collate_substitute(s)
193 int dest_len, len, nlen;
194 int delta = strlen(s);
195 u_char *dest_str = NULL;
197 if (s == NULL || *s == '\0')
198 return (__collate_strdup(""));
200 dest_str = malloc(dest_len = delta);
201 if (dest_str == NULL)
202 __collate_err(EX_OSERR, __FUNCTION__);
205 nlen = len + strlen(__collate_substitute_table[*s]);
206 if (dest_len <= nlen) {
207 dest_str = reallocf(dest_str, dest_len = nlen + delta);
208 if (dest_str == NULL)
209 __collate_err(EX_OSERR, __FUNCTION__);
211 (void)strcpy(dest_str + len, __collate_substitute_table[*s++]);
218 __collate_lookup(t, len, prim, sec)
220 int *len, *prim, *sec;
222 struct __collate_st_chain_pri *p2;
226 for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++) {
227 if (*t == p2->str[0] &&
228 strncmp(t, p2->str, strlen(p2->str)) == 0) {
229 *len = strlen(p2->str);
235 *prim = __collate_char_pri_table[*t].prim;
236 *sec = __collate_char_pri_table[*t].sec;
243 u_char *t = strdup(s);
246 __collate_err(EX_OSERR, __FUNCTION__);
251 __collate_err(int ex, const char *f)
253 extern char *__progname; /* Program name, from crt0. */
258 _write(STDERR_FILENO, s, strlen(s));
259 _write(STDERR_FILENO, ": ", 2);
261 _write(STDERR_FILENO, s, strlen(s));
262 _write(STDERR_FILENO, ": ", 2);
263 s = strerror(serrno);
264 _write(STDERR_FILENO, s, strlen(s));
265 _write(STDERR_FILENO, "\n", 1);
271 __collate_print_tables()
274 struct __collate_st_chain_pri *p2;
276 printf("Substitute table:\n");
277 for (i = 0; i < UCHAR_MAX + 1; i++)
278 if (i != *__collate_substitute_table[i])
279 printf("\t'%c' --> \"%s\"\n", i,
280 __collate_substitute_table[i]);
281 printf("Chain priority table:\n");
282 for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
283 printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
284 printf("Char priority table:\n");
285 for (i = 0; i < UCHAR_MAX + 1; i++)
286 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
287 __collate_char_pri_table[i].sec);