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
28 #include <sys/cdefs.h>
30 #include "namespace.h"
38 #include "un-namespace.h"
42 extern char *_PathLocale
;
43 int __collate_load_error
= 1;
44 int __collate_substitute_nontrivial
;
45 char __collate_version
[STR_LEN
];
46 u_char __collate_substitute_table
[UCHAR_MAX
+ 1][STR_LEN
];
47 struct __collate_st_char_pri __collate_char_pri_table
[UCHAR_MAX
+ 1];
48 struct __collate_st_chain_pri __collate_chain_pri_table
[TABLE_SIZE
];
50 #define FREAD(a, b, c, d) \
52 if (fread(a, b, c, d) != c) { \
58 void __collate_err(int ex
, const char *f
);
61 __collate_load_tables(
67 int i
, save_load_error
;
69 save_load_error
= __collate_load_error
;
70 __collate_load_error
= 1;
72 __collate_load_error
= save_load_error
;
75 if (!strcmp(encoding
, "C") || !strcmp(encoding
, "POSIX"))
78 __collate_load_error
= save_load_error
;
81 /* Range checking not needed, encoding has fixed size */
82 (void) strcpy(buf
, _PathLocale
);
83 (void) strcat(buf
, "/");
84 (void) strcat(buf
, encoding
);
85 (void) strcat(buf
, "/LC_COLLATE");
86 if ((fp
= fopen(buf
, "r")) == NULL
) {
87 __collate_load_error
= save_load_error
;
90 FREAD(__collate_version
, sizeof(__collate_version
), 1, fp
);
91 if (strcmp(__collate_version
, COLLATE_VERSION
) != 0) {
95 FREAD(__collate_substitute_table
, sizeof(__collate_substitute_table
),
97 FREAD(__collate_char_pri_table
, sizeof(__collate_char_pri_table
), 1,
99 FREAD(__collate_chain_pri_table
, sizeof(__collate_chain_pri_table
), 1,
102 __collate_load_error
= 0;
104 __collate_substitute_nontrivial
= 0;
105 for (i
= 0; i
< UCHAR_MAX
+ 1; i
++) {
106 if (__collate_substitute_table
[i
][0] != i
||
107 __collate_substitute_table
[i
][1] != 0) {
108 __collate_substitute_nontrivial
= 1;
117 __collate_substitute(
121 int dest_len
, len
, nlen
;
122 int delta
= strlen((const char *) s
);
123 u_char
*dest_str
= NULL
;
125 if(s
== NULL
|| *s
== '\0')
126 return __collate_strdup((u_char
*) "");
128 dest_str
= (u_char
*) malloc(dest_len
= delta
);
130 __collate_err(EX_OSERR
, __FUNCTION__
);
133 nlen
= len
+ strlen((const char *)
134 __collate_substitute_table
[*s
]);
135 if (dest_len
<= nlen
) {
136 dest_str
= reallocf(dest_str
, dest_len
= nlen
+ delta
);
138 __collate_err(EX_OSERR
, __FUNCTION__
);
140 strcpy((char *) dest_str
+ len
,
141 (const char *) __collate_substitute_table
[*s
++]);
155 struct __collate_st_chain_pri
*p2
;
159 for(p2
= __collate_chain_pri_table
; p2
->str
[0]; p2
++) {
160 if(strncmp((const char *) t
, (const char *) p2
->str
,
161 strlen((const char *) p2
->str
)) == 0) {
162 *len
= strlen((const char *) p2
->str
);
168 *prim
= __collate_char_pri_table
[*t
].prim
;
169 *sec
= __collate_char_pri_table
[*t
].sec
;
173 __collate_strdup(u_char
*s
)
175 u_char
*t
= (u_char
*) strdup((const char *) s
);
178 __collate_err(EX_OSERR
, __FUNCTION__
);
183 __collate_err(int ex
, const char *f
)
189 /* Be careful to change write counts if you change the strings */
190 write(STDERR_FILENO
, "collate_error: ", 15);
191 write(STDERR_FILENO
, f
, strlen(f
));
192 write(STDERR_FILENO
, ": ", 2);
193 s
= _strerror_r(_REENT
, serrno
, 1, &dummy
);
194 write(STDERR_FILENO
, s
, strlen(s
));
195 write(STDERR_FILENO
, "\n", 1);
201 __collate_print_tables()
204 struct __collate_st_chain_pri
*p2
;
206 printf("Substitute table:\n");
207 for (i
= 0; i
< UCHAR_MAX
+ 1; i
++)
208 if (i
!= *__collate_substitute_table
[i
])
209 printf("\t'%c' --> \"%s\"\n", i
,
210 __collate_substitute_table
[i
]);
211 printf("Chain priority table:\n");
212 for (p2
= __collate_chain_pri_table
; p2
->str
[0]; p2
++)
213 printf("\t\"%s\" : %d %d\n\n", p2
->str
, p2
->prim
, p2
->sec
);
214 printf("Char priority table:\n");
215 for (i
= 0; i
< UCHAR_MAX
+ 1; i
++)
216 printf("\t'%c' : %d %d\n", i
, __collate_char_pri_table
[i
].prim
,
217 __collate_char_pri_table
[i
].sec
);