localealias.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Handle aliases for locale names.
  2. Copyright (C) 1995-2026 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 <https://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. static char *string_space;
  107. static size_t string_space_act;
  108. static size_t string_space_max;
  109. static struct alias_map *map;
  110. static size_t nmap;
  111. static size_t maxmap;
  112. /* Prototypes for local functions. */
  113. static size_t read_alias_file (const char *fname, int fname_len);
  114. static int extend_alias_table (void);
  115. static int alias_compare (const struct alias_map *map1,
  116. const struct alias_map *map2);
  117. const char *
  118. _nl_expand_alias (const char *name)
  119. {
  120. static const char *locale_alias_path;
  121. struct alias_map *retval;
  122. const char *result = NULL;
  123. size_t added;
  124. __libc_lock_lock (lock);
  125. if (locale_alias_path == NULL)
  126. locale_alias_path = LOCALE_ALIAS_PATH;
  127. do
  128. {
  129. struct alias_map item;
  130. item.alias = name;
  131. if (nmap > 0)
  132. retval = (struct alias_map *) bsearch (&item, map, nmap,
  133. sizeof (struct alias_map),
  134. (int (*) (const void *,
  135. const void *)
  136. ) alias_compare);
  137. else
  138. retval = NULL;
  139. /* We really found an alias. Return the value. */
  140. if (retval != NULL)
  141. {
  142. result = retval->value;
  143. break;
  144. }
  145. /* Perhaps we can find another alias file. */
  146. added = 0;
  147. while (added == 0 && locale_alias_path[0] != '\0')
  148. {
  149. const char *start;
  150. while (locale_alias_path[0] == PATH_SEPARATOR)
  151. ++locale_alias_path;
  152. start = locale_alias_path;
  153. while (locale_alias_path[0] != '\0'
  154. && locale_alias_path[0] != PATH_SEPARATOR)
  155. ++locale_alias_path;
  156. if (start < locale_alias_path)
  157. added = read_alias_file (start, locale_alias_path - start);
  158. }
  159. }
  160. while (added != 0);
  161. __libc_lock_unlock (lock);
  162. return result;
  163. }
  164. static size_t
  165. read_alias_file (const char *fname, int fname_len)
  166. {
  167. FILE *fp;
  168. char *full_fname;
  169. size_t added;
  170. static const char aliasfile[] = "/locale.alias";
  171. full_fname = (char *) alloca (fname_len + sizeof aliasfile);
  172. #ifdef HAVE_MEMPCPY
  173. mempcpy (mempcpy (full_fname, fname, fname_len),
  174. aliasfile, sizeof aliasfile);
  175. #else
  176. memcpy (full_fname, fname, fname_len);
  177. memcpy (&full_fname[fname_len], aliasfile, sizeof aliasfile);
  178. #endif
  179. #ifdef _LIBC
  180. /* Note the file is opened with cancellation in the I/O functions
  181. disabled. */
  182. fp = fopen (relocate (full_fname), "rce");
  183. #else
  184. fp = fopen (relocate (full_fname), "r");
  185. #endif
  186. freea (full_fname);
  187. if (fp == NULL)
  188. return 0;
  189. #ifdef HAVE___FSETLOCKING
  190. /* No threads present. */
  191. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  192. #endif
  193. added = 0;
  194. while (!FEOF (fp))
  195. {
  196. /* It is a reasonable approach to use a fix buffer here because
  197. a) we are only interested in the first two fields
  198. b) these fields must be usable as file names and so must not
  199. be that long
  200. We avoid a multi-kilobyte buffer here since this would use up
  201. stack space which we might not have if the program ran out of
  202. memory. */
  203. char buf[400];
  204. char *alias;
  205. char *value;
  206. char *cp;
  207. int complete_line;
  208. if (FGETS (buf, sizeof buf, fp) == NULL)
  209. /* EOF reached. */
  210. break;
  211. /* Determine whether the line is complete. */
  212. complete_line = strchr (buf, '\n') != NULL;
  213. cp = buf;
  214. /* Ignore leading white space. */
  215. while (isspace ((unsigned char) cp[0]))
  216. ++cp;
  217. /* A leading '#' signals a comment line. */
  218. if (cp[0] != '\0' && cp[0] != '#')
  219. {
  220. alias = cp++;
  221. while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
  222. ++cp;
  223. /* Terminate alias name. */
  224. if (cp[0] != '\0')
  225. *cp++ = '\0';
  226. /* Now look for the beginning of the value. */
  227. while (isspace ((unsigned char) cp[0]))
  228. ++cp;
  229. if (cp[0] != '\0')
  230. {
  231. value = cp++;
  232. while (cp[0] != '\0' && !isspace ((unsigned char) cp[0]))
  233. ++cp;
  234. /* Terminate value. */
  235. if (cp[0] == '\n')
  236. {
  237. /* This has to be done to make the following test
  238. for the end of line possible. We are looking for
  239. the terminating '\n' which do not overwrite here. */
  240. *cp++ = '\0';
  241. *cp = '\n';
  242. }
  243. else if (cp[0] != '\0')
  244. *cp++ = '\0';
  245. #ifdef IN_LIBGLOCALE
  246. /* glibc's locale.alias contains entries for ja_JP and ko_KR
  247. that make it impossible to use a Japanese or Korean UTF-8
  248. locale under the name "ja_JP" or "ko_KR". Ignore these
  249. entries. */
  250. if (strchr (alias, '_') == NULL)
  251. #endif
  252. {
  253. size_t alias_len;
  254. size_t value_len;
  255. if (nmap >= maxmap)
  256. if (__builtin_expect (extend_alias_table (), 0))
  257. goto out;
  258. alias_len = strlen (alias) + 1;
  259. value_len = strlen (value) + 1;
  260. if (string_space_act + alias_len + value_len > string_space_max)
  261. {
  262. #pragma GCC diagnostic push
  263. #if defined __GNUC__ && __GNUC__ >= 12
  264. /* Suppress the valid GCC 12 warning until the code below is changed
  265. to avoid using pointers to the reallocated block. */
  266. # pragma GCC diagnostic ignored "-Wuse-after-free"
  267. #endif
  268. /* Increase size of memory pool. */
  269. size_t new_size = (string_space_max
  270. + (alias_len + value_len > 1024
  271. ? alias_len + value_len : 1024));
  272. char *new_pool = (char *) realloc (string_space, new_size);
  273. if (new_pool == NULL)
  274. goto out;
  275. if (__builtin_expect (string_space != new_pool, 0))
  276. {
  277. size_t i;
  278. for (i = 0; i < nmap; i++)
  279. {
  280. map[i].alias += new_pool - string_space;
  281. map[i].value += new_pool - string_space;
  282. }
  283. }
  284. string_space = new_pool;
  285. string_space_max = new_size;
  286. }
  287. map[nmap].alias =
  288. (const char *) memcpy (&string_space[string_space_act],
  289. alias, alias_len);
  290. string_space_act += alias_len;
  291. map[nmap].value =
  292. (const char *) memcpy (&string_space[string_space_act],
  293. value, value_len);
  294. string_space_act += value_len;
  295. #pragma GCC diagnostic pop
  296. ++nmap;
  297. ++added;
  298. }
  299. }
  300. }
  301. /* Possibly not the whole line fits into the buffer. Ignore
  302. the rest of the line. */
  303. if (! complete_line)
  304. do
  305. if (FGETS (buf, sizeof buf, fp) == NULL)
  306. /* Make sure the inner loop will be left. The outer loop
  307. will exit at the `feof' test. */
  308. break;
  309. while (strchr (buf, '\n') == NULL);
  310. }
  311. out:
  312. /* Should we test for ferror()? I think we have to silently ignore
  313. errors. --drepper */
  314. fclose (fp);
  315. if (added > 0)
  316. qsort (map, nmap, sizeof (struct alias_map),
  317. (int (*) (const void *, const void *)) alias_compare);
  318. return added;
  319. }
  320. static int
  321. extend_alias_table (void)
  322. {
  323. size_t new_size;
  324. struct alias_map *new_map;
  325. new_size = maxmap == 0 ? 100 : 2 * maxmap;
  326. new_map = (struct alias_map *) realloc (map, (new_size
  327. * sizeof (struct alias_map)));
  328. if (new_map == NULL)
  329. /* Simply don't extend: we don't have any more core. */
  330. return -1;
  331. map = new_map;
  332. maxmap = new_size;
  333. return 0;
  334. }
  335. static int
  336. alias_compare (const struct alias_map *map1, const struct alias_map *map2)
  337. {
  338. #if defined _LIBC || defined HAVE_STRCASECMP
  339. return strcasecmp (map1->alias, map2->alias);
  340. #else
  341. const unsigned char *p1 = (const unsigned char *) map1->alias;
  342. const unsigned char *p2 = (const unsigned char *) map2->alias;
  343. unsigned char c1, c2;
  344. if (p1 == p2)
  345. return 0;
  346. do
  347. {
  348. /* I know this seems to be odd but the tolower() function in
  349. some systems libc cannot handle nonalpha characters. */
  350. c1 = isupper (*p1) ? tolower (*p1) : *p1;
  351. c2 = isupper (*p2) ? tolower (*p2) : *p2;
  352. if (c1 == '\0')
  353. break;
  354. ++p1;
  355. ++p2;
  356. }
  357. while (c1 == c2);
  358. return c1 - c2;
  359. #endif
  360. }
  361. void
  362. __libc_localealias_freemem (void)
  363. {
  364. free (string_space);
  365. free (map);
  366. }