]>
Commit | Line | Data |
---|---|---|
6973fc01 | 1 | /* Handle configuration data. |
688903eb | 2 | Copyright (C) 1997-2018 Free Software Foundation, Inc. |
6973fc01 UD |
3 | This file is part of the GNU C Library. |
4 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
5 | ||
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
6973fc01 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
6973fc01 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
6973fc01 | 19 | |
d6204268 | 20 | #include <assert.h> |
6973fc01 | 21 | #include <ctype.h> |
e34b0f29 | 22 | #include <errno.h> |
fab6d621 | 23 | #include <limits.h> |
5db91571 | 24 | #include <locale.h> |
6973fc01 | 25 | #include <search.h> |
17427edd | 26 | #include <stddef.h> |
6973fc01 | 27 | #include <stdio.h> |
2706ee38 | 28 | #include <stdio_ext.h> |
6973fc01 UD |
29 | #include <stdlib.h> |
30 | #include <string.h> | |
31 | #include <unistd.h> | |
32 | #include <sys/param.h> | |
33 | ||
ec999b8e | 34 | #include <libc-lock.h> |
e62c19f1 UD |
35 | #include <gconv_int.h> |
36 | ||
6973fc01 UD |
37 | |
38 | /* This is the default path where we look for module lists. */ | |
39 | static const char default_gconv_path[] = GCONV_PATH; | |
40 | ||
335a3b0a AS |
41 | /* Type to represent search path. */ |
42 | struct path_elem | |
43 | { | |
44 | const char *name; | |
45 | size_t len; | |
46 | }; | |
47 | ||
835bf8e0 UD |
48 | /* The path elements, as determined by the __gconv_get_path function. |
49 | All path elements end in a slash. */ | |
17427edd | 50 | struct path_elem *__gconv_path_elem; |
835bf8e0 | 51 | /* Maximum length of a single path element in __gconv_path_elem. */ |
d6204268 UD |
52 | size_t __gconv_max_path_elem_len; |
53 | ||
54 | /* We use the following struct if we couldn't allocate memory. */ | |
4c038b68 | 55 | static const struct path_elem empty_path_elem = { NULL, 0 }; |
d6204268 | 56 | |
6973fc01 UD |
57 | /* Name of the file containing the module information in the directories |
58 | along the path. */ | |
59 | static const char gconv_conf_filename[] = "gconv-modules"; | |
60 | ||
e34b0f29 UD |
61 | /* Filename extension for the modules. */ |
62 | #ifndef MODULE_EXT | |
63 | # define MODULE_EXT ".so" | |
64 | #endif | |
65 | static const char gconv_module_ext[] = MODULE_EXT; | |
66 | ||
6973fc01 UD |
67 | /* We have a few builtin transformations. */ |
68 | static struct gconv_module builtin_modules[] = | |
69 | { | |
f9ad060c UD |
70 | #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \ |
71 | MinF, MaxF, MinT, MaxT) \ | |
6973fc01 | 72 | { \ |
0274d73c RM |
73 | .from_string = From, \ |
74 | .to_string = To, \ | |
75 | .cost_hi = Cost, \ | |
76 | .cost_lo = INT_MAX, \ | |
77 | .module_name = Name \ | |
6973fc01 | 78 | }, |
5891046a UD |
79 | #define BUILTIN_ALIAS(From, To) |
80 | ||
81 | #include "gconv_builtin.h" | |
5891046a UD |
82 | |
83 | #undef BUILTIN_TRANSFORMATION | |
84 | #undef BUILTIN_ALIAS | |
f9ad060c | 85 | }; |
5891046a | 86 | |
88dbff8c | 87 | static const char builtin_aliases[] = |
5891046a | 88 | { |
f9ad060c UD |
89 | #define BUILTIN_TRANSFORMATION(From, To, Cost, Name, Fct, BtowcFct, \ |
90 | MinF, MaxF, MinT, MaxT) | |
88dbff8c | 91 | #define BUILTIN_ALIAS(From, To) From "\0" To "\0" |
6973fc01 UD |
92 | |
93 | #include "gconv_builtin.h" | |
f9ad060c UD |
94 | |
95 | #undef BUILTIN_TRANSFORMATION | |
96 | #undef BUILTIN_ALIAS | |
6973fc01 UD |
97 | }; |
98 | ||
3ce1f295 UD |
99 | #include <libio/libioP.h> |
100 | #define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp) | |
50304ef0 | 101 | |
6973fc01 | 102 | |
6b98979f UD |
103 | /* Value of the GCONV_PATH environment variable. */ |
104 | const char *__gconv_path_envvar; | |
105 | ||
106 | ||
2bd60880 | 107 | /* Test whether there is already a matching module known. */ |
6973fc01 | 108 | static int |
d2dfc5de | 109 | detect_conflict (const char *alias) |
6973fc01 | 110 | { |
2bd60880 | 111 | struct gconv_module *node = __gconv_modules_db; |
6973fc01 | 112 | |
2bd60880 | 113 | while (node != NULL) |
6973fc01 | 114 | { |
d2dfc5de | 115 | int cmpres = strcmp (alias, node->from_string); |
8c479619 | 116 | |
2bd60880 | 117 | if (cmpres == 0) |
d2dfc5de UD |
118 | /* We have a conflict. */ |
119 | return 1; | |
2bd60880 UD |
120 | else if (cmpres < 0) |
121 | node = node->left; | |
122 | else | |
123 | node = node->right; | |
8c479619 | 124 | } |
2bd60880 UD |
125 | |
126 | return node != NULL; | |
8c479619 UD |
127 | } |
128 | ||
129 | ||
88dbff8c UD |
130 | /* The actual code to add aliases. */ |
131 | static void | |
132 | add_alias2 (const char *from, const char *to, const char *wp, void *modules) | |
133 | { | |
134 | /* Test whether this alias conflicts with any available module. */ | |
135 | if (detect_conflict (from)) | |
136 | /* It does conflict, don't add the alias. */ | |
137 | return; | |
138 | ||
139 | struct gconv_alias *new_alias = (struct gconv_alias *) | |
140 | malloc (sizeof (struct gconv_alias) + (wp - from)); | |
141 | if (new_alias != NULL) | |
142 | { | |
143 | void **inserted; | |
144 | ||
145 | new_alias->fromname = memcpy ((char *) new_alias | |
146 | + sizeof (struct gconv_alias), | |
147 | from, wp - from); | |
148 | new_alias->toname = new_alias->fromname + (to - from); | |
149 | ||
150 | inserted = (void **) __tsearch (new_alias, &__gconv_alias_db, | |
151 | __gconv_alias_compare); | |
152 | if (inserted == NULL || *inserted != new_alias) | |
153 | /* Something went wrong, free this entry. */ | |
154 | free (new_alias); | |
155 | } | |
156 | } | |
157 | ||
158 | ||
a334319f | 159 | /* Add new alias. */ |
dd9423a6 | 160 | static void |
a334319f | 161 | add_alias (char *rp, void *modules) |
6973fc01 | 162 | { |
a334319f UD |
163 | /* We now expect two more string. The strings are normalized |
164 | (converted to UPPER case) and strored in the alias database. */ | |
a334319f UD |
165 | char *from, *to, *wp; |
166 | ||
4b5b009c | 167 | while (__isspace_l (*rp, _nl_C_locobj_ptr)) |
a334319f UD |
168 | ++rp; |
169 | from = wp = rp; | |
4b5b009c UD |
170 | while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr)) |
171 | *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr); | |
a334319f UD |
172 | if (*rp == '\0') |
173 | /* There is no `to' string on the line. Ignore it. */ | |
174 | return; | |
175 | *wp++ = '\0'; | |
176 | to = ++rp; | |
4b5b009c | 177 | while (__isspace_l (*rp, _nl_C_locobj_ptr)) |
a334319f | 178 | ++rp; |
4b5b009c UD |
179 | while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr)) |
180 | *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr); | |
a334319f UD |
181 | if (to == wp) |
182 | /* No `to' string, ignore the line. */ | |
183 | return; | |
184 | *wp++ = '\0'; | |
185 | ||
88dbff8c | 186 | add_alias2 (from, to, wp, modules); |
6973fc01 UD |
187 | } |
188 | ||
189 | ||
2bd60880 | 190 | /* Insert a data structure for a new module in the search tree. */ |
dd9423a6 | 191 | static void |
b2fe29dd | 192 | insert_module (struct gconv_module *newp, int tobefreed) |
2bd60880 UD |
193 | { |
194 | struct gconv_module **rootp = &__gconv_modules_db; | |
195 | ||
196 | while (*rootp != NULL) | |
197 | { | |
198 | struct gconv_module *root = *rootp; | |
2bd60880 UD |
199 | int cmpres; |
200 | ||
d2dfc5de | 201 | cmpres = strcmp (newp->from_string, root->from_string); |
2bd60880 UD |
202 | if (cmpres == 0) |
203 | { | |
d2dfc5de UD |
204 | /* Both strings are identical. Insert the string at the |
205 | end of the `same' list if it is not already there. */ | |
206 | while (strcmp (newp->from_string, root->from_string) != 0 | |
207 | || strcmp (newp->to_string, root->to_string) != 0) | |
2bd60880 | 208 | { |
d2dfc5de UD |
209 | rootp = &root->same; |
210 | root = *rootp; | |
211 | if (root == NULL) | |
212 | break; | |
2bd60880 UD |
213 | } |
214 | ||
d2dfc5de | 215 | if (root != NULL) |
b2fe29dd | 216 | { |
2debc8c5 UD |
217 | /* This is a no new conversion. But maybe the cost is |
218 | better. */ | |
219 | if (newp->cost_hi < root->cost_hi | |
220 | || (newp->cost_hi == root->cost_hi | |
221 | && newp->cost_lo < root->cost_lo)) | |
222 | { | |
9c0592ab UD |
223 | newp->left = root->left; |
224 | newp->right = root->right; | |
225 | newp->same = root->same; | |
226 | *rootp = newp; | |
2debc8c5 | 227 | |
9c0592ab UD |
228 | free (root); |
229 | } | |
230 | else if (tobefreed) | |
b2fe29dd UD |
231 | free (newp); |
232 | return; | |
233 | } | |
2bd60880 | 234 | |
d2dfc5de | 235 | break; |
2bd60880 UD |
236 | } |
237 | else if (cmpres < 0) | |
238 | rootp = &root->left; | |
239 | else | |
240 | rootp = &root->right; | |
241 | } | |
242 | ||
243 | /* Plug in the new node here. */ | |
244 | *rootp = newp; | |
245 | } | |
246 | ||
247 | ||
6973fc01 | 248 | /* Add new module. */ |
d2dfc5de | 249 | static void |
6973fc01 | 250 | add_module (char *rp, const char *directory, size_t dir_len, void **modules, |
fab6d621 | 251 | size_t *nmodules, int modcounter) |
6973fc01 UD |
252 | { |
253 | /* We expect now | |
254 | 1. `from' name | |
255 | 2. `to' name | |
256 | 3. filename of the module | |
257 | 4. an optional cost value | |
258 | */ | |
d2dfc5de | 259 | struct gconv_alias fake_alias; |
6973fc01 UD |
260 | struct gconv_module *new_module; |
261 | char *from, *to, *module, *wp; | |
e34b0f29 | 262 | int need_ext; |
fab6d621 | 263 | int cost_hi; |
6973fc01 | 264 | |
4b5b009c | 265 | while (__isspace_l (*rp, _nl_C_locobj_ptr)) |
6973fc01 UD |
266 | ++rp; |
267 | from = rp; | |
4b5b009c | 268 | while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr)) |
6973fc01 | 269 | { |
4b5b009c | 270 | *rp = __toupper_l (*rp, _nl_C_locobj_ptr); |
6973fc01 UD |
271 | ++rp; |
272 | } | |
273 | if (*rp == '\0') | |
274 | return; | |
275 | *rp++ = '\0'; | |
276 | to = wp = rp; | |
4b5b009c | 277 | while (__isspace_l (*rp, _nl_C_locobj_ptr)) |
6973fc01 | 278 | ++rp; |
4b5b009c UD |
279 | while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr)) |
280 | *wp++ = __toupper_l (*rp++, _nl_C_locobj_ptr); | |
6973fc01 UD |
281 | if (*rp == '\0') |
282 | return; | |
283 | *wp++ = '\0'; | |
284 | do | |
285 | ++rp; | |
4b5b009c | 286 | while (__isspace_l (*rp, _nl_C_locobj_ptr)); |
6973fc01 | 287 | module = wp; |
4b5b009c | 288 | while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr)) |
6973fc01 UD |
289 | *wp++ = *rp++; |
290 | if (*rp == '\0') | |
291 | { | |
292 | /* There is no cost, use one by default. */ | |
293 | *wp++ = '\0'; | |
fab6d621 | 294 | cost_hi = 1; |
6973fc01 UD |
295 | } |
296 | else | |
297 | { | |
298 | /* There might be a cost value. */ | |
299 | char *endp; | |
300 | ||
301 | *wp++ = '\0'; | |
fab6d621 | 302 | cost_hi = strtol (rp, &endp, 10); |
2bd60880 | 303 | if (rp == endp || cost_hi < 1) |
6973fc01 | 304 | /* No useful information. */ |
fab6d621 | 305 | cost_hi = 1; |
6973fc01 UD |
306 | } |
307 | ||
308 | if (module[0] == '\0') | |
309 | /* No module name given. */ | |
310 | return; | |
311 | if (module[0] == '/') | |
312 | dir_len = 0; | |
6973fc01 | 313 | |
e34b0f29 UD |
314 | /* See whether we must add the ending. */ |
315 | need_ext = 0; | |
17427edd | 316 | if (wp - module < (ptrdiff_t) sizeof (gconv_module_ext) |
e34b0f29 UD |
317 | || memcmp (wp - sizeof (gconv_module_ext), gconv_module_ext, |
318 | sizeof (gconv_module_ext)) != 0) | |
319 | /* We must add the module extension. */ | |
320 | need_ext = sizeof (gconv_module_ext) - 1; | |
321 | ||
d2dfc5de UD |
322 | /* See whether we have already an alias with this name defined. */ |
323 | fake_alias.fromname = strndupa (from, to - from); | |
6973fc01 | 324 | |
d2dfc5de UD |
325 | if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) != NULL) |
326 | /* This module duplicates an alias. */ | |
327 | return; | |
6973fc01 | 328 | |
2bd60880 UD |
329 | new_module = (struct gconv_module *) calloc (1, |
330 | sizeof (struct gconv_module) | |
e34b0f29 UD |
331 | + (wp - from) |
332 | + dir_len + need_ext); | |
6973fc01 UD |
333 | if (new_module != NULL) |
334 | { | |
e34b0f29 UD |
335 | char *tmp; |
336 | ||
17427edd UD |
337 | new_module->from_string = tmp = (char *) (new_module + 1); |
338 | tmp = __mempcpy (tmp, from, to - from); | |
e34b0f29 | 339 | |
17427edd UD |
340 | new_module->to_string = tmp; |
341 | tmp = __mempcpy (tmp, to, module - to); | |
6973fc01 | 342 | |
fab6d621 UD |
343 | new_module->cost_hi = cost_hi; |
344 | new_module->cost_lo = modcounter; | |
6973fc01 | 345 | |
17427edd | 346 | new_module->module_name = tmp; |
e34b0f29 | 347 | |
17427edd UD |
348 | if (dir_len != 0) |
349 | tmp = __mempcpy (tmp, directory, dir_len); | |
6973fc01 | 350 | |
e34b0f29 UD |
351 | tmp = __mempcpy (tmp, module, wp - module); |
352 | ||
353 | if (need_ext) | |
354 | memcpy (tmp - 1, gconv_module_ext, sizeof (gconv_module_ext)); | |
355 | ||
2bd60880 | 356 | /* Now insert the new module data structure in our search tree. */ |
b2fe29dd | 357 | insert_module (new_module, 1); |
6973fc01 UD |
358 | } |
359 | } | |
360 | ||
361 | ||
6973fc01 UD |
362 | /* Read the next configuration file. */ |
363 | static void | |
6973fc01 UD |
364 | read_conf_file (const char *filename, const char *directory, size_t dir_len, |
365 | void **modules, size_t *nmodules) | |
366 | { | |
ee8449f7 UD |
367 | /* Note the file is opened with cancellation in the I/O functions |
368 | disabled. */ | |
312be3f9 | 369 | FILE *fp = fopen (filename, "rce"); |
6973fc01 UD |
370 | char *line = NULL; |
371 | size_t line_len = 0; | |
03fb20b5 | 372 | static int modcounter; |
6973fc01 UD |
373 | |
374 | /* Don't complain if a file is not present or readable, simply silently | |
375 | ignore it. */ | |
376 | if (fp == NULL) | |
377 | return; | |
378 | ||
2706ee38 UD |
379 | /* No threads reading from this stream. */ |
380 | __fsetlocking (fp, FSETLOCKING_BYCALLER); | |
381 | ||
6973fc01 UD |
382 | /* Process the known entries of the file. Comments start with `#' and |
383 | end with the end of the line. Empty lines are ignored. */ | |
7fc03cf3 | 384 | while (!__feof_unlocked (fp)) |
6973fc01 UD |
385 | { |
386 | char *rp, *endp, *word; | |
387 | ssize_t n = __getdelim (&line, &line_len, '\n', fp); | |
388 | if (n < 0) | |
389 | /* An error occurred. */ | |
390 | break; | |
391 | ||
392 | rp = line; | |
6973fc01 UD |
393 | /* Terminate the line (excluding comments or newline) by an NUL byte |
394 | to simplify the following code. */ | |
395 | endp = strchr (rp, '#'); | |
396 | if (endp != NULL) | |
397 | *endp = '\0'; | |
398 | else | |
04be94a8 UD |
399 | if (rp[n - 1] == '\n') |
400 | rp[n - 1] = '\0'; | |
6973fc01 | 401 | |
4b5b009c | 402 | while (__isspace_l (*rp, _nl_C_locobj_ptr)) |
e34b0f29 UD |
403 | ++rp; |
404 | ||
6973fc01 UD |
405 | /* If this is an empty line go on with the next one. */ |
406 | if (rp == endp) | |
407 | continue; | |
408 | ||
409 | word = rp; | |
4b5b009c | 410 | while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr)) |
6973fc01 UD |
411 | ++rp; |
412 | ||
413 | if (rp - word == sizeof ("alias") - 1 | |
e34b0f29 | 414 | && memcmp (word, "alias", sizeof ("alias") - 1) == 0) |
8c479619 | 415 | add_alias (rp, *modules); |
6973fc01 | 416 | else if (rp - word == sizeof ("module") - 1 |
e34b0f29 | 417 | && memcmp (word, "module", sizeof ("module") - 1) == 0) |
fab6d621 | 418 | add_module (rp, directory, dir_len, modules, nmodules, modcounter++); |
6973fc01 UD |
419 | /* else */ |
420 | /* Otherwise ignore the line. */ | |
421 | } | |
422 | ||
d6204268 UD |
423 | free (line); |
424 | ||
6973fc01 UD |
425 | fclose (fp); |
426 | } | |
427 | ||
428 | ||
d6204268 | 429 | /* Determine the directories we are looking for data in. */ |
335a3b0a | 430 | static void |
d6204268 UD |
431 | __gconv_get_path (void) |
432 | { | |
433 | struct path_elem *result; | |
434 | __libc_lock_define_initialized (static, lock); | |
435 | ||
436 | __libc_lock_lock (lock); | |
437 | ||
438 | /* Make sure there wasn't a second thread doing it already. */ | |
439 | result = (struct path_elem *) __gconv_path_elem; | |
440 | if (result == NULL) | |
441 | { | |
442 | /* Determine the complete path first. */ | |
d6204268 UD |
443 | char *gconv_path; |
444 | size_t gconv_path_len; | |
445 | char *elem; | |
446 | char *oldp; | |
447 | char *cp; | |
448 | int nelems; | |
40739d9f UD |
449 | char *cwd; |
450 | size_t cwdlen; | |
d6204268 | 451 | |
6b98979f | 452 | if (__gconv_path_envvar == NULL) |
d6204268 UD |
453 | { |
454 | /* No user-defined path. Make a modifiable copy of the | |
455 | default path. */ | |
456 | gconv_path = strdupa (default_gconv_path); | |
457 | gconv_path_len = sizeof (default_gconv_path); | |
40739d9f UD |
458 | cwd = NULL; |
459 | cwdlen = 0; | |
d6204268 UD |
460 | } |
461 | else | |
462 | { | |
463 | /* Append the default path to the user-defined path. */ | |
6b98979f | 464 | size_t user_len = strlen (__gconv_path_envvar); |
d6204268 UD |
465 | |
466 | gconv_path_len = user_len + 1 + sizeof (default_gconv_path); | |
467 | gconv_path = alloca (gconv_path_len); | |
6b98979f UD |
468 | __mempcpy (__mempcpy (__mempcpy (gconv_path, __gconv_path_envvar, |
469 | user_len), | |
d6204268 UD |
470 | ":", 1), |
471 | default_gconv_path, sizeof (default_gconv_path)); | |
40739d9f | 472 | cwd = __getcwd (NULL, 0); |
d19df6ac | 473 | cwdlen = __glibc_unlikely (cwd == NULL) ? 0 : strlen (cwd); |
d6204268 | 474 | } |
079e46f0 | 475 | assert (default_gconv_path[0] == '/'); |
d6204268 UD |
476 | |
477 | /* In a first pass we calculate the number of elements. */ | |
478 | oldp = NULL; | |
479 | cp = strchr (gconv_path, ':'); | |
480 | nelems = 1; | |
481 | while (cp != NULL) | |
482 | { | |
483 | if (cp != oldp + 1) | |
484 | ++nelems; | |
485 | oldp = cp; | |
486 | cp = strchr (cp + 1, ':'); | |
487 | } | |
488 | ||
489 | /* Allocate the memory for the result. */ | |
490 | result = (struct path_elem *) malloc ((nelems + 1) | |
491 | * sizeof (struct path_elem) | |
40739d9f UD |
492 | + gconv_path_len + nelems |
493 | + (nelems - 1) * (cwdlen + 1)); | |
d6204268 UD |
494 | if (result != NULL) |
495 | { | |
496 | char *strspace = (char *) &result[nelems + 1]; | |
497 | int n = 0; | |
498 | ||
499 | /* Separate the individual parts. */ | |
500 | __gconv_max_path_elem_len = 0; | |
501 | elem = __strtok_r (gconv_path, ":", &gconv_path); | |
502 | assert (elem != NULL); | |
503 | do | |
504 | { | |
505 | result[n].name = strspace; | |
40739d9f UD |
506 | if (elem[0] != '/') |
507 | { | |
508 | assert (cwd != NULL); | |
509 | strspace = __mempcpy (strspace, cwd, cwdlen); | |
510 | *strspace++ = '/'; | |
511 | } | |
d6204268 UD |
512 | strspace = __stpcpy (strspace, elem); |
513 | if (strspace[-1] != '/') | |
514 | *strspace++ = '/'; | |
515 | ||
516 | result[n].len = strspace - result[n].name; | |
517 | if (result[n].len > __gconv_max_path_elem_len) | |
518 | __gconv_max_path_elem_len = result[n].len; | |
519 | ||
520 | *strspace++ = '\0'; | |
521 | ++n; | |
522 | } | |
523 | while ((elem = __strtok_r (NULL, ":", &gconv_path)) != NULL); | |
524 | ||
525 | result[n].name = NULL; | |
526 | result[n].len = 0; | |
527 | } | |
528 | ||
8a0746ae | 529 | __gconv_path_elem = result ?: (struct path_elem *) &empty_path_elem; |
40739d9f | 530 | |
72e6cdfa | 531 | free (cwd); |
d6204268 UD |
532 | } |
533 | ||
534 | __libc_lock_unlock (lock); | |
535 | } | |
536 | ||
537 | ||
6973fc01 UD |
538 | /* Read all configuration files found in the user-specified and the default |
539 | path. */ | |
540 | void | |
dff07c4b | 541 | attribute_hidden |
6973fc01 UD |
542 | __gconv_read_conf (void) |
543 | { | |
6973fc01 UD |
544 | void *modules = NULL; |
545 | size_t nmodules = 0; | |
e34b0f29 | 546 | int save_errno = errno; |
5891046a | 547 | size_t cnt; |
6973fc01 | 548 | |
6b98979f UD |
549 | /* First see whether we should use the cache. */ |
550 | if (__gconv_load_cache () == 0) | |
551 | { | |
552 | /* Yes, we are done. */ | |
553 | __set_errno (save_errno); | |
554 | return; | |
555 | } | |
556 | ||
557 | #ifndef STATIC_GCONV | |
d6204268 UD |
558 | /* Find out where we have to look. */ |
559 | if (__gconv_path_elem == NULL) | |
560 | __gconv_get_path (); | |
6973fc01 | 561 | |
d6204268 | 562 | for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt) |
6973fc01 | 563 | { |
835bf8e0 UD |
564 | const char *elem = __gconv_path_elem[cnt].name; |
565 | size_t elem_len = __gconv_path_elem[cnt].len; | |
566 | char *filename; | |
567 | ||
568 | /* No slash needs to be inserted between elem and gconv_conf_filename; | |
569 | elem already ends in a slash. */ | |
570 | filename = alloca (elem_len + sizeof (gconv_conf_filename)); | |
571 | __mempcpy (__mempcpy (filename, elem, elem_len), | |
572 | gconv_conf_filename, sizeof (gconv_conf_filename)); | |
573 | ||
574 | /* Read the next configuration file. */ | |
575 | read_conf_file (filename, elem, elem_len, &modules, &nmodules); | |
6973fc01 | 576 | } |
6b98979f | 577 | #endif |
6973fc01 | 578 | |
2bd60880 UD |
579 | /* Add the internal modules. */ |
580 | for (cnt = 0; cnt < sizeof (builtin_modules) / sizeof (builtin_modules[0]); | |
581 | ++cnt) | |
6973fc01 | 582 | { |
d2dfc5de | 583 | struct gconv_alias fake_alias; |
6973fc01 | 584 | |
8a0746ae | 585 | fake_alias.fromname = (char *) builtin_modules[cnt].from_string; |
6973fc01 | 586 | |
d2dfc5de UD |
587 | if (__tfind (&fake_alias, &__gconv_alias_db, __gconv_alias_compare) |
588 | != NULL) | |
589 | /* It'll conflict so don't add it. */ | |
590 | continue; | |
2bd60880 | 591 | |
b2fe29dd | 592 | insert_module (&builtin_modules[cnt], 0); |
e34b0f29 | 593 | } |
6973fc01 | 594 | |
5891046a | 595 | /* Add aliases for builtin conversions. */ |
88dbff8c UD |
596 | const char *cp = builtin_aliases; |
597 | do | |
5891046a | 598 | { |
88dbff8c UD |
599 | const char *from = cp; |
600 | const char *to = __rawmemchr (from, '\0') + 1; | |
601 | cp = __rawmemchr (to, '\0') + 1; | |
602 | ||
603 | add_alias2 (from, to, cp, modules); | |
5891046a | 604 | } |
88dbff8c | 605 | while (*cp != '\0'); |
5891046a | 606 | |
e34b0f29 UD |
607 | /* Restore the error number. */ |
608 | __set_errno (save_errno); | |
6973fc01 | 609 | } |
d6204268 UD |
610 | |
611 | ||
612 | ||
613 | /* Free all resources if necessary. */ | |
c877418f | 614 | libc_freeres_fn (free_mem) |
d6204268 UD |
615 | { |
616 | if (__gconv_path_elem != NULL && __gconv_path_elem != &empty_path_elem) | |
617 | free ((void *) __gconv_path_elem); | |
618 | } |