locale.c 18 KB

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