1/*- 2 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> 3 * at Electronni Visti IA, Kiev, Ukraine. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 12 unchanged lines hidden (view full) --- 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 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> |
29__FBSDID("$FreeBSD: head/lib/libc/locale/collate.c 101498 2002-08-08 05:51:54Z ache $"); |
30 31#include "namespace.h" 32#include <rune.h> 33#include <stdio.h> 34#include <stdlib.h> 35#include <string.h> 36#include <errno.h> 37#include <unistd.h> 38#include <sysexits.h> 39#include "un-namespace.h" 40 41#include "collate.h" 42#include "setlocale.h" |
43#include "ldpart.h" |
44 45#include "libc_private.h" 46 47int __collate_load_error = 1; 48int __collate_substitute_nontrivial; |
49 |
50u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN]; 51struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1]; 52struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE]; 53 |
54void __collate_err(int ex, const char *f) __dead2; 55 56int |
57__collate_load_tables(const char *encoding) |
58{ |
59 FILE *fp; |
60 int i, saverr; 61 char collate_version[STR_LEN]; 62 char buf[PATH_MAX]; 63 char *TMP_substitute_table, *TMP_char_pri_table, *TMP_chain_pri_table; 64 static char collate_encoding[ENCODING_LEN + 1]; |
65 |
66 /* 'encoding' must be already checked. */ 67 if (strcmp(encoding, "C") == 0 || strcmp(encoding, "POSIX") == 0) { 68 __collate_load_error = 1; 69 return (_LDP_CACHE); |
70 } |
71 72 /* 73 * If the locale name is the same as our cache, use the cache. 74 */ 75 if (strcmp(encoding, collate_encoding) == 0) { 76 __collate_load_error = 0; 77 return (_LDP_CACHE); |
78 } |
79 80 /* 81 * Slurp the locale file into the cache. 82 */ 83 84 /* 'PathLocale' must be already set & checked. */ |
85 /* Range checking not needed, encoding has fixed size */ 86 (void)strcpy(buf, _PathLocale); 87 (void)strcat(buf, "/"); 88 (void)strcat(buf, encoding); 89 (void)strcat(buf, "/LC_COLLATE"); |
90 if ((fp = fopen(buf, "r")) == NULL) 91 return (_LDP_ERROR); 92 93 if ((TMP_substitute_table = 94 malloc(sizeof(__collate_substitute_table))) == NULL) { 95 (void)fclose(fp); 96 errno = ENOMEM; 97 return (_LDP_ERROR); |
98 } |
99 if ((TMP_char_pri_table = 100 malloc(sizeof(__collate_char_pri_table))) == NULL) { 101 free(TMP_substitute_table); |
102 (void)fclose(fp); |
103 errno = ENOMEM; 104 return (_LDP_ERROR); |
105 } |
106 if ((TMP_chain_pri_table = 107 malloc(sizeof(__collate_chain_pri_table))) == NULL) { 108 free(TMP_substitute_table); 109 free(TMP_char_pri_table); 110 (void)fclose(fp); 111 errno = ENOMEM; 112 return (_LDP_ERROR); 113 } 114 115#define FREAD(a, b, c, d) \ 116{ \ 117 if (fread(a, b, c, d) != c) { \ 118 saverr = errno; \ 119 free(TMP_substitute_table); \ 120 free(TMP_char_pri_table); \ 121 free(TMP_chain_pri_table); \ 122 (void)fclose(d); \ 123 errno = saverr; \ 124 return (_LDP_ERROR); \ 125 } \ 126} 127 128 FREAD(collate_version, sizeof(collate_version), 1, fp); 129 if (strcmp(collate_version, COLLATE_VERSION) != 0) { 130 free(TMP_substitute_table); 131 free(TMP_char_pri_table); 132 free(TMP_chain_pri_table); 133 (void)fclose(fp); 134 errno = EFTYPE; 135 return (_LDP_ERROR); 136 } 137 FREAD(TMP_substitute_table, sizeof(__collate_substitute_table), 1, fp); 138 FREAD(TMP_char_pri_table, sizeof(__collate_char_pri_table), 1, fp); 139 FREAD(TMP_chain_pri_table, sizeof(__collate_chain_pri_table), 1, fp); |
140 (void)fclose(fp); |
141 142 (void)strcpy(collate_encoding, encoding); 143 (void)memcpy(__collate_substitute_table, TMP_substitute_table, 144 sizeof(__collate_substitute_table)); 145 (void)memcpy(__collate_char_pri_table, TMP_char_pri_table, 146 sizeof(__collate_char_pri_table)); 147 (void)memcpy(__collate_chain_pri_table, TMP_chain_pri_table, 148 sizeof(__collate_chain_pri_table)); 149 free(TMP_substitute_table); 150 free(TMP_char_pri_table); 151 free(TMP_chain_pri_table); |
152 153 __collate_substitute_nontrivial = 0; 154 for (i = 0; i < UCHAR_MAX + 1; i++) { 155 if (__collate_substitute_table[i][0] != i || 156 __collate_substitute_table[i][1] != 0) { 157 __collate_substitute_nontrivial = 1; 158 break; 159 } 160 } |
161 __collate_load_error = 0; |
162 |
163 return (_LDP_LOADED); |
164} 165 166u_char * 167__collate_substitute(s) 168 const u_char *s; 169{ 170 int dest_len, len, nlen; 171 int delta = strlen(s); --- 23 unchanged lines hidden (view full) --- 195__collate_lookup(t, len, prim, sec) 196 const u_char *t; 197 int *len, *prim, *sec; 198{ 199 struct __collate_st_chain_pri *p2; 200 201 *len = 1; 202 *prim = *sec = 0; |
203 for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) { 204 if (strncmp(t, p2->str, strlen(p2->str)) == 0) { |
205 *len = strlen(p2->str); 206 *prim = p2->prim; 207 *sec = p2->sec; 208 return; 209 } 210 } 211 *prim = __collate_char_pri_table[*t].prim; 212 *sec = __collate_char_pri_table[*t].sec; --- 52 unchanged lines hidden --- |