Deleted Added
full compact
collate.c (101478) collate.c (101498)
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>
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 101478 2002-08-07 18:02:45Z ache $");
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"
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"
43
44#include "libc_private.h"
45
46int __collate_load_error = 1;
47int __collate_substitute_nontrivial;
44
45#include "libc_private.h"
46
47int __collate_load_error = 1;
48int __collate_substitute_nontrivial;
48char __collate_version[STR_LEN];
49
49u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
50struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
51struct __collate_st_chain_pri __collate_chain_pri_table[TABLE_SIZE];
52
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
53#define FREAD(a, b, c, d) \
54 do { \
55 if (fread(a, b, c, d) != c) { \
56 (void)fclose(d); \
57 return -1; \
58 } \
59 } while(0)
60
61void __collate_err(int ex, const char *f) __dead2;
62
63int
54void __collate_err(int ex, const char *f) __dead2;
55
56int
64__collate_load_tables(encoding)
65 char *encoding;
57__collate_load_tables(const char *encoding)
66{
58{
67 char buf[PATH_MAX];
68 FILE *fp;
59 FILE *fp;
69 int i, save_load_error;
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];
70
65
71 save_load_error = __collate_load_error;
72 __collate_load_error = 1;
73 if (!encoding) {
74 __collate_load_error = save_load_error;
75 return -1;
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);
76 }
70 }
77 if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
78 return 0;
79 if (!_PathLocale) {
80 __collate_load_error = save_load_error;
81 return -1;
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);
82 }
78 }
79
80 /*
81 * Slurp the locale file into the cache.
82 */
83
84 /* 'PathLocale' must be already set & checked. */
83 /* Range checking not needed, encoding has fixed size */
84 (void)strcpy(buf, _PathLocale);
85 (void)strcat(buf, "/");
86 (void)strcat(buf, encoding);
87 (void)strcat(buf, "/LC_COLLATE");
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");
88 if ((fp = fopen(buf, "r")) == NULL) {
89 __collate_load_error = save_load_error;
90 return -1;
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);
91 }
98 }
92 FREAD(__collate_version, sizeof(__collate_version), 1, fp);
93 if (strcmp(__collate_version, COLLATE_VERSION) != 0) {
99 if ((TMP_char_pri_table =
100 malloc(sizeof(__collate_char_pri_table))) == NULL) {
101 free(TMP_substitute_table);
94 (void)fclose(fp);
102 (void)fclose(fp);
95 return -1;
103 errno = ENOMEM;
104 return (_LDP_ERROR);
96 }
105 }
97 FREAD(__collate_substitute_table, sizeof(__collate_substitute_table),
98 1, fp);
99 FREAD(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1,
100 fp);
101 FREAD(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1,
102 fp);
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);
103 (void)fclose(fp);
140 (void)fclose(fp);
104 __collate_load_error = 0;
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);
105
106 __collate_substitute_nontrivial = 0;
107 for (i = 0; i < UCHAR_MAX + 1; i++) {
108 if (__collate_substitute_table[i][0] != i ||
109 __collate_substitute_table[i][1] != 0) {
110 __collate_substitute_nontrivial = 1;
111 break;
112 }
113 }
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;
114
162
115 return 0;
163 return (_LDP_LOADED);
116}
117
118u_char *
119__collate_substitute(s)
120 const u_char *s;
121{
122 int dest_len, len, nlen;
123 int delta = strlen(s);

--- 23 unchanged lines hidden (view full) ---

147__collate_lookup(t, len, prim, sec)
148 const u_char *t;
149 int *len, *prim, *sec;
150{
151 struct __collate_st_chain_pri *p2;
152
153 *len = 1;
154 *prim = *sec = 0;
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;
155 for(p2 = __collate_chain_pri_table; p2->str[0]; p2++) {
156 if(strncmp(t, p2->str, strlen(p2->str)) == 0) {
203 for (p2 = __collate_chain_pri_table; p2->str[0]; p2++) {
204 if (strncmp(t, p2->str, strlen(p2->str)) == 0) {
157 *len = strlen(p2->str);
158 *prim = p2->prim;
159 *sec = p2->sec;
160 return;
161 }
162 }
163 *prim = __collate_char_pri_table[*t].prim;
164 *sec = __collate_char_pri_table[*t].sec;

--- 52 unchanged lines hidden ---
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 ---