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