1 /* Copyright (C) 2002 Manuel Novoa III
2 * From my (incomplete) stdlib library for linux and (soon) elks.
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.
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.
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.
19 /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
21 * This code is currently under development. Also, I plan to port
22 * it to elks which is a 16-bit environment with a fairly limited
23 * compiler. Therefore, please refrain from modifying this code
24 * and, instead, pass any bug-fixes, etc. to me. Thanks. Manuel
26 * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
29 * Fix a couple of 'restrict' bugs in mbstowcs and wcstombs.
32 * Add wscto{inttype} functions.
35 #define _ISOC99_SOURCE /* for ULLONG primarily... */
38 /* Work around gcc's refusal to create aliases.
39 * TODO: Add in a define to disable the aliases? */
41 #if UINT_MAX == ULONG_MAX
43 #define abs __ignore_abs
46 #define atoi __ignore_atoi
49 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
51 #define llabs __ignore_llabs
54 #define atoll __ignore_atoll
57 #define strtoll __ignore_strtoll
60 #define strtoull __ignore_strtoull
63 #define wcstoll __ignore_wcstoll
66 #define wcstoull __ignore_wcstoull
69 #define strtoll_l __ignore_strtoll_l
72 #define strtoull_l __ignore_strtoull_l
75 #define wcstoll_l __ignore_wcstoll_l
78 #define wcstoull_l __ignore_wcstoull_l
81 #if defined(ULLONG_MAX) && (ULLONG_MAX == UINTMAX_MAX)
82 #if defined L_labs || defined L_llabs
83 #define imaxabs __ignore_imaxabs
97 #ifdef __UCLIBC_HAS_WCHAR__
101 #include <bits/uClibc_uwchar.h>
103 #ifdef __UCLIBC_HAS_XLOCALE__
105 #endif /* __UCLIBC_HAS_XLOCALE__ */
107 /* TODO: clean up the following... */
109 #if WCHAR_MAX > 0xffffUL
110 #define UTF_8_MAX_LEN 6
112 #define UTF_8_MAX_LEN 3
115 #ifdef __UCLIBC_HAS_LOCALE__
117 #define ENCODING ((__UCLIBC_CURLOCALE_DATA).encoding)
118 #ifndef __CTYPE_HAS_UTF_8_LOCALES
121 #warning __CTYPE_HAS_UTF_8_LOCALES not set!
125 #else /* __UCLIBC_HAS_LOCALE__ */
127 #ifdef __UCLIBC_MJN3_ONLY__
130 #warning devel checks
133 #ifdef __CTYPE_HAS_8_BIT_LOCALES
134 #error __CTYPE_HAS_8_BIT_LOCALES is defined!
136 #ifdef __CTYPE_HAS_UTF_8_LOCALES
137 #error __CTYPE_HAS_UTF_8_LOCALES is defined!
141 #endif /* __UCLIBC_HAS_LOCALE__ */
143 /**********************************************************************/
144 #ifdef __UCLIBC_HAS_XLOCALE__
147 _stdlib_strto_l_l(register const char * __restrict str,
148 char ** __restrict endptr, int base, int sflag,
149 __locale_t locale_arg) attribute_hidden;
151 #if defined(ULLONG_MAX)
152 extern unsigned long long
153 _stdlib_strto_ll_l(register const char * __restrict str,
154 char ** __restrict endptr, int base, int sflag,
155 __locale_t locale_arg) attribute_hidden;
158 #ifdef __UCLIBC_HAS_WCHAR__
160 _stdlib_wcsto_l_l(register const wchar_t * __restrict str,
161 wchar_t ** __restrict endptr, int base, int sflag,
162 __locale_t locale_arg) attribute_hidden;
164 #if defined(ULLONG_MAX)
165 extern unsigned long long
166 _stdlib_wcsto_ll_l(register const wchar_t * __restrict str,
167 wchar_t ** __restrict endptr, int base, int sflag,
168 __locale_t locale_arg) attribute_hidden;
170 #endif /* __UCLIBC_HAS_WCHAR__ */
172 #endif /* __UCLIBC_HAS_XLOCALE__ */
177 _stdlib_strto_l(register const char * __restrict str,
178 char ** __restrict endptr, int base, int sflag) attribute_hidden;
180 #if defined(ULLONG_MAX)
181 extern unsigned long long
182 _stdlib_strto_ll(register const char * __restrict str,
183 char ** __restrict endptr, int base, int sflag) attribute_hidden;
186 #ifdef __UCLIBC_HAS_WCHAR__
188 _stdlib_wcsto_l(register const wchar_t * __restrict str,
189 wchar_t ** __restrict endptr, int base, int sflag) attribute_hidden;
191 #if defined(ULLONG_MAX)
192 extern unsigned long long
193 _stdlib_wcsto_ll(register const wchar_t * __restrict str,
194 wchar_t ** __restrict endptr, int base, int sflag) attribute_hidden;
196 #endif /* __UCLIBC_HAS_WCHAR__ */
197 /**********************************************************************/
200 libc_hidden_proto(strtod)
202 double atof(const char *nptr)
204 return strtod(nptr, (char **) NULL);
208 /**********************************************************************/
211 #if INT_MAX < LONG_MAX
215 return (j >= 0) ? j : -j;
218 #endif /* INT_MAX < LONG_MAX */
221 /**********************************************************************/
224 long int labs(long int j)
226 return (j >= 0) ? j : -j;
229 #if UINT_MAX == ULONG_MAX
231 extern __typeof(labs) abs;
232 strong_alias(labs,abs)
235 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
237 extern __typeof(labs) llabs;
238 strong_alias(labs,llabs)
241 #if ULONG_MAX == UINTMAX_MAX
243 extern __typeof(labs) imaxabs;
244 strong_alias(labs,imaxabs)
248 /**********************************************************************/
251 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
253 long long int llabs(long long int j)
255 return (j >= 0) ? j : -j;
258 #if (ULLONG_MAX == UINTMAX_MAX)
260 extern __typeof(llabs) imaxabs;
261 strong_alias(llabs,imaxabs)
264 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
267 /**********************************************************************/
270 #if INT_MAX < LONG_MAX
272 libc_hidden_proto(strtol)
274 libc_hidden_proto(atoi)
275 int atoi(const char *nptr)
277 return (int) strtol(nptr, (char **) NULL, 10);
279 libc_hidden_def(atoi)
281 #endif /* INT_MAX < LONG_MAX */
284 /**********************************************************************/
287 libc_hidden_proto(strtol)
289 libc_hidden_proto(atol)
290 long atol(const char *nptr)
292 return strtol(nptr, (char **) NULL, 10);
294 libc_hidden_def(atol)
296 #if UINT_MAX == ULONG_MAX
298 extern __typeof(atol) atoi;
299 libc_hidden_proto(atoi)
300 strong_alias(atol,atoi)
301 libc_hidden_def(atoi)
304 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
306 extern __typeof(atol) atoll;
307 strong_alias(atol,atoll)
311 /**********************************************************************/
314 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
316 libc_hidden_proto(strtoll)
318 long long atoll(const char *nptr)
320 return strtoll(nptr, (char **) NULL, 10);
323 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
326 /**********************************************************************/
327 #if defined(L_strtol) || defined(L_strtol_l)
329 libc_hidden_proto(__XL_NPP(strtol))
330 long __XL_NPP(strtol)(const char * __restrict str, char ** __restrict endptr,
331 int base __LOCALE_PARAM)
333 return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 1 __LOCALE_ARG);
335 libc_hidden_def(__XL_NPP(strtol))
337 #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtol_l)
338 strong_alias(strtol,strtoimax)
341 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
347 extern __typeof(strtol) __XL_NPP(strtoll);
348 libc_hidden_proto(__XL_NPP(strtoll))
349 strong_alias(__XL_NPP(strtol),__XL_NPP(strtoll))
350 libc_hidden_def(__XL_NPP(strtoll))
354 /**********************************************************************/
355 #if defined(L_strtoll) || defined(L_strtoll_l)
357 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
359 libc_hidden_proto(__XL_NPP(strtoll))
360 long long __XL_NPP(strtoll)(const char * __restrict str,
361 char ** __restrict endptr, int base
364 return (long long) __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 1 __LOCALE_ARG);
366 libc_hidden_def(__XL_NPP(strtoll))
368 #if !defined(L_strtoll_l)
369 #if (ULLONG_MAX == UINTMAX_MAX)
370 strong_alias(strtoll,strtoimax)
372 strong_alias(strtoll,strtoq)
375 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
378 /**********************************************************************/
379 #if defined(L_strtoul) || defined(L_strtoul_l)
381 libc_hidden_proto(__XL_NPP(strtoul))
382 unsigned long __XL_NPP(strtoul)(const char * __restrict str,
383 char ** __restrict endptr, int base
386 return __XL_NPP(_stdlib_strto_l)(str, endptr, base, 0 __LOCALE_ARG);
388 libc_hidden_def(__XL_NPP(strtoul))
390 #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_strtoul_l)
391 strong_alias(strtoul,strtoumax)
394 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
400 extern __typeof(strtoul) __XL_NPP(strtoull);
401 libc_hidden_proto(__XL_NPP(strtoull))
402 strong_alias(__XL_NPP(strtoul),__XL_NPP(strtoull))
403 libc_hidden_def(__XL_NPP(strtoull))
408 /**********************************************************************/
409 #if defined(L_strtoull) || defined(L_strtoull_l)
411 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
413 libc_hidden_proto(__XL_NPP(strtoull))
414 unsigned long long __XL_NPP(strtoull)(const char * __restrict str,
415 char ** __restrict endptr, int base
418 return __XL_NPP(_stdlib_strto_ll)(str, endptr, base, 0 __LOCALE_ARG);
420 libc_hidden_def(__XL_NPP(strtoull))
422 #if !defined(L_strtoull_l)
423 #if (ULLONG_MAX == UINTMAX_MAX)
424 strong_alias(strtoull,strtoumax)
426 strong_alias(strtoull,strtouq)
429 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
432 /**********************************************************************/
433 /* Support routines follow */
434 /**********************************************************************/
435 /* Set if we want errno set appropriately. */
436 /* NOTE: Implies _STRTO_ENDPTR below */
437 #define _STRTO_ERRNO 1
439 /* Set if we want support for the endptr arg. */
440 /* Implied by _STRTO_ERRNO. */
441 #define _STRTO_ENDPTR 1
445 #define _STRTO_ENDPTR 1
446 #define SET_ERRNO(X) __set_errno(X)
448 #define SET_ERRNO(X) ((void)(X)) /* keep side effects */
451 /**********************************************************************/
452 #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
453 #ifndef L__stdlib_strto_l
454 #define L__stdlib_strto_l
458 #if defined(L__stdlib_strto_l) || defined(L__stdlib_strto_l_l)
460 #if defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l)
462 #define _stdlib_strto_l _stdlib_wcsto_l
463 #define _stdlib_strto_l_l _stdlib_wcsto_l_l
464 #define Wchar wchar_t
465 #define Wuchar __uwchar_t
466 #ifdef __UCLIBC_DO_XLOCALE
467 #define ISSPACE(C) iswspace_l((C), locale_arg)
468 libc_hidden_proto(iswspace_l)
470 #define ISSPACE(C) iswspace((C))
471 libc_hidden_proto(iswspace)
474 #else /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
477 #define Wuchar unsigned char
478 #ifdef __UCLIBC_DO_XLOCALE
479 #define ISSPACE(C) isspace_l((C), locale_arg)
480 libc_hidden_proto(isspace_l)
482 #define ISSPACE(C) isspace((C))
483 libc_hidden_proto(isspace)
486 #endif /* defined(L__stdlib_wcsto_l) || defined(L__stdlib_wcsto_l_l) */
488 #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
490 unsigned long attribute_hidden _stdlib_strto_l(register const Wchar * __restrict str,
491 Wchar ** __restrict endptr, int base,
494 return _stdlib_strto_l_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
498 #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
500 /* This is the main work fuction which handles both strtol (sflag = 1) and
501 * strtoul (sflag = 0). */
503 #ifdef __UCLIBC_HAS_XLOCALE__
504 libc_hidden_proto(__ctype_b_loc)
505 #elif defined __UCLIBC_HAS_CTYPE_TABLES__
506 libc_hidden_proto(__ctype_b)
509 unsigned long attribute_hidden __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str,
510 Wchar ** __restrict endptr, int base,
511 int sflag __LOCALE_PARAM)
513 unsigned long number, cutoff;
515 const Wchar *fail_char;
516 #define SET_FAIL(X) fail_char = (X)
518 #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
520 unsigned char negative, digit, cutoff_digit;
522 assert(((unsigned int)sflag) <= 1);
526 while (ISSPACE(*str)) { /* Skip leading whitespace. */
530 /* Handle optional sign. */
533 case '-': negative = 1; /* Fall through to increment str. */
537 if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
538 base += 10; /* Default is 10 (26). */
541 base -= 2; /* Now base is 8 or 16 (24). */
542 if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
544 base += base; /* Base is 16 (16 or 48). */
548 if (base > 16) { /* Adjust in case base wasn't dynamic. */
555 if (((unsigned)(base - 2)) < 35) { /* Legal base. */
556 cutoff_digit = ULONG_MAX % base;
557 cutoff = ULONG_MAX / base;
559 digit = ((Wuchar)(*str - '0') <= 9)
560 ? /* 0..9 */ (*str - '0')
561 : /* else */ (((Wuchar)(0x20 | *str) >= 'a') /* WARNING: assumes ascii. */
562 ? /* >= A/a */ ((Wuchar)(0x20 | *str) - ('a' - 10))
563 : /* else */ 40 /* bad value */);
571 if ((number > cutoff)
572 || ((number == cutoff) && (digit > cutoff_digit))) {
577 number = number * base + digit;
584 *endptr = (Wchar *) fail_char;
589 unsigned long tmp = (negative
590 ? ((unsigned long)(-(1+LONG_MIN)))+1
592 if (sflag && (number > tmp)) {
598 return negative ? (unsigned long)(-((long)number)) : number;
601 #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
605 /**********************************************************************/
606 #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
607 #ifndef L__stdlib_strto_ll
608 #define L__stdlib_strto_ll
612 #if defined(L__stdlib_strto_ll) || defined(L__stdlib_strto_ll_l)
614 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
616 #if defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l)
617 #define _stdlib_strto_ll _stdlib_wcsto_ll
618 #define _stdlib_strto_ll_l _stdlib_wcsto_ll_l
619 #define Wchar wchar_t
620 #define Wuchar __uwchar_t
621 #ifdef __UCLIBC_DO_XLOCALE
622 #define ISSPACE(C) iswspace_l((C), locale_arg)
623 libc_hidden_proto(iswspace_l)
625 #define ISSPACE(C) iswspace((C))
626 libc_hidden_proto(iswspace)
629 #else /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
632 #define Wuchar unsigned char
633 #ifdef __UCLIBC_DO_XLOCALE
634 #define ISSPACE(C) isspace_l((C), locale_arg)
635 libc_hidden_proto(isspace_l)
637 #define ISSPACE(C) isspace((C))
638 libc_hidden_proto(isspace)
641 #endif /* defined(L__stdlib_wcsto_ll) || defined(L__stdlib_wcsto_ll_l) */
643 #if defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE)
645 unsigned long long attribute_hidden _stdlib_strto_ll(register const Wchar * __restrict str,
646 Wchar ** __restrict endptr, int base,
649 return _stdlib_strto_ll_l(str, endptr, base, sflag, __UCLIBC_CURLOCALE);
653 #else /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
655 #if !defined __UCLIBC_HAS_XLOCALE__ && defined __UCLIBC_HAS_CTYPE_TABLES__
656 libc_hidden_proto(__ctype_b)
658 /* This is the main work fuction which handles both strtoll (sflag = 1) and
659 * strtoull (sflag = 0). */
661 unsigned long long attribute_hidden __XL_NPP(_stdlib_strto_ll)(register const Wchar * __restrict str,
662 Wchar ** __restrict endptr, int base,
663 int sflag __LOCALE_PARAM)
665 unsigned long long number;
667 const Wchar *fail_char;
668 #define SET_FAIL(X) fail_char = (X)
670 #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
673 unsigned char negative, digit;
675 assert(((unsigned int)sflag) <= 1);
679 while (ISSPACE(*str)) { /* Skip leading whitespace. */
683 /* Handle optional sign. */
686 case '-': negative = 1; /* Fall through to increment str. */
690 if (!(base & ~0x10)) { /* Either dynamic (base = 0) or base 16. */
691 base += 10; /* Default is 10 (26). */
694 base -= 2; /* Now base is 8 or 16 (24). */
695 if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
697 base += base; /* Base is 16 (16 or 48). */
701 if (base > 16) { /* Adjust in case base wasn't dynamic. */
708 if (((unsigned)(base - 2)) < 35) { /* Legal base. */
710 digit = ((Wuchar)(*str - '0') <= 9)
711 ? /* 0..9 */ (*str - '0')
712 : /* else */ (((Wuchar)(0x20 | *str) >= 'a') /* WARNING: assumes ascii. */
713 ? /* >= A/a */ ((Wuchar)(0x20 | *str) - ('a' - 10))
714 : /* else */ 40 /* bad value */);
723 /* Optional, but speeds things up in the usual case. */
724 if (number <= (ULLONG_MAX >> 6)) {
725 number = number * base + digit;
729 n1 = ((unsigned char) number) * base + digit;
730 number = (number >> CHAR_BIT) * base;
732 if (number + (n1 >> CHAR_BIT) <= (ULLONG_MAX >> CHAR_BIT)) {
733 number = (number << CHAR_BIT) + n1;
734 } else { /* Overflow. */
746 *endptr = (Wchar *) fail_char;
751 unsigned long long tmp = ((negative)
752 ? ((unsigned long long)(-(1+LLONG_MIN)))+1
754 if (sflag && (number > tmp)) {
760 return negative ? (unsigned long long)(-((long long)number)) : number;
763 #endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */
765 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
768 /**********************************************************************/
769 /* Made _Exit() an alias for _exit(), as per C99. */
771 /* libc_hidden_proto(_exit) */
772 /* void _Exit(int status) */
778 /**********************************************************************/
781 void *bsearch(const void *key, const void *base, size_t /* nmemb */ high,
782 size_t size, int (*compar)(const void *, const void *))
789 if (size > 0) { /* TODO: change this to an assert?? */
792 mid = low + ((high - low) >> 1); /* Avoid possible overflow here. */
793 p = ((char *)base) + mid * size; /* Could overflow here... */
794 r = (*compar)(key, p); /* but that's an application problem! */
808 /**********************************************************************/
811 /* This code is derived from a public domain shell sort routine by
812 * Ray Gardner and found in Bob Stout's snippets collection. The
813 * original code is included below in an #if 0/#endif block.
815 * I modified it to avoid the possibility of overflow in the wgap
816 * calculation, as well as to reduce the generated code size with
819 libc_hidden_proto(qsort)
820 void qsort(void *base,
823 int (*comp)(const void *, const void *))
825 size_t wgap, i, j, k;
828 if ((nel > 1) && (width > 0)) {
829 assert(nel <= ((size_t)(-1)) / width); /* check for overflow */
833 } while (wgap < (nel-1)/3);
834 /* From the above, we know that either wgap == 1 < nel or */
835 /* ((wgap-1)/3 < (int) ((nel-1)/3) <= (nel-1)/3 ==> wgap < nel. */
836 wgap *= width; /* So this can not overflow if wnel doesn't. */
837 nel *= width; /* Convert nel to 'wnel' */
847 a = j + ((char *)base);
849 if ((*comp)(a, b) <= 0) {
861 wgap = (wgap - width)/3;
865 libc_hidden_def(qsort)
867 /* ---------- original snippets version below ---------- */
871 ** ssort() -- Fast, small, qsort()-compatible Shell sort
873 ** by Ray Gardner, public domain 5/90
878 void ssort(void *base,
881 int (*comp)(const void *, const void *))
883 size_t wnel, gap, wgap, i, j, k;
887 for (gap = 0; ++gap < nel;)
889 while ((gap /= 3) != 0) {
891 for (i = wgap; i < wnel; i += width) {
892 for (j = i - wgap; ;j -= wgap) {
893 a = j + (char *)base;
895 if ((*comp)(a, b) <= 0)
912 /**********************************************************************/
913 #ifdef L__stdlib_mb_cur_max
915 libc_hidden_proto(_stdlib_mb_cur_max)
916 size_t _stdlib_mb_cur_max(void)
918 #ifdef __CTYPE_HAS_UTF_8_LOCALES
919 return __UCLIBC_CURLOCALE_DATA.mb_cur_max;
921 #ifdef __CTYPE_HAS_8_BIT_LOCALES
922 #ifdef __UCLIBC_MJN3_ONLY__
923 #warning need to change this when/if transliteration is implemented
929 libc_hidden_def(_stdlib_mb_cur_max)
932 /**********************************************************************/
935 libc_hidden_proto(mbrlen)
937 int mblen(register const char *s, size_t n)
939 static mbstate_t state;
944 #ifdef __CTYPE_HAS_UTF_8_LOCALES
945 return ENCODING == __ctype_encoding_utf8;
951 if ((r = mbrlen(s, n, &state)) == (size_t) -2) {
952 /* TODO: Should we set an error state? */
953 state.__wc = 0xffffU; /* Make sure we're in an error state. */
954 return -1; /* TODO: Change error code above? */
960 /**********************************************************************/
963 libc_hidden_proto(mbrtowc)
965 int mbtowc(wchar_t *__restrict pwc, register const char *__restrict s, size_t n)
967 static mbstate_t state;
972 #ifdef __CTYPE_HAS_UTF_8_LOCALES
973 return ENCODING == __ctype_encoding_utf8;
979 if ((r = mbrtowc(pwc, s, n, &state)) == (size_t) -2) {
980 /* TODO: Should we set an error state? */
981 state.__wc = 0xffffU; /* Make sure we're in an error state. */
982 return -1; /* TODO: Change error code above? */
988 /**********************************************************************/
991 /* Note: We completely ignore state in all currently supported conversions. */
993 libc_hidden_proto(wcrtomb)
995 int wctomb(register char *__restrict s, wchar_t swc)
999 #ifdef __CTYPE_HAS_UTF_8_LOCALES
1000 (ENCODING == __ctype_encoding_utf8)
1002 0 /* Encoding is stateless. */
1004 : ((ssize_t) wcrtomb(s, swc, NULL));
1008 /**********************************************************************/
1011 libc_hidden_proto(mbsrtowcs)
1013 size_t mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
1016 const char *e = s; /* Needed because of restrict. */
1018 state.__mask = 0; /* Always start in initial shift state. */
1019 return mbsrtowcs(pwcs, &e, n, &state);
1023 /**********************************************************************/
1026 /* Note: We completely ignore state in all currently supported conversions. */
1028 libc_hidden_proto(wcsrtombs)
1030 size_t wcstombs(char * __restrict s, const wchar_t * __restrict pwcs, size_t n)
1032 const wchar_t *e = pwcs; /* Needed because of restrict. */
1034 return wcsrtombs(s, &e, n, NULL);
1038 /**********************************************************************/
1039 #if defined(L_wcstol) || defined(L_wcstol_l)
1041 libc_hidden_proto(__XL_NPP(wcstol))
1042 long __XL_NPP(wcstol)(const wchar_t * __restrict str,
1043 wchar_t ** __restrict endptr, int base __LOCALE_PARAM)
1045 return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 1 __LOCALE_ARG);
1047 libc_hidden_def(__XL_NPP(wcstol))
1049 #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstol_l)
1050 strong_alias(wcstol,wcstoimax)
1053 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
1059 extern __typeof(wcstol) __XL_NPP(wcstoll);
1060 libc_hidden_proto(__XL_NPP(wcstoll))
1061 strong_alias(__XL_NPP(wcstol),__XL_NPP(wcstoll))
1062 libc_hidden_def(__XL_NPP(wcstoll))
1066 /**********************************************************************/
1067 #if defined(L_wcstoll) || defined(L_wcstoll_l)
1069 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
1071 libc_hidden_proto(__XL_NPP(wcstoll))
1072 long long __XL_NPP(wcstoll)(const wchar_t * __restrict str,
1073 wchar_t ** __restrict endptr, int base
1076 return (long long) __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 1 __LOCALE_ARG);
1078 libc_hidden_def(__XL_NPP(wcstoll))
1080 #if !defined(L_wcstoll_l)
1081 #if (ULLONG_MAX == UINTMAX_MAX)
1082 strong_alias(wcstoll,wcstoimax)
1084 strong_alias(wcstoll,wcstoq)
1087 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
1090 /**********************************************************************/
1091 #if defined(L_wcstoul) || defined(L_wcstoul_l)
1093 libc_hidden_proto(__XL_NPP(wcstoul))
1094 unsigned long __XL_NPP(wcstoul)(const wchar_t * __restrict str,
1095 wchar_t ** __restrict endptr, int base
1098 return __XL_NPP(_stdlib_wcsto_l)(str, endptr, base, 0 __LOCALE_ARG);
1100 libc_hidden_def(__XL_NPP(wcstoul))
1102 #if (ULONG_MAX == UINTMAX_MAX) && !defined(L_wcstoul_l)
1103 strong_alias(wcstoul,wcstoumax)
1106 #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
1112 extern __typeof(wcstoul) __XL_NPP(wcstoull);
1113 libc_hidden_proto(__XL_NPP(wcstoull))
1114 strong_alias(__XL_NPP(wcstoul),__XL_NPP(wcstoull))
1115 libc_hidden_def(__XL_NPP(wcstoull))
1119 /**********************************************************************/
1120 #if defined(L_wcstoull) || defined(L_wcstoull_l)
1122 #if defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX)
1124 libc_hidden_proto(__XL_NPP(wcstoull))
1125 unsigned long long __XL_NPP(wcstoull)(const wchar_t * __restrict str,
1126 wchar_t ** __restrict endptr, int base
1129 return __XL_NPP(_stdlib_wcsto_ll)(str, endptr, base, 0 __LOCALE_ARG);
1131 libc_hidden_def(__XL_NPP(wcstoull))
1133 #if !defined(L_wcstoull_l)
1134 #if (ULLONG_MAX == UINTMAX_MAX)
1135 strong_alias(wcstoull,wcstoumax)
1137 strong_alias(wcstoull,wcstouq)
1140 #endif /* defined(ULLONG_MAX) && (LLONG_MAX > LONG_MAX) */
1143 /**********************************************************************/