localealias.c 10 KB

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