localealias.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /* Handle aliases for locale names.
  2. Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. /* Tell glibc's <string.h> to provide a prototype for mempcpy().
  15. This must come before <config.h> because <config.h> may include
  16. <features.h>, and once <features.h> has been included, it's too late. */
  17. #ifndef _GNU_SOURCE
  18. # define _GNU_SOURCE 1
  19. #endif
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <sys/types.h>
  26. #ifdef __GNUC__
  27. # define alloca __builtin_alloca
  28. # define HAVE_ALLOCA 1
  29. #else
  30. # if defined HAVE_ALLOCA_H || defined _LIBC
  31. # include <alloca.h>
  32. # else
  33. # ifdef _AIX
  34. #pragma alloca
  35. # else
  36. # ifndef alloca
  37. char *alloca ();
  38. # endif
  39. # endif
  40. # endif
  41. #endif
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #if !HAVE_STRCHR && !defined _LIBC
  45. # ifndef strchr
  46. # define strchr index
  47. # endif
  48. #endif
  49. #include "gettextP.h"
  50. /* @@ end of prolog @@ */
  51. #ifdef _LIBC
  52. /* Rename the non ANSI C functions. This is required by the standard
  53. because some ANSI C functions will require linking with this object
  54. file and the name space must not be polluted. */
  55. # define strcasecmp __strcasecmp
  56. # ifndef mempcpy
  57. # define mempcpy __mempcpy
  58. # endif
  59. # define HAVE_MEMPCPY 1
  60. /* We need locking here since we can be called from different places. */
  61. # include <bits/libc-lock.h>
  62. __libc_lock_define_initialized (static, lock);
  63. #endif
  64. #ifndef internal_function
  65. # define internal_function
  66. #endif
  67. /* For those losing systems which don't have `alloca' we have to add
  68. some additional code emulating it. */
  69. #ifdef HAVE_ALLOCA
  70. # define freea(p) /* nothing */
  71. #else
  72. # define alloca(n) malloc (n)
  73. # define freea(p) free (p)
  74. #endif
  75. #if defined _LIBC_REENTRANT || defined HAVE_FGETS_UNLOCKED
  76. # undef fgets
  77. # define fgets(buf, len, s) fgets_unlocked (buf, len, s)
  78. #endif
  79. #if defined _LIBC_REENTRANT || defined HAVE_FEOF_UNLOCKED
  80. # undef feof
  81. # define feof(s) feof_unlocked (s)
  82. #endif
  83. struct alias_map
  84. {
  85. const char *alias;
  86. const char *value;
  87. };
  88. static char *string_space;
  89. static size_t string_space_act;
  90. static size_t string_space_max;
  91. static struct alias_map *map;
  92. static size_t nmap;
  93. static size_t maxmap;
  94. /* Prototypes for local functions. */
  95. static size_t read_alias_file PARAMS ((const char *fname, int fname_len))
  96. internal_function;
  97. static int extend_alias_table PARAMS ((void));
  98. static int alias_compare PARAMS ((const struct alias_map *map1,
  99. const struct alias_map *map2));
  100. const char *
  101. _nl_expand_alias (name)
  102. const char *name;
  103. {
  104. static const char *locale_alias_path = LOCALE_ALIAS_PATH;
  105. struct alias_map *retval;
  106. const char *result = NULL;
  107. size_t added;
  108. #ifdef _LIBC
  109. __libc_lock_lock (lock);
  110. #endif
  111. do
  112. {
  113. struct alias_map item;
  114. item.alias = name;
  115. if (nmap > 0)
  116. retval = (struct alias_map *) bsearch (&item, map, nmap,
  117. sizeof (struct alias_map),
  118. (int (*) PARAMS ((const void *,
  119. const void *))
  120. ) alias_compare);
  121. else
  122. retval = NULL;
  123. /* We really found an alias. Return the value. */
  124. if (retval != NULL)
  125. {
  126. result = retval->value;
  127. break;
  128. }
  129. /* Perhaps we can find another alias file. */
  130. added = 0;
  131. while (added == 0 && locale_alias_path[0] != '\0')
  132. {
  133. const char *start;
  134. while (locale_alias_path[0] == PATH_SEPARATOR)
  135. ++locale_alias_path;
  136. start = locale_alias_path;
  137. while (locale_alias_path[0] != '\0'
  138. && locale_alias_path[0] != PATH_SEPARATOR)
  139. ++locale_alias_path;
  140. if (start < locale_alias_path)
  141. added = read_alias_file (start, locale_alias_path - start);
  142. }
  143. }
  144. while (added != 0);
  145. #ifdef _LIBC
  146. __libc_lock_unlock (lock);
  147. #endif
  148. return result;
  149. }
  150. static size_t
  151. internal_function
  152. read_alias_file (fname, fname_len)
  153. const char *fname;
  154. int fname_len;
  155. {
  156. FILE *fp;
  157. char *full_fname;
  158. size_t added;
  159. static const char aliasfile[] = "/locale.alias";
  160. full_fname = (char *) alloca (fname_len + sizeof aliasfile);
  161. #ifdef HAVE_MEMPCPY
  162. mempcpy (mempcpy (full_fname, fname, fname_len),
  163. aliasfile, sizeof aliasfile);
  164. #else
  165. memcpy (full_fname, fname, fname_len);
  166. memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
  167. #endif
  168. fp = fopen (full_fname, "r");
  169. freea (full_fname);
  170. if (fp == NULL)
  171. return 0;
  172. added = 0;
  173. while (!feof (fp))
  174. {
  175. /* It is a reasonable approach to use a fix buffer here because
  176. a) we are only interested in the first two fields
  177. b) these fields must be usable as file names and so must not
  178. be that long
  179. */
  180. char buf[BUFSIZ];
  181. char *alias;
  182. char *value;
  183. char *cp;
  184. if (fgets (buf, sizeof buf, fp) == NULL)
  185. /* EOF reached. */
  186. break;
  187. /* Possibly not the whole line fits into the buffer. Ignore
  188. the rest of the line. */
  189. if (strchr (buf, '\n') == NULL)
  190. {
  191. char altbuf[BUFSIZ];
  192. do
  193. if (fgets (altbuf, sizeof altbuf, fp) == NULL)
  194. /* Make sure the inner loop will be left. The outer loop
  195. will exit at the `feof' test. */
  196. break;
  197. while (strchr (altbuf, '\n') == NULL);
  198. }
  199. cp = buf;
  200. /* Ignore leading white space. */
  201. while (isspace (cp[0]))
  202. ++cp;
  203. /* A leading '#' signals a comment line. */
  204. if (cp[0] != '\0' && cp[0] != '#')
  205. {
  206. alias = cp++;
  207. while (cp[0] != '\0' && !isspace (cp[0]))
  208. ++cp;
  209. /* Terminate alias name. */
  210. if (cp[0] != '\0')
  211. *cp++ = '\0';
  212. /* Now look for the beginning of the value. */
  213. while (isspace (cp[0]))
  214. ++cp;
  215. if (cp[0] != '\0')
  216. {
  217. size_t alias_len;
  218. size_t value_len;
  219. value = cp++;
  220. while (cp[0] != '\0' && !isspace (cp[0]))
  221. ++cp;
  222. /* Terminate value. */
  223. if (cp[0] == '\n')
  224. {
  225. /* This has to be done to make the following test
  226. for the end of line possible. We are looking for
  227. the terminating '\n' which do not overwrite here. */
  228. *cp++ = '\0';
  229. *cp = '\n';
  230. }
  231. else if (cp[0] != '\0')
  232. *cp++ = '\0';
  233. if (nmap >= maxmap)
  234. if (__builtin_expect (extend_alias_table (), 0))
  235. return added;
  236. alias_len = strlen (alias) + 1;
  237. value_len = strlen (value) + 1;
  238. if (string_space_act + alias_len + value_len > string_space_max)
  239. {
  240. /* Increase size of memory pool. */
  241. size_t new_size = (string_space_max
  242. + (alias_len + value_len > 1024
  243. ? alias_len + value_len : 1024));
  244. char *new_pool = (char *) realloc (string_space, new_size);
  245. if (new_pool == NULL)
  246. return added;
  247. if (__builtin_expect (string_space != new_pool, 0))
  248. {
  249. size_t i;
  250. for (i = 0; i < nmap; i++)
  251. {
  252. map[i].alias += new_pool - string_space;
  253. map[i].value += new_pool - string_space;
  254. }
  255. }
  256. string_space = new_pool;
  257. string_space_max = new_size;
  258. }
  259. map[nmap].alias = memcpy (&string_space[string_space_act],
  260. alias, alias_len);
  261. string_space_act += alias_len;
  262. map[nmap].value = memcpy (&string_space[string_space_act],
  263. value, value_len);
  264. string_space_act += value_len;
  265. ++nmap;
  266. ++added;
  267. }
  268. }
  269. }
  270. /* Should we test for ferror()? I think we have to silently ignore
  271. errors. --drepper */
  272. fclose (fp);
  273. if (added > 0)
  274. qsort (map, nmap, sizeof (struct alias_map),
  275. (int (*) PARAMS ((const void *, const void *))) alias_compare);
  276. return added;
  277. }
  278. static int
  279. extend_alias_table ()
  280. {
  281. size_t new_size;
  282. struct alias_map *new_map;
  283. new_size = maxmap == 0 ? 100 : 2 * maxmap;
  284. new_map = (struct alias_map *) realloc (map, (new_size
  285. * sizeof (struct alias_map)));
  286. if (new_map == NULL)
  287. /* Simply don't extend: we don't have any more core. */
  288. return -1;
  289. map = new_map;
  290. maxmap = new_size;
  291. return 0;
  292. }
  293. #ifdef _LIBC
  294. static void __attribute__ ((unused))
  295. free_mem (void)
  296. {
  297. if (string_space != NULL)
  298. free (string_space);
  299. if (map != NULL)
  300. free (map);
  301. }
  302. text_set_element (__libc_subfreeres, free_mem);
  303. #endif
  304. static int
  305. alias_compare (map1, map2)
  306. const struct alias_map *map1;
  307. const struct alias_map *map2;
  308. {
  309. #if defined _LIBC || defined HAVE_STRCASECMP
  310. return strcasecmp (map1->alias, map2->alias);
  311. #else
  312. const unsigned char *p1 = (const unsigned char *) map1->alias;
  313. const unsigned char *p2 = (const unsigned char *) map2->alias;
  314. unsigned char c1, c2;
  315. if (p1 == p2)
  316. return 0;
  317. do
  318. {
  319. /* I know this seems to be odd but the tolower() function in
  320. some systems libc cannot handle nonalpha characters. */
  321. c1 = isupper (*p1) ? tolower (*p1) : *p1;
  322. c2 = isupper (*p2) ? tolower (*p2) : *p2;
  323. if (c1 == '\0')
  324. break;
  325. ++p1;
  326. ++p2;
  327. }
  328. while (c1 == c2);
  329. return c1 - c2;
  330. #endif
  331. }