You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

localealias.c 9.7KB

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