Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / wcrtomb.c
blob1b84720ac42d3d36d7f92616ea27f3d1b8794992
1 #include <reent.h>
2 #include <newlib.h>
3 #include <wchar.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include "local.h"
9 #ifdef _REENT_THREAD_LOCAL
10 _Thread_local _mbstate_t _tls_wcrtomb_state;
11 #endif
13 size_t
14 _wcrtomb_r (struct _reent *ptr,
15 char *s,
16 wchar_t wc,
17 mbstate_t *ps)
19 int retval = 0;
20 char buf[10];
22 #ifdef _MB_CAPABLE
23 if (ps == NULL)
25 _REENT_CHECK_MISC(ptr);
26 ps = &(_REENT_WCRTOMB_STATE(ptr));
28 #endif
30 if (s == NULL)
31 retval = __WCTOMB (ptr, buf, L'\0', ps);
32 else
33 retval = __WCTOMB (ptr, s, wc, ps);
35 if (retval == -1)
37 ps->__count = 0;
38 _REENT_ERRNO(ptr) = EILSEQ;
39 return (size_t)(-1);
41 else
42 return (size_t)retval;
45 #ifndef _REENT_ONLY
46 size_t
47 wcrtomb (char *__restrict s,
48 wchar_t wc,
49 mbstate_t *__restrict ps)
51 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
52 return _wcrtomb_r (_REENT, s, wc, ps);
53 #else
54 int retval = 0;
55 struct _reent *reent = _REENT;
56 char buf[10];
58 #ifdef _MB_CAPABLE
59 if (ps == NULL)
61 _REENT_CHECK_MISC(reent);
62 ps = &(_REENT_WCRTOMB_STATE(reent));
64 #endif
66 if (s == NULL)
67 retval = __WCTOMB (reent, buf, L'\0', ps);
68 else
69 retval = __WCTOMB (reent, s, wc, ps);
71 if (retval == -1)
73 ps->__count = 0;
74 _REENT_ERRNO(reent) = EILSEQ;
75 return (size_t)(-1);
77 else
78 return (size_t)retval;
79 #endif /* not PREFER_SIZE_OVER_SPEED */
81 #endif /* !_REENT_ONLY */