stdlib.c 18 KB

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