locale.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* setlocale.c
  2. * Load LC_CTYPE 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. /*
  11. * No-locale-support setlocale() added.
  12. */
  13. #include <locale.h>
  14. #include <stdio.h> /* NULL, fopen */
  15. #include <stdlib.h> /* malloc */
  16. #include <string.h>
  17. #include <limits.h> /* PATH_MAX */
  18. #include "../ctype/ctype.h"
  19. #undef TEST_LOCALE
  20. #ifdef __UCLIBC_HAS_LOCALE__
  21. static char C_LOCALE_NAME[]="C";
  22. #ifdef TEST_LOCALE
  23. static const char PATH_LOCALE[]="./";
  24. #else
  25. static const char PATH_LOCALE[]=__UCLIBC_LOCALE_DIR;
  26. #endif
  27. static const char LC_CTYPE_STR[]="/LC_CTYPE";
  28. struct SAV_LOADED_LOCALE {
  29. char *locale;
  30. const unsigned char *buf;
  31. struct SAV_LOADED_LOCALE *next;
  32. };
  33. static struct SAV_LOADED_LOCALE sav_loaded_locale [1] = {
  34. { C_LOCALE_NAME, _uc_ctype_b_C, 0 }
  35. };
  36. static struct SAV_LOADED_LOCALE * old_locale = sav_loaded_locale;
  37. static char *set_new_locale(struct SAV_LOADED_LOCALE * s_locale)
  38. {
  39. _uc_ctype_b = s_locale->buf;
  40. _uc_ctype_trans = s_locale->buf+LOCALE_BUF_SIZE/2;
  41. old_locale = s_locale;
  42. return s_locale->locale;
  43. }
  44. /* Current support only LC_CTYPE or LC_ALL category */
  45. char *setlocale(int category, const char *locale)
  46. {
  47. FILE * fl;
  48. struct SAV_LOADED_LOCALE *cur;
  49. struct SAV_LOADED_LOCALE *bottom;
  50. char full_path[PATH_MAX];
  51. char * buf = 0;
  52. int l;
  53. if(category!=LC_CTYPE && category!=LC_ALL)
  54. return NULL;
  55. if(locale==0)
  56. return set_new_locale(old_locale);
  57. if(strcmp(locale, "POSIX")==0)
  58. return set_new_locale(sav_loaded_locale);
  59. else if(*locale == '\0') {
  60. locale = getenv(LC_CTYPE_STR+1);
  61. if(locale == 0 || *locale == 0)
  62. locale = getenv("LANG");
  63. if(locale == 0 || *locale == '\0')
  64. return set_new_locale(old_locale);
  65. if(strcmp(locale, "POSIX")==0)
  66. return set_new_locale(sav_loaded_locale);
  67. }
  68. for(cur = sav_loaded_locale; cur; cur = cur->next)
  69. if(strcmp(cur->locale, locale)==0)
  70. return set_new_locale(cur);
  71. l = strlen(locale);
  72. if((l+sizeof(PATH_LOCALE)+sizeof(LC_CTYPE_STR))>=PATH_MAX)
  73. return NULL;
  74. strcpy(full_path, PATH_LOCALE);
  75. strcat(full_path, locale);
  76. strcat(full_path, LC_CTYPE_STR);
  77. fl = fopen(full_path, "r");
  78. if(fl==0)
  79. return NULL;
  80. cur = malloc(sizeof(struct SAV_LOADED_LOCALE)+LOCALE_BUF_SIZE+l);
  81. if(cur) {
  82. buf = (char *)(cur+1);
  83. if(fread(buf, 1, LOCALE_BUF_SIZE+1, fl)!=(LOCALE_BUF_SIZE)) {
  84. /* broken locale file */
  85. free(cur);
  86. buf = 0;
  87. #ifdef TEST_LOCALE
  88. fprintf(stderr, "\nbroken locale file\n");
  89. #endif
  90. }
  91. }
  92. fclose(fl);
  93. if(cur==0) /* not enough memory */
  94. return NULL;
  95. if(buf==0) /* broken locale file, set to "C" */
  96. return set_new_locale(sav_loaded_locale);
  97. cur->next = 0;
  98. strcpy(buf+LOCALE_BUF_SIZE, locale);
  99. bottom = sav_loaded_locale;
  100. while(bottom->next!=0)
  101. bottom = bottom->next;
  102. bottom->next = cur;
  103. /* next two line only pedantic */
  104. cur->buf = buf;
  105. cur->locale = buf+LOCALE_BUF_SIZE;
  106. return set_new_locale(cur);
  107. }
  108. #else /* no locale support */
  109. char *setlocale(int category, const char *locale)
  110. {
  111. /* Allow locales "C" and "" (native). Both are "C" for our purposes. */
  112. if (locale) {
  113. if (*locale == 'C') {
  114. ++locale;
  115. }
  116. if (*locale) { /* locale wasn't "C" or ""!! */
  117. return NULL;
  118. }
  119. }
  120. /* Allow any legal category for "C" or "" (native) locale. */
  121. if((category < LC_CTYPE) || (category > LC_ALL)) { /* Illegal category! */
  122. return NULL;
  123. }
  124. return "C";
  125. }
  126. #endif