locale.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* TODO:
  18. * Implement the shared mmap code so non-mmu platforms can use this.
  19. * Add some basic collate functionality similar to what the previous
  20. * locale support had (8-bit codesets only).
  21. */
  22. #define _GNU_SOURCE
  23. #include <locale.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stddef.h>
  27. #include <limits.h>
  28. #include <stdint.h>
  29. #include <assert.h>
  30. #ifndef __LOCALE_C_ONLY
  31. #define CUR_LOCALE_SPEC (__global_locale.cur_locale)
  32. #undef CODESET_LIST
  33. #define CODESET_LIST (__locale_mmap->codeset_list)
  34. /* TODO: Optional... See below. */
  35. #define __LOCALE_STRICTER_SETLOCALE
  36. #endif /* __LOCALE_C_ONLY */
  37. /**********************************************************************/
  38. #ifdef L_setlocale
  39. #ifdef __LOCALE_C_ONLY
  40. link_warning(setlocale,"the 'setlocale' function supports only C|POSIX locales");
  41. static const char C_string[] = "C";
  42. char *setlocale(int category, register const char *locale)
  43. {
  44. return ( (((unsigned int)(category)) <= LC_ALL)
  45. && ( (!locale) /* Request for locale category string. */
  46. || (!*locale) /* Implementation-defined default is C. */
  47. || ((*locale == 'C') && !locale[1])
  48. || (!strcmp(locale, "POSIX"))) )
  49. ? (char *) C_string /* Always in C/POSIX locale. */
  50. : NULL;
  51. }
  52. #else /* ---------------------------------------------- __LOCALE_C_ONLY */
  53. #if !defined(NUM_LOCALES) || (NUM_LOCALES <= 1)
  54. #error locales enabled, but not data other than for C locale!
  55. #endif
  56. static unsigned char setlocale_buf[LOCALE_STRING_SIZE];
  57. #define LOCALE_NAMES (__locale_mmap->locale_names5)
  58. #define LOCALES (__locale_mmap->locales)
  59. #define LOCALE_AT_MODIFIERS (__locale_mmap->locale_at_modifiers)
  60. #define CATEGORY_NAMES (__locale_mmap->lc_names)
  61. static const char posix[] = "POSIX";
  62. static int find_locale(int category, const char *p, unsigned char *new_locale)
  63. {
  64. int i;
  65. const unsigned char *s;
  66. uint16_t n;
  67. unsigned char lang_cult, codeset;
  68. #if defined(LOCALE_AT_MODIFIERS_LENGTH) && 1
  69. /* Support standard locale handling for @-modifiers. */
  70. char buf[18]; /* TODO: 7+{max codeset name length} */
  71. const char *q;
  72. if ((q = strchr(p,'@')) != NULL) {
  73. if ((((size_t)((q-p)-5)) > (sizeof(buf) - 5)) || (p[2] != '_')) {
  74. return 0;
  75. }
  76. /* locale name at least 5 chars long and 3rd char is '_' */
  77. s = LOCALE_AT_MODIFIERS;
  78. do {
  79. if (!strcmp(s+2, q+1)) {
  80. break;
  81. }
  82. s += 2 + *s; /* TODO - fix this throughout */
  83. } while (*s);
  84. if (!*s) {
  85. return 0;
  86. }
  87. memcpy(buf, p, q-p);
  88. buf[q-p] = 0;
  89. buf[2] = s[1];
  90. p = buf;
  91. }
  92. #endif
  93. lang_cult = codeset = 0; /* Assume C and default codeset. */
  94. if (((*p == 'C') && !p[1]) || !strcmp(p, posix)) {
  95. goto FIND_LOCALE;
  96. }
  97. if (p[5] == '.') { /* Codeset specified in locale name? */
  98. /* TODO: maybe CODESET_LIST + *s ??? */
  99. /* 7bit is 1, UTF-8 is 2, 8-bit is >= 3 */
  100. codeset = 2;
  101. if (strcmp("UTF-8",p+6) != 0) {/* TODO - fix! */
  102. s = CODESET_LIST;
  103. do {
  104. ++codeset; /* Increment codeset first. */
  105. if (!strcmp(CODESET_LIST+*s, p+6)) {
  106. goto FIND_LANG_CULT;
  107. }
  108. } while (*++s);
  109. return 0; /* No matching codeset! */
  110. }
  111. }
  112. FIND_LANG_CULT: /* Find language_culture number. */
  113. s = LOCALE_NAMES;
  114. do { /* TODO -- do a binary search? */
  115. /* TODO -- fix gen_mmap!*/
  116. ++lang_cult; /* Increment first since C/POSIX is 0. */
  117. if (!strncmp(s,p,5)) { /* Found a matching locale name; */
  118. goto FIND_LOCALE;
  119. }
  120. s += 5;
  121. } while (lang_cult < NUM_LOCALE_NAMES);
  122. return 0; /* No matching language_culture! */
  123. FIND_LOCALE: /* Find locale row matching name and codeset */
  124. s = LOCALES;
  125. n = 1;
  126. do { /* TODO -- do a binary search? */
  127. if ((lang_cult == *s) && ((codeset == s[1]) || (codeset == s[2]))) {
  128. i = ((category == LC_ALL) ? 0 : category);
  129. s = new_locale + 2*i;
  130. do {
  131. /* Encode current locale row number. */
  132. *((unsigned char *) ++s) = (n >> 8) | 0x80;
  133. *((unsigned char *) ++s) = n & 0xff;
  134. } while (++i < category);
  135. return i; /* Return non-zero */
  136. }
  137. s += WIDTH_LOCALES;
  138. ++n;
  139. } while (n <= NUM_LOCALES); /* We started at 1!!! */
  140. return 0; /* Unsupported locale. */
  141. }
  142. char *setlocale(int category, const char *locale)
  143. {
  144. const unsigned char *p;
  145. unsigned char *s;
  146. int i;
  147. unsigned lc_mask;
  148. unsigned char new_locale[LOCALE_STRING_SIZE];
  149. if (((unsigned int)(category)) > LC_ALL) {
  150. /* TODO - set errno? SUSv3 doesn't say too. */
  151. return NULL; /* Illegal/unsupported category. */
  152. }
  153. lc_mask = 1 << category;
  154. if (category == LC_ALL) {
  155. --lc_mask;
  156. }
  157. if (!locale) { /* Request for locale category string... */
  158. DONE:
  159. strcpy(setlocale_buf, CUR_LOCALE_SPEC);
  160. #ifdef __LOCALE_STRICTER_SETLOCALE
  161. /* The standard says you can only use the string returned to restore
  162. * the category (categories) requested. This could be optional.
  163. * See below as well. */
  164. s = setlocale_buf + 1;
  165. lc_mask |= (1 << LC_ALL);
  166. do {
  167. if (!(lc_mask & 1)) {
  168. /* Encode non-selected locale flag. */
  169. s[1] = *s = 0xff;
  170. }
  171. s += 2;
  172. } while ((lc_mask >>= 1) > 1);
  173. #endif /* __LOCALE_STRICTER_SETLOCALE */
  174. return (char *) setlocale_buf;
  175. }
  176. strcpy(new_locale, CUR_LOCALE_SPEC); /* Start with current. */
  177. if (!*locale) { /* locale == "", so check environment. */
  178. i = ((category == LC_ALL) ? 0 : category);
  179. do {
  180. /* Note: SUSv3 doesn't define a fallback mechanism here. So,
  181. * if LC_ALL is invalid, we do _not_ continue trying the other
  182. * environment vars. */
  183. if (!(p = getenv("LC_ALL"))) {
  184. if (!(p = getenv(CATEGORY_NAMES + CATEGORY_NAMES[i]))) {
  185. if (!(p = getenv("LANG"))) {
  186. p = posix;
  187. }
  188. }
  189. }
  190. /* The user set something... is it valid? */
  191. /* Note: Since we don't support user-supplied locales and
  192. * alternate paths, we don't need to worry about special
  193. * handling for suid/sgid apps. */
  194. if (!find_locale(i, p, new_locale)) {
  195. return NULL;
  196. }
  197. } while (++i < category);
  198. } else if (*locale == '#') { /* Previsouly returned value. */
  199. assert(strlen(locale) == LOCALE_STRING_SIZE - 1);
  200. i = ((category == LC_ALL) ? 0 : category);
  201. p = locale + 2*i;
  202. s = new_locale + 2*i;
  203. do {
  204. #ifdef __LOCALE_STRICTER_SETLOCALE
  205. /* Only set categories that were selected in the previous
  206. * return value. Could be optional. See above as well.
  207. * NOTE: This still isn't quite right for non-LC_ALL
  208. * as it only checks the category selected to set. */
  209. if ((*p == 0xff) && (p[1] == 0xff)) {
  210. return NULL;
  211. }
  212. #endif /* __LOCALE_STRICTER_SETLOCALE */
  213. /* Note: Validate settings below. */
  214. *++s = *++p;
  215. *++s = *++p;
  216. } while (++i < category);
  217. } else if (!find_locale(category, locale, new_locale)) {
  218. return NULL;
  219. }
  220. /* TODO: Ok, everything checks out, so install the new locale. */
  221. _locale_set(new_locale);
  222. /* Everything ok, so make a copy in setlocale_buf and return. */
  223. goto DONE;
  224. }
  225. #endif /* __LOCALE_C_ONLY */
  226. #endif
  227. /**********************************************************************/
  228. #ifdef L_localeconv
  229. /* Note: We assume here that the compiler does the sane thing regarding
  230. * placement of the fields in the struct. If necessary, we could ensure
  231. * this usings an array of offsets but at some size cost. */
  232. #ifdef __LOCALE_C_ONLY
  233. #warning localeconv is hardwired for C/POSIX locale only
  234. link_warning(localeconv,"the 'localeconv' function is hardwired for C/POSIX locale only");
  235. static struct lconv the_lconv;
  236. static const char decpt[] = ".";
  237. struct lconv *localeconv(void)
  238. {
  239. register char *p = (char *)(&the_lconv);
  240. *((char **)p) = (char *) decpt;
  241. do {
  242. p += sizeof(char **);
  243. *((char **)p) = (char *) (decpt+1);
  244. } while (p < (char *) &the_lconv.negative_sign);
  245. p = (&the_lconv.int_frac_digits);
  246. do {
  247. *p = CHAR_MAX;
  248. ++p;
  249. } while (p <= &the_lconv.int_n_sign_posn);
  250. return &the_lconv;
  251. }
  252. #else /* __LOCALE_C_ONLY */
  253. static struct lconv the_lconv;
  254. struct lconv *localeconv(void)
  255. {
  256. register char *p = (char *) &the_lconv;
  257. register char **q = (char **) &__global_locale.decimal_point;
  258. do {
  259. *((char **)p) = *q;
  260. p += sizeof(char **);
  261. ++q;
  262. } while (p < &the_lconv.int_frac_digits);
  263. do {
  264. *p = **q;
  265. ++p;
  266. ++q;
  267. } while (p <= &the_lconv.int_n_sign_posn);
  268. return &the_lconv;
  269. }
  270. #endif /* __LOCALE_C_ONLY */
  271. #endif
  272. /**********************************************************************/
  273. #ifdef L__locale_init
  274. #ifndef __LOCALE_C_ONLY
  275. #define C_LOCALE_SELECTOR "\x23\x80\x01\x80\x01\x80\x01\x80\x01\x80\x01\x80\x01"
  276. #define LOCALE_INIT_FAILED "locale init failed!\n"
  277. #define CUR_LOCALE_SPEC (__global_locale.cur_locale)
  278. __locale_t __global_locale;
  279. void _locale_init(void)
  280. {
  281. /* TODO: mmap the locale file */
  282. /* TODO - ??? */
  283. memset(CUR_LOCALE_SPEC, 0, LOCALE_STRING_SIZE);
  284. CUR_LOCALE_SPEC[0] = '#';
  285. memcpy(__global_locale.category_item_count,
  286. __locale_mmap->lc_common_item_offsets_LEN,
  287. LC_ALL);
  288. __global_locale.category_offsets[0] = offsetof(__locale_t, codeset);
  289. __global_locale.category_offsets[1] = offsetof(__locale_t, decimal_point);
  290. __global_locale.category_offsets[2] = offsetof(__locale_t, int_curr_symbol);
  291. __global_locale.category_offsets[3] = offsetof(__locale_t, abday_1);
  292. /* __global_locale.category_offsets[4] = offsetof(__locale_t, collate???); */
  293. __global_locale.category_offsets[5] = offsetof(__locale_t, yesexpr);
  294. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  295. __global_locale.tbl8ctype
  296. = (const unsigned char *) &__locale_mmap->tbl8ctype;
  297. __global_locale.tbl8uplow
  298. = (const unsigned char *) &__locale_mmap->tbl8uplow;
  299. #ifdef __WCHAR_ENABLED
  300. __global_locale.tbl8c2wc
  301. = (const uint16_t *) &__locale_mmap->tbl8c2wc;
  302. __global_locale.tbl8wc2c
  303. = (const unsigned char *) &__locale_mmap->tbl8wc2c;
  304. /* translit */
  305. #endif /* __WCHAR_ENABLED */
  306. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  307. #ifdef __WCHAR_ENABLED
  308. __global_locale.tblwctype
  309. = (const unsigned char *) &__locale_mmap->tblwctype;
  310. __global_locale.tblwuplow
  311. = (const unsigned char *) &__locale_mmap->tblwuplow;
  312. __global_locale.tblwuplow_diff
  313. = (const uint16_t *) &__locale_mmap->tblwuplow_diff;
  314. __global_locale.tblwcomb
  315. = (const unsigned char *) &__locale_mmap->tblwcomb;
  316. /* width?? */
  317. #endif /* __WCHAR_ENABLED */
  318. _locale_set(C_LOCALE_SELECTOR);
  319. }
  320. static const char ascii[] = "ASCII";
  321. static const char utf8[] = "UTF-8";
  322. void _locale_set(const unsigned char *p)
  323. {
  324. const char **x;
  325. unsigned char *s = CUR_LOCALE_SPEC + 1;
  326. const size_t *stp;
  327. const unsigned char *r;
  328. const uint16_t *io;
  329. const uint16_t *ii;
  330. const unsigned char *d;
  331. int row; /* locale row */
  332. int crow; /* category row */
  333. int len;
  334. int c;
  335. int i = 0;
  336. ++p;
  337. do {
  338. if ((*p != *s) || (p[1] != s[1])) {
  339. row = (((int)(*p & 0x7f)) << 8) + p[1] - 1;
  340. #ifndef NDEBUG
  341. assert(row < NUM_LOCALES);
  342. #endif
  343. *s = *p;
  344. s[1] = p[1];
  345. if (i == LC_CTYPE) {
  346. c = __locale_mmap->locales[ WIDTH_LOCALES * row + 2 ]; /* codeset */
  347. if (c <= 2) {
  348. if (c == 2) {
  349. __global_locale.codeset = utf8;
  350. __global_locale.encoding = __ctype_encoding_utf8;
  351. /* TODO - fix for bcc */
  352. __global_locale.mb_cur_max = 6;
  353. } else {
  354. assert(c==1);
  355. __global_locale.codeset = ascii;
  356. __global_locale.encoding = __ctype_encoding_7_bit;
  357. __global_locale.mb_cur_max = 1;
  358. }
  359. } else {
  360. const codeset_8_bit_t *c8b;
  361. r = CODESET_LIST;
  362. __global_locale.codeset = r + r[c -= 3];
  363. __global_locale.encoding = __ctype_encoding_8_bit;
  364. #warning REMINDER: update 8 bit mb_cur_max when trasnlit implemented!
  365. /* TODO - update when translit implemented! */
  366. __global_locale.mb_cur_max = 1;
  367. c8b = __locale_mmap->codeset_8_bit + c;
  368. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  369. __global_locale.idx8ctype = c8b->idx8ctype;
  370. __global_locale.idx8uplow = c8b->idx8uplow;
  371. #ifdef __WCHAR_ENABLED
  372. __global_locale.idx8c2wc = c8b->idx8c2wc;
  373. __global_locale.idx8wc2c = c8b->idx8wc2c;
  374. /* translit */
  375. #endif /* __WCHAR_ENABLED */
  376. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  377. }
  378. } else if ((len = __locale_mmap->lc_common_item_offsets_LEN[i]) != 0) {
  379. crow = __locale_mmap->locales[ WIDTH_LOCALES * row + 3 + i ]
  380. * len;
  381. x = (const char **)(((char *) &__global_locale)
  382. + __global_locale.category_offsets[i]);
  383. stp = __locale_mmap->lc_common_tbl_offsets + 4*i;
  384. r = (const unsigned char *)( ((char *)__locale_mmap) + *stp );
  385. io = (const uint16_t *)( ((char *)__locale_mmap) + *++stp );
  386. ii = (const uint16_t *)( ((char *)__locale_mmap) + *++stp );
  387. d = (const unsigned char *)( ((char *)__locale_mmap) + *++stp );
  388. for (c=0 ; c < len ; c++) {
  389. *(x + c) = d + ii[ r[crow + c] + io[c] ];
  390. }
  391. }
  392. }
  393. ++i;
  394. p += 2;
  395. s += 2;
  396. } while (i < LC_ALL);
  397. }
  398. #endif /* __LOCALE_C_ONLY */
  399. #endif
  400. /**********************************************************************/
  401. #ifdef L_nl_langinfo
  402. #include <langinfo.h>
  403. #include <nl_types.h>
  404. #ifdef __LOCALE_C_ONLY
  405. /* We need to index 300 bytes of data, so you might initially think we
  406. * need to store the offsets in shorts. But since the offset of the
  407. * 64th item is 231, we'll store "offset - 64" for all items >= 64
  408. * and always calculate the data offset as "offset[i] + (i & 64)".
  409. * This allows us to pack the data offsets in an unsigned char while
  410. * also avoiding an "if".
  411. *
  412. * Note: Category order is assumed to be:
  413. * ctype, numeric, monetary, time, collate, messages, all
  414. */
  415. #define C_LC_ALL 6
  416. /* Combine the data to avoid size penalty for seperate char arrays when
  417. * compiler aligns objects. The original code is left in as documentation. */
  418. #define cat_start nl_data
  419. #define C_locale_data (nl_data + C_LC_ALL + 1 + 78)
  420. static const unsigned char nl_data[C_LC_ALL + 1 + 78 + 300] = {
  421. /* static const unsigned char cat_start[C_LC_ALL + 1] = { */
  422. '\x00', '\x01', '\x04', '\x1a', '\x4c', '\x4c', '\x4e',
  423. /* }; */
  424. /* static const unsigned char item_offset[78] = { */
  425. '\x00', '\x06', '\x07', '\x07', '\x07', '\x07', '\x07', '\x07',
  426. '\x07', '\x07', '\x07', '\x08', '\x08', '\x08', '\x08', '\x08',
  427. '\x08', '\x08', '\x08', '\x08', '\x08', '\x08', '\x08', '\x08',
  428. '\x08', '\x0a', '\x0c', '\x10', '\x14', '\x18', '\x1c', '\x20',
  429. '\x24', '\x28', '\x2f', '\x36', '\x3e', '\x48', '\x51', '\x58',
  430. '\x61', '\x65', '\x69', '\x6d', '\x71', '\x75', '\x79', '\x7d',
  431. '\x81', '\x85', '\x89', '\x8d', '\x91', '\x99', '\xa2', '\xa8',
  432. '\xae', '\xb2', '\xb7', '\xbc', '\xc3', '\xcd', '\xd5', '\xde',
  433. '\xa7', '\xaa', '\xad', '\xc2', '\xcb', '\xd4', '\xdf', '\xdf',
  434. '\xdf', '\xdf', '\xdf', '\xdf', '\xe0', '\xe6',
  435. /* }; */
  436. /* static const unsigned char C_locale_data[300] = { */
  437. 'A', 'S', 'C', 'I', 'I', '\x00', '.', '\x00',
  438. '\x7f', '\x00', '-', '\x00', 'S', 'u', 'n', '\x00',
  439. 'M', 'o', 'n', '\x00', 'T', 'u', 'e', '\x00',
  440. 'W', 'e', 'd', '\x00', 'T', 'h', 'u', '\x00',
  441. 'F', 'r', 'i', '\x00', 'S', 'a', 't', '\x00',
  442. 'S', 'u', 'n', 'd', 'a', 'y', '\x00', 'M',
  443. 'o', 'n', 'd', 'a', 'y', '\x00', 'T', 'u',
  444. 'e', 's', 'd', 'a', 'y', '\x00', 'W', 'e',
  445. 'd', 'n', 'e', 's', 'd', 'a', 'y', '\x00',
  446. 'T', 'h', 'u', 'r', 's', 'd', 'a', 'y',
  447. '\x00', 'F', 'r', 'i', 'd', 'a', 'y', '\x00',
  448. 'S', 'a', 't', 'u', 'r', 'd', 'a', 'y',
  449. '\x00', 'J', 'a', 'n', '\x00', 'F', 'e', 'b',
  450. '\x00', 'M', 'a', 'r', '\x00', 'A', 'p', 'r',
  451. '\x00', 'M', 'a', 'y', '\x00', 'J', 'u', 'n',
  452. '\x00', 'J', 'u', 'l', '\x00', 'A', 'u', 'g',
  453. '\x00', 'S', 'e', 'p', '\x00', 'O', 'c', 't',
  454. '\x00', 'N', 'o', 'v', '\x00', 'D', 'e', 'c',
  455. '\x00', 'J', 'a', 'n', 'u', 'a', 'r', 'y',
  456. '\x00', 'F', 'e', 'b', 'r', 'u', 'a', 'r',
  457. 'y', '\x00', 'M', 'a', 'r', 'c', 'h', '\x00',
  458. 'A', 'p', 'r', 'i', 'l', '\x00', 'M', 'a',
  459. 'y', '\x00', 'J', 'u', 'n', 'e', '\x00', 'J',
  460. 'u', 'l', 'y', '\x00', 'A', 'u', 'g', 'u',
  461. 's', 't', '\x00', 'S', 'e', 'p', 't', 'e',
  462. 'm', 'b', 'e', 'r', '\x00', 'O', 'c', 't',
  463. 'o', 'b', 'e', 'r', '\x00', 'N', 'o', 'v',
  464. 'e', 'm', 'b', 'e', 'r', '\x00', 'D', 'e',
  465. 'c', 'e', 'm', 'b', 'e', 'r', '\x00', 'A',
  466. 'M', '\x00', 'P', 'M', '\x00', '%', 'a', ' ',
  467. '%', 'b', ' ', '%', 'e', ' ', '%', 'H',
  468. ':', '%', 'M', ':', '%', 'S', ' ', '%',
  469. 'Y', '\x00', '%', 'm', '/', '%', 'd', '/',
  470. '%', 'y', '\x00', '%', 'H', ':', '%', 'M',
  471. ':', '%', 'S', '\x00', '%', 'I', ':', '%',
  472. 'M', ':', '%', 'S', ' ', '%', 'p', '\x00',
  473. '^', '[', 'y', 'Y', ']', '\x00', '^', '[',
  474. 'n', 'N', ']', '\x00',
  475. };
  476. char *nl_langinfo(nl_item item)
  477. {
  478. unsigned int c;
  479. unsigned int i;
  480. if ((c = _NL_ITEM_CATEGORY(item)) < C_LC_ALL) {
  481. if ((i = cat_start[c] + _NL_ITEM_INDEX(item)) < cat_start[c+1]) {
  482. /* return (char *) C_locale_data + item_offset[i] + (i & 64); */
  483. return (char *) C_locale_data + nl_data[C_LC_ALL+1+i] + (i & 64);
  484. }
  485. }
  486. return (char *) cat_start; /* Conveniently, this is the empty string. */
  487. }
  488. #else /* __LOCALE_C_ONLY */
  489. static const char empty[] = "";
  490. char *nl_langinfo(nl_item item)
  491. {
  492. unsigned int c = _NL_ITEM_CATEGORY(item);
  493. unsigned int i = _NL_ITEM_INDEX(item);
  494. if ((c < LC_ALL) && (i < __global_locale.category_item_count[c])) {
  495. return ((char **)(((char *) &__global_locale)
  496. + __global_locale.category_offsets[c]))[i];
  497. }
  498. return (char *) empty;
  499. }
  500. #endif /* __LOCALE_C_ONLY */
  501. #endif
  502. /**********************************************************************/