stdlib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /* Copyright (C) 2002 Manuel Novoa III
  2. * From my (incomplete) stdlib library for linux and (soon) elks.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the Free
  16. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  19. *
  20. * This code is currently under development. Also, I plan to port
  21. * it to elks which is a 16-bit environment with a fairly limited
  22. * compiler. Therefore, please refrain from modifying this code
  23. * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
  24. *
  25. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  26. /* Oct 29, 2002
  27. *
  28. * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs.
  29. */
  30. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  31. #define _GNU_SOURCE
  32. #include <limits.h>
  33. #include <stdint.h>
  34. #include <inttypes.h>
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include <assert.h>
  38. #include <unistd.h>
  39. /* Work around gcc's refusal to create aliases.
  40. * TODO: Add in a define to disable the aliases? */
  41. #if UINT_MAX == ULONG_MAX
  42. #define atoi __ignore_atoi
  43. #define abs __ignore_abs
  44. #endif
  45. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  46. #define llabs __ignore_llabs
  47. #define atoll __ignore_atoll
  48. #define strtoll __ignore_strtoll
  49. #define strtoull __ignore_strtoull
  50. #endif
  51. #include <stdlib.h>
  52. #if UINT_MAX == ULONG_MAX
  53. #undef atoi
  54. #undef abs
  55. #endif
  56. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  57. #undef llabs
  58. #undef atoll
  59. #undef strtoll
  60. #undef strtoull
  61. #endif
  62. extern unsigned long
  63. _stdlib_strto_l(register const char * __restrict str,
  64. char ** __restrict endptr, int base, int sflag);
  65. #if defined(ULLONG_MAX)
  66. extern unsigned long long
  67. _stdlib_strto_ll(register const char * __restrict str,
  68. char ** __restrict endptr, int base, int sflag);
  69. #endif
  70. /**********************************************************************/
  71. #ifdef L_atof
  72. double atof(const char *nptr)
  73. {
  74. return strtod(nptr, (char **) NULL);
  75. }
  76. #endif
  77. /**********************************************************************/
  78. #ifdef L_abs
  79. #if INT_MAX < LONG_MAX
  80. int abs(int j)
  81. {
  82. return (j >= 0) ? j : -j;
  83. }
  84. #endif /* INT_MAX < LONG_MAX */
  85. #endif
  86. /**********************************************************************/
  87. #ifdef L_labs
  88. #if UINT_MAX == ULONG_MAX
  89. strong_alias(labs,abs)
  90. #endif
  91. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  92. strong_alias(labs,llabs)
  93. #endif
  94. #if ULONG_MAX == UINTMAX_MAX
  95. strong_alias(labs,imaxabs)
  96. #endif
  97. long int labs(long int j)
  98. {
  99. return (j >= 0) ? j : -j;
  100. }
  101. #endif
  102. /**********************************************************************/
  103. #ifdef L_llabs
  104. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  105. #if (ULLONG_MAX == UINTMAX_MAX)
  106. strong_alias(llabs,imaxabs)
  107. #endif
  108. long long int llabs(long long int j)
  109. {
  110. return (j >= 0) ? j : -j;
  111. }
  112. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  113. #endif
  114. /**********************************************************************/
  115. #ifdef L_atoi
  116. #if INT_MAX < LONG_MAX
  117. int atoi(const char *nptr)
  118. {
  119. return (int) strtol(nptr, (char **) NULL, 10);
  120. }
  121. #endif /* INT_MAX < LONG_MAX */
  122. #endif
  123. /**********************************************************************/
  124. #ifdef L_atol
  125. #if UINT_MAX == ULONG_MAX
  126. strong_alias(atol,atoi)
  127. #endif
  128. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  129. strong_alias(atol,atoll)
  130. #endif
  131. long atol(const char *nptr)
  132. {
  133. return strtol(nptr, (char **) NULL, 10);
  134. }
  135. #endif
  136. /**********************************************************************/
  137. #ifdef L_atoll
  138. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  139. long long atoll(const char *nptr)
  140. {
  141. return strtoll(nptr, (char **) NULL, 10);
  142. }
  143. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  144. #endif
  145. /**********************************************************************/
  146. #ifdef L_strtol
  147. #if ULONG_MAX == UINTMAX_MAX
  148. strong_alias(strtol,strtoimax)
  149. #endif
  150. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  151. strong_alias(strtol,strtoll)
  152. #endif
  153. long strtol(const char * __restrict str, char ** __restrict endptr, int base)
  154. {
  155. return _stdlib_strto_l(str, endptr, base, 1);
  156. }
  157. #endif
  158. /**********************************************************************/
  159. #ifdef L_strtoll
  160. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  161. #if (ULLONG_MAX == UINTMAX_MAX)
  162. strong_alias(strtoll,strtoimax)
  163. #endif
  164. long long strtoll(const char * __restrict str,
  165. char ** __restrict endptr, int base)
  166. {
  167. return (long long) _stdlib_strto_ll(str, endptr, base, 1);
  168. }
  169. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  170. #endif
  171. /**********************************************************************/
  172. #ifdef L_strtoul
  173. #if ULONG_MAX == UINTMAX_MAX
  174. strong_alias(strtoul,strtoumax)
  175. #endif
  176. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  177. strong_alias(strtoul,strtoull)
  178. #endif
  179. unsigned long strtoul(const char * __restrict str,
  180. char ** __restrict endptr, int base)
  181. {
  182. return _stdlib_strto_l(str, endptr, base, 0);
  183. }
  184. #endif
  185. /**********************************************************************/
  186. #ifdef L_strtoull
  187. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  188. #if (ULLONG_MAX == UINTMAX_MAX)
  189. strong_alias(strtoull,strtoumax)
  190. #endif
  191. unsigned long long strtoull(const char * __restrict str,
  192. char ** __restrict endptr, int base)
  193. {
  194. return _stdlib_strto_ll(str, endptr, base, 0);
  195. }
  196. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  197. #endif
  198. /**********************************************************************/
  199. /* Support routines follow */
  200. /**********************************************************************/
  201. /* Set if we want errno set appropriately. */
  202. /* NOTE: Implies _STRTO_ENDPTR below */
  203. #define _STRTO_ERRNO 1
  204. /* Set if we want support for the endptr arg. */
  205. /* Implied by _STRTO_ERRNO. */
  206. #define _STRTO_ENDPTR 1
  207. #if _STRTO_ERRNO
  208. #undef _STRTO_ENDPTR
  209. #define _STRTO_ENDPTR 1
  210. #define SET_ERRNO(X) __set_errno(X)
  211. #else
  212. #define SET_ERRNO(X) ((void)(X)) /* keep side effects */
  213. #endif
  214. /**********************************************************************/
  215. #ifdef L__stdlib_strto_l
  216. /* This is the main work fuction which handles both strtol (sflag = 1) and
  217. * strtoul (sflag = 0). */
  218. unsigned long _stdlib_strto_l(register const char * __restrict str,
  219. char ** __restrict endptr, int base, int sflag)
  220. {
  221. unsigned long number, cutoff;
  222. #if _STRTO_ENDPTR
  223. const char *fail_char;
  224. #define SET_FAIL(X) fail_char = (X)
  225. #else
  226. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  227. #endif
  228. unsigned char negative, digit, cutoff_digit;
  229. assert((sflag == 0) || (sflag == 1));
  230. SET_FAIL(str);
  231. while (isspace(*str)) { /* Skip leading whitespace. */
  232. ++str;
  233. }
  234. /* Handle optional sign. */
  235. negative = 0;
  236. switch(*str) {
  237. case '-': negative = 1; /* Fall through to increment str. */
  238. case '+': ++str;
  239. }
  240. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  241. base += 10; /* Default is 10 (26). */
  242. if (*str == '0') {
  243. SET_FAIL(++str);
  244. base -= 2; /* Now base is 8 or 16 (24). */
  245. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  246. ++str;
  247. base += base; /* Base is 16 (16 or 48). */
  248. }
  249. }
  250. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  251. base = 16;
  252. }
  253. }
  254. number = 0;
  255. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  256. cutoff_digit = ULONG_MAX % base;
  257. cutoff = ULONG_MAX / base;
  258. do {
  259. digit = (((unsigned char)(*str - '0')) <= 9)
  260. ? (*str - '0')
  261. : ((*str >= 'A')
  262. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  263. : 40);
  264. if (digit >= base) {
  265. break;
  266. }
  267. SET_FAIL(++str);
  268. if ((number > cutoff)
  269. || ((number == cutoff) && (digit > cutoff_digit))) {
  270. number = ULONG_MAX;
  271. negative &= sflag;
  272. SET_ERRNO(ERANGE);
  273. } else {
  274. number = number * base + digit;
  275. }
  276. } while (1);
  277. }
  278. #if _STRTO_ENDPTR
  279. if (endptr) {
  280. *endptr = (char *) fail_char;
  281. }
  282. #endif
  283. {
  284. unsigned long tmp = ((negative)
  285. ? ((unsigned long)(-(1+LONG_MIN)))+1
  286. : LONG_MAX);
  287. if (sflag && (number > tmp)) {
  288. number = tmp;
  289. SET_ERRNO(ERANGE);
  290. }
  291. }
  292. return negative ? (unsigned long)(-((long)number)) : number;
  293. }
  294. #endif
  295. /**********************************************************************/
  296. #ifdef L__stdlib_strto_ll
  297. #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
  298. /* This is the main work fuction which handles both strtoll (sflag = 1) and
  299. * strtoull (sflag = 0). */
  300. unsigned long long _stdlib_strto_ll(register const char * __restrict str,
  301. char ** __restrict endptr, int base,
  302. int sflag)
  303. {
  304. unsigned long long number;
  305. #if _STRTO_ENDPTR
  306. const char *fail_char;
  307. #define SET_FAIL(X) fail_char = (X)
  308. #else
  309. #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
  310. #endif
  311. unsigned int n1;
  312. unsigned char negative, digit;
  313. assert((sflag == 0) || (sflag == 1));
  314. SET_FAIL(str);
  315. while (isspace(*str)) { /* Skip leading whitespace. */
  316. ++str;
  317. }
  318. /* Handle optional sign. */
  319. negative = 0;
  320. switch(*str) {
  321. case '-': negative = 1; /* Fall through to increment str. */
  322. case '+': ++str;
  323. }
  324. if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
  325. base += 10; /* Default is 10 (26). */
  326. if (*str == '0') {
  327. SET_FAIL(++str);
  328. base -= 2; /* Now base is 8 or 16 (24). */
  329. if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
  330. ++str;
  331. base += base; /* Base is 16 (16 or 48). */
  332. }
  333. }
  334. if (base > 16) { /* Adjust in case base wasn't dynamic. */
  335. base = 16;
  336. }
  337. }
  338. number = 0;
  339. if (((unsigned)(base - 2)) < 35) { /* Legal base. */
  340. do {
  341. digit = (((unsigned char)(*str - '0')) <= 9)
  342. ? (*str - '0')
  343. : ((*str >= 'A')
  344. ? (((0x20|(*str)) - 'a' + 10)) /* WARNING: assumes ascii. */
  345. : 40);
  346. if (digit >= base) {
  347. break;
  348. }
  349. SET_FAIL(++str);
  350. #if 1
  351. /* Optional, but speeds things up in the usual case. */
  352. if (number <= (ULLONG_MAX >> 6)) {
  353. number = number * base + digit;
  354. } else
  355. #endif
  356. {
  357. n1 = ((unsigned char) number) * base + digit;
  358. number = (number >> CHAR_BIT) * base;
  359. if (number + (n1 >> CHAR_BIT) <= (ULLONG_MAX >> CHAR_BIT)) {
  360. number = (number << CHAR_BIT) + n1;
  361. } else { /* Overflow. */
  362. number = ULLONG_MAX;
  363. negative &= sflag;
  364. SET_ERRNO(ERANGE);
  365. }
  366. }
  367. } while (1);
  368. }
  369. #if _STRTO_ENDPTR
  370. if (endptr) {
  371. *endptr = (char *) fail_char;
  372. }
  373. #endif
  374. {
  375. unsigned long long tmp = ((negative)
  376. ? ((unsigned long long)(-(1+LLONG_MIN)))+1
  377. : LLONG_MAX);
  378. if (sflag && (number > tmp)) {
  379. number = tmp;
  380. SET_ERRNO(ERANGE);
  381. }
  382. }
  383. return negative ? (unsigned long long)(-((long long)number)) : number;
  384. }
  385. #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
  386. #endif
  387. /**********************************************************************/
  388. /* Made _Exit() an alias for _exit(), as per C99. */
  389. /* #ifdef L__Exit */
  390. /* void _Exit(int status) */
  391. /* { */
  392. /* _exit(status); */
  393. /* } */
  394. /* #endif */
  395. /**********************************************************************/
  396. #ifdef L_bsearch
  397. void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
  398. size_t size, int (*compar)(const void *, const void *))
  399. {
  400. register char *p;
  401. size_t low;
  402. size_t mid;
  403. int r;
  404. if (size > 0) { /* TODO: change this to an assert?? */
  405. low = 0;
  406. while (low < high) {
  407. mid = low + ((high - low) >> 1); /* Avoid possible overflow here. */
  408. p = ((char *)base) + mid * size; /* Could overflow here... */
  409. r = (*compar)(key, p); /* but that's an application problem! */
  410. if (r > 0) {
  411. low = mid + 1;
  412. } else if (r < 0) {
  413. high = mid;
  414. } else {
  415. return p;
  416. }
  417. }
  418. }
  419. return NULL;
  420. }
  421. #endif
  422. /**********************************************************************/
  423. #ifdef L_qsort
  424. /* This code is derived from a public domain shell sort routine by
  425. * Ray Gardner and found in Bob Stout's snippets collection. The
  426. * original code is included below in an #if 0/#endif block.
  427. *
  428. * I modified it to avoid the possibility of overflow in the wgap
  429. * calculation, as well as to reduce the generated code size with
  430. * bcc and gcc. */
  431. void qsort (void *base,
  432. size_t nel,
  433. size_t width,
  434. int (*comp)(const void *, const void *))
  435. {
  436. size_t wgap, i, j, k;
  437. char tmp;
  438. if ((nel > 1) && (width > 0)) {
  439. assert( nel <= ((size_t)(-1)) / width ); /* check for overflow */
  440. wgap = 0;
  441. do {
  442. wgap = 3 * wgap + 1;
  443. } while (wgap < (nel-1)/3);
  444. /* From the above, we know that either wgap == 1 < nel or */
  445. /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */
  446. wgap *= width; /* So this can not overflow if wnel doesn't. */
  447. nel *= width; /* Convert nel to 'wnel' */
  448. do {
  449. i = wgap;
  450. do {
  451. j = i;
  452. do {
  453. register char *a;
  454. register char *b;
  455. j -= wgap;
  456. a = j + ((char *)base);
  457. b = a + wgap;
  458. if ( (*comp)(a, b) <= 0 ) {
  459. break;
  460. }
  461. k = width;
  462. do {
  463. tmp = *a;
  464. *a++ = *b;
  465. *b++ = tmp;
  466. } while ( --k );
  467. } while (j >= wgap);
  468. i += width;
  469. } while (i < nel);
  470. wgap = (wgap - width)/3;
  471. } while (wgap);
  472. }
  473. }
  474. /* ---------- original snippets version below ---------- */
  475. #if 0
  476. /*
  477. ** ssort() -- Fast, small, qsort()-compatible Shell sort
  478. **
  479. ** by Ray Gardner, public domain 5/90
  480. */
  481. #include <stddef.h>
  482. void ssort (void *base,
  483. size_t nel,
  484. size_t width,
  485. int (*comp)(const void *, const void *))
  486. {
  487. size_t wnel, gap, wgap, i, j, k;
  488. char *a, *b, tmp;
  489. wnel = width * nel;
  490. for (gap = 0; ++gap < nel;)
  491. gap *= 3;
  492. while ( gap /= 3 )
  493. {
  494. wgap = width * gap;
  495. for (i = wgap; i < wnel; i += width)
  496. {
  497. for (j = i - wgap; ;j -= wgap)
  498. {
  499. a = j + (char *)base;
  500. b = a + wgap;
  501. if ( (*comp)(a, b) <= 0 )
  502. break;
  503. k = width;
  504. do
  505. {
  506. tmp = *a;
  507. *a++ = *b;
  508. *b++ = tmp;
  509. } while ( --k );
  510. if (j < wgap)
  511. break;
  512. }
  513. }
  514. }
  515. }
  516. #endif
  517. #endif
  518. /**********************************************************************/
  519. /* Multibyte and wchar stuff follows. */
  520. #ifdef __UCLIBC_HAS_WCHAR__
  521. #include <locale.h>
  522. #include <wchar.h>
  523. /* TODO: clean up the following... */
  524. #if WCHAR_MAX > 0xffffUL
  525. #define UTF_8_MAX_LEN 6
  526. #else
  527. #define UTF_8_MAX_LEN 3
  528. #endif
  529. #ifdef __UCLIBC_HAS_LOCALE__
  530. #define ENCODING (__global_locale.encoding)
  531. #ifdef __UCLIBC_MJN3_ONLY__
  532. #warning implement __CTYPE_HAS_UTF_8_LOCALES!
  533. #endif
  534. #define __CTYPE_HAS_UTF_8_LOCALES
  535. #else
  536. #define ENCODING (__ctype_encoding_7_bit)
  537. #undef __CTYPE_HAS_8_BIT_LOCALES
  538. #undef __CTYPE_HAS_UTF_8_LOCALES
  539. #undef L__wchar_utf8sntowcs
  540. #undef L__wchar_wcsntoutf8s
  541. #endif
  542. #endif
  543. /**********************************************************************/
  544. #ifdef L__stdlib_mb_cur_max
  545. size_t _stdlib_mb_cur_max(void)
  546. {
  547. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  548. return __global_locale.mb_cur_max;
  549. #else
  550. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  551. #ifdef __UCLIBC_MJN3_ONLY__
  552. #warning need to change this when/if transliteration is implemented
  553. #endif
  554. #endif
  555. return 1;
  556. #endif
  557. }
  558. #endif
  559. /**********************************************************************/
  560. #ifdef L_mblen
  561. int mblen(register const char *s, size_t n)
  562. {
  563. static mbstate_t state;
  564. size_t r;
  565. if (!s) {
  566. state.mask = 0;
  567. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  568. return ENCODING == __ctype_encoding_utf8;
  569. #else
  570. return 0;
  571. #endif
  572. }
  573. if ((r = mbrlen(s, n, &state)) == (size_t) -2) {
  574. /* TODO: Should we set an error state? */
  575. state.wc = 0xffffU; /* Make sure we're in an error state. */
  576. return (size_t) -1; /* TODO: Change error code above? */
  577. }
  578. return r;
  579. }
  580. #endif
  581. /**********************************************************************/
  582. #ifdef L_mbtowc
  583. int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
  584. {
  585. static mbstate_t state;
  586. size_t r;
  587. if (!s) {
  588. state.mask = 0;
  589. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  590. return ENCODING == __ctype_encoding_utf8;
  591. #else
  592. return 0;
  593. #endif
  594. }
  595. if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
  596. /* TODO: Should we set an error state? */
  597. state.wc = 0xffffU; /* Make sure we're in an error state. */
  598. return (size_t) -1; /* TODO: Change error code above? */
  599. }
  600. return r;
  601. }
  602. #endif
  603. /**********************************************************************/
  604. #ifdef L_wctomb
  605. /* Note: We completely ignore state in all currently supported conversions. */
  606. int wctomb(register char *__restrict s, wchar_t swc)
  607. {
  608. return (!s)
  609. ?
  610. #ifdef __CTYPE_HAS_UTF_8_LOCALES
  611. (ENCODING == __ctype_encoding_utf8)
  612. #else
  613. 0 /* Encoding is stateless. */
  614. #endif
  615. : ((ssize_t) wcrtomb(s, swc, NULL));
  616. }
  617. #endif
  618. /**********************************************************************/
  619. #ifdef L_mbstowcs
  620. size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
  621. {
  622. mbstate_t state;
  623. const char *e = s; /* Needed because of restrict. */
  624. state.mask = 0; /* Always start in initial shift state. */
  625. return mbsrtowcs(pwcs, &e, n, &state);
  626. }
  627. #endif
  628. /**********************************************************************/
  629. #ifdef L_wcstombs
  630. /* Note: We completely ignore state in all currently supported conversions. */
  631. size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
  632. {
  633. const wchar_t *e = pwcs; /* Needed because of restrict. */
  634. return wcsrtombs(s, &e, n, NULL);
  635. }
  636. #endif
  637. /**********************************************************************/