locale.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* setlocale.c
  2. * Load LC_CTYPE and LC_COLLATE locale only special for uclibc
  3. *
  4. * Written by Vladimir Oleynik (c) vodz@usa.net
  5. *
  6. * This file is part of the uClibc C library and is distributed
  7. * under the GNU Library General Public License.
  8. * used ideas is part of the GNU C Library.
  9. */
  10. #include <locale.h>
  11. #include <stdio.h> /* NULL, fopen */
  12. #include <stdlib.h> /* malloc */
  13. #include <string.h>
  14. #include <limits.h> /* PATH_MAX */
  15. #include <errno.h> /* EINVAL */
  16. #include <unistd.h> /* get(e)[u|g]id */
  17. #include "_locale.h"
  18. static char C_LOCALE_NAME []="C";
  19. static char POSIX_LOCALE_NAME[]="POSIX";
  20. static char composite_name_C []=
  21. "LC_CTYPE=C;LC_NUMERIC=C;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C";
  22. #ifdef __UCLIBC_HAS_LOCALE__
  23. #ifdef TEST_LOCALE
  24. static const char PATH_LOCALE[]="./";
  25. #else
  26. static const char PATH_LOCALE[]=__UCLIBC_LOCALE_DIR;
  27. #endif
  28. struct SAV_LOADED_LOCALE {
  29. int category;
  30. char *locale;
  31. const unsigned char *buf;
  32. struct SAV_LOADED_LOCALE *next;
  33. };
  34. static struct SAV_LOADED_LOCALE sll_C_LC_MESSAGES = {
  35. LC_MESSAGES, C_LOCALE_NAME, 0, 0
  36. };
  37. static struct SAV_LOADED_LOCALE sll_C_LC_MONETARY = {
  38. LC_MONETARY, C_LOCALE_NAME, 0, &sll_C_LC_MESSAGES
  39. };
  40. static struct SAV_LOADED_LOCALE sll_C_LC_COLLATE = {
  41. LC_COLLATE, C_LOCALE_NAME, 0, &sll_C_LC_MONETARY
  42. };
  43. static struct SAV_LOADED_LOCALE sll_C_LC_TIME = {
  44. LC_TIME, C_LOCALE_NAME, 0, &sll_C_LC_COLLATE
  45. };
  46. static struct SAV_LOADED_LOCALE sll_C_LC_NUMERIC = {
  47. LC_NUMERIC, C_LOCALE_NAME, 0, &sll_C_LC_TIME
  48. };
  49. static struct SAV_LOADED_LOCALE sll_C_LC_CTYPE = {
  50. LC_CTYPE, C_LOCALE_NAME, _uc_ctype_b_C, &sll_C_LC_NUMERIC
  51. };
  52. static struct SAV_LOADED_LOCALE *sll = &sll_C_LC_CTYPE;
  53. #endif /* __UCLIBC_HAS_LOCALE__ */
  54. static char *nl_current[LC_ALL+1] = {
  55. C_LOCALE_NAME, C_LOCALE_NAME, C_LOCALE_NAME,
  56. C_LOCALE_NAME, C_LOCALE_NAME, C_LOCALE_NAME,
  57. composite_name_C
  58. };
  59. static const char * const LC_strs[LC_ALL+1] = {
  60. "/LC_CTYPE",
  61. "/LC_NUMERIC",
  62. "/LC_TIME",
  63. "/LC_COLLATE",
  64. "/LC_MONETARY",
  65. "/LC_MESSAGES",
  66. "/LC_ALL"
  67. };
  68. static char *find_locale(int c, const char **plocale)
  69. {
  70. #ifdef __UCLIBC_HAS_LOCALE__
  71. struct SAV_LOADED_LOCALE *cur;
  72. #endif
  73. const char *name = *plocale;
  74. if (name[0] == '\0') {
  75. /* The user decides which locale to use by setting environment
  76. variables. */
  77. name = getenv (&LC_strs[LC_ALL][1]);
  78. if (name == NULL || name[0] == '\0')
  79. name = getenv (&LC_strs[c][1]);
  80. if (name == NULL || name[0] == '\0')
  81. name = getenv ("LANG");
  82. if (name == NULL || name[0] == '\0')
  83. name = C_LOCALE_NAME;
  84. }
  85. if (strcmp (name, C_LOCALE_NAME) == 0 ||
  86. strcmp (name, POSIX_LOCALE_NAME) == 0 ||
  87. /* TODO! */ (c!=LC_CTYPE && c!=LC_COLLATE))
  88. name = C_LOCALE_NAME;
  89. *plocale = name;
  90. #ifdef __UCLIBC_HAS_LOCALE__
  91. for(cur = sll; cur; cur = cur->next)
  92. if(cur->category == c && strcmp(cur->locale, name)==0)
  93. return cur->locale;
  94. #else
  95. if(name == C_LOCALE_NAME)
  96. return C_LOCALE_NAME;
  97. #endif
  98. return NULL;
  99. }
  100. #ifdef __UCLIBC_HAS_LOCALE__
  101. static char *load_locale(int category, const char *locale)
  102. {
  103. FILE * fl;
  104. char full_path[PATH_MAX];
  105. char * buf = 0;
  106. struct SAV_LOADED_LOCALE *cur;
  107. struct SAV_LOADED_LOCALE *bottom;
  108. int bufsize;
  109. int l = strlen(locale);
  110. if((l+sizeof(PATH_LOCALE)+strlen(LC_strs[category]))>=PATH_MAX)
  111. return NULL;
  112. /* Not allow acces suid/sgid binaries to outside PATH_LOCALE */
  113. if((geteuid()!=getuid() || getegid()!=getgid()) &&
  114. strchr(locale, '/')!=NULL)
  115. return NULL;
  116. strcpy(full_path, PATH_LOCALE);
  117. strcat(full_path, locale);
  118. strcat(full_path, LC_strs[category]);
  119. fl = fopen(full_path, "r");
  120. if(fl==0)
  121. return NULL;
  122. switch(category) {
  123. case LC_CTYPE:
  124. bufsize = LOCALE_BUF_SIZE;
  125. break;
  126. case LC_COLLATE:
  127. bufsize = 256;
  128. break;
  129. default: /* TODO */
  130. bufsize = 0;
  131. break;
  132. }
  133. cur = malloc(sizeof(struct SAV_LOADED_LOCALE)+bufsize+l+2);
  134. if(cur) {
  135. buf = (char *)(cur+1);
  136. if(bufsize!=0 && fread(buf, 1, bufsize+1, fl)!=(bufsize)) {
  137. /* broken locale file */
  138. free(cur);
  139. buf = 0;
  140. #ifdef TEST_LOCALE
  141. fprintf(stderr, "\nbroken locale file\n");
  142. #endif
  143. }
  144. }
  145. fclose(fl);
  146. if(cur==0) /* not enough memory */
  147. return NULL;
  148. if(buf==0) { /* broken locale file, set to "C" */
  149. return C_LOCALE_NAME;
  150. }
  151. cur->next = 0;
  152. cur->buf = buf;
  153. cur->category = category;
  154. cur->locale = buf+bufsize;
  155. strcpy(cur->locale, locale);
  156. bottom = sll;
  157. while(bottom->next!=0)
  158. bottom = bottom->next;
  159. bottom->next = cur;
  160. return cur->locale;
  161. }
  162. static char *set_composite(int category, char *locale)
  163. {
  164. int i, l;
  165. char *old_composite_name = nl_current[LC_ALL];
  166. char *new_composite_name;
  167. struct SAV_LOADED_LOCALE *cur;
  168. for(l=i=0; i<LC_ALL; i++) {
  169. new_composite_name = i == category ? locale : nl_current[i];
  170. /* '=' + ';' or '\0' */
  171. l += strlen(&LC_strs[i][1])+strlen(new_composite_name)+2;
  172. }
  173. new_composite_name = malloc(l);
  174. if(new_composite_name==NULL)
  175. return NULL;
  176. if(old_composite_name!=composite_name_C)
  177. free(old_composite_name);
  178. nl_current[category] = locale; /* change after malloc */
  179. *new_composite_name = 0;
  180. for(i=0; i<LC_ALL; i++) {
  181. if(i)
  182. strcat(new_composite_name, ";");
  183. strcat(new_composite_name, &LC_strs[i][1]);
  184. strcat(new_composite_name, "=");
  185. strcat(new_composite_name, nl_current[i]);
  186. }
  187. nl_current[LC_ALL] = new_composite_name;
  188. /* set locale data for ctype and strcollate functions */
  189. for(cur = sll; ; cur = cur->next)
  190. if(cur->category == category && cur->locale == locale)
  191. break;
  192. switch(category) {
  193. case LC_CTYPE:
  194. _uc_ctype_b = cur->buf;
  195. _uc_ctype_trans = cur->buf+LOCALE_BUF_SIZE/2;
  196. break;
  197. case LC_COLLATE:
  198. _uc_collate_b = cur->buf;
  199. break;
  200. default: /* TODO */
  201. break;
  202. }
  203. return locale;
  204. }
  205. #endif /* __UCLIBC_HAS_LOCALE__ */
  206. char *setlocale(int category, const char *locale)
  207. {
  208. char * tl;
  209. #ifdef __UCLIBC_HAS_LOCALE__
  210. int i;
  211. #endif
  212. if (category < 0 || category > LC_ALL) {
  213. #ifdef __UCLIBC_HAS_LOCALE__
  214. einval:
  215. #endif
  216. errno = EINVAL;
  217. return NULL;
  218. }
  219. if(locale==NULL)
  220. return nl_current[category];
  221. if(category!=LC_ALL) {
  222. tl = find_locale(category, &locale);
  223. #ifdef __UCLIBC_HAS_LOCALE__
  224. if(tl==NULL)
  225. tl = load_locale(category, locale);
  226. if(tl) {
  227. if(nl_current[category] != tl)
  228. tl = set_composite(category, tl);
  229. }
  230. #endif
  231. return tl;
  232. }
  233. /* LC_ALL */
  234. #ifdef __UCLIBC_HAS_LOCALE__
  235. /* The user wants to set all categories. The desired locales
  236. for the individual categories can be selected by using a
  237. composite locale name. This is a semi-colon separated list
  238. of entries of the form `CATEGORY=VALUE'. */
  239. tl = strchr(locale, ';');
  240. if(tl==NULL) {
  241. /* This is not a composite name. Load the data for each category. */
  242. for(i=0; i<LC_ALL; i++)
  243. setlocale(i, locale); /* recursive */
  244. } else {
  245. /* This is a composite name. Make a copy and split it up. */
  246. const char *newnames[LC_ALL];
  247. char *np;
  248. char *cp;
  249. i = strlen(locale);
  250. np = alloca (i);
  251. if(np)
  252. strcpy(np, locale);
  253. else
  254. return NULL;
  255. for (i = 0; i < LC_ALL; ++i)
  256. newnames[i] = 0;
  257. while ((cp = strchr (np, '=')) != NULL) {
  258. for (i = 0; i < LC_ALL; ++i)
  259. if ((size_t) (cp - np) == strlen(&LC_strs[i][1])
  260. && memcmp (np, &LC_strs[i][1], (cp - np)) == 0)
  261. break;
  262. if (i == LC_ALL)
  263. /* Bogus category name. */
  264. goto einval;
  265. /* Found the category this clause sets. */
  266. newnames[i] = ++cp;
  267. cp = strchr (cp, ';');
  268. if (cp != NULL) {
  269. /* Examine the next clause. */
  270. *cp = '\0';
  271. np = cp + 1;
  272. } else
  273. /* This was the last clause. We are done. */
  274. break;
  275. }
  276. for (i = 0; i < LC_ALL; ++i)
  277. setlocale(i, newnames[i]); /* recursive */
  278. }
  279. #endif
  280. return nl_current[LC_ALL];
  281. }