Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / posix / collate.c
blob8e84895baca4496a4edd397ee526558e4b4faa10
1 /*-
2 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
3 * at Electronni Visti IA, Kiev, Ukraine.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
25 * SUCH DAMAGE.
28 #include <sys/cdefs.h>
30 #include "namespace.h"
31 #include "rune.h"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include "sysexits.h"
38 #include "un-namespace.h"
40 #include "collate.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) \
51 do { \
52 if (fread(a, b, c, d) != c) { \
53 fclose(d); \
54 return -1; \
55 } \
56 } while(0)
58 void __collate_err(int ex, const char *f);
60 int
61 __collate_load_tables(
62 char *encoding
65 char buf[PATH_MAX];
66 FILE *fp;
67 int i, save_load_error;
69 save_load_error = __collate_load_error;
70 __collate_load_error = 1;
71 if (!encoding) {
72 __collate_load_error = save_load_error;
73 return -1;
75 if (!strcmp(encoding, "C") || !strcmp(encoding, "POSIX"))
76 return 0;
77 if (!_PathLocale) {
78 __collate_load_error = save_load_error;
79 return -1;
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;
88 return -1;
90 FREAD(__collate_version, sizeof(__collate_version), 1, fp);
91 if (strcmp(__collate_version, COLLATE_VERSION) != 0) {
92 fclose(fp);
93 return -1;
95 FREAD(__collate_substitute_table, sizeof(__collate_substitute_table),
96 1, fp);
97 FREAD(__collate_char_pri_table, sizeof(__collate_char_pri_table), 1,
98 fp);
99 FREAD(__collate_chain_pri_table, sizeof(__collate_chain_pri_table), 1,
100 fp);
101 fclose(fp);
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;
109 break;
113 return 0;
116 u_char *
117 __collate_substitute(
118 const u_char *s
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 *) "");
127 delta += delta / 8;
128 dest_str = (u_char *) malloc(dest_len = delta);
129 if(dest_str == NULL)
130 __collate_err(EX_OSERR, __FUNCTION__);
131 len = 0;
132 while(*s) {
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);
137 if(dest_str == NULL)
138 __collate_err(EX_OSERR, __FUNCTION__);
140 strcpy((char *) dest_str + len,
141 (const char *) __collate_substitute_table[*s++]);
142 len = nlen;
144 return dest_str;
147 void
148 __collate_lookup(
149 const u_char *t,
150 int *len,
151 int *prim,
152 int *sec
155 struct __collate_st_chain_pri *p2;
157 *len = 1;
158 *prim = *sec = 0;
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);
163 *prim = p2->prim;
164 *sec = p2->sec;
165 return;
168 *prim = __collate_char_pri_table[*t].prim;
169 *sec = __collate_char_pri_table[*t].sec;
172 u_char *
173 __collate_strdup(u_char *s)
175 u_char *t = (u_char *) strdup((const char *) s);
177 if (t == NULL)
178 __collate_err(EX_OSERR, __FUNCTION__);
179 return t;
182 void
183 __collate_err(int ex, const char *f)
185 const char *s;
186 int serrno = errno;
187 int dummy;
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);
196 exit(ex);
199 #ifdef COLLATE_DEBUG
200 void
201 __collate_print_tables()
203 int i;
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);
219 #endif