Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / mbrtowc.c
blobe641b8cd62d73735a16feb87eb6de5708283cc88
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 <string.h>
8 #include "local.h"
10 #ifdef _REENT_THREAD_LOCAL
11 _Thread_local _mbstate_t _tls_mbrtowc_state;
12 #endif
14 size_t
15 _mbrtowc_r (struct _reent *ptr,
16 wchar_t *pwc,
17 const char *s,
18 size_t n,
19 mbstate_t *ps)
21 int retval = 0;
23 #ifdef _MB_CAPABLE
24 if (ps == NULL)
26 _REENT_CHECK_MISC(ptr);
27 ps = &(_REENT_MBRTOWC_STATE(ptr));
29 #endif
31 if (s == NULL)
32 retval = __MBTOWC (ptr, NULL, "", 1, ps);
33 else
34 retval = __MBTOWC (ptr, pwc, s, n, ps);
36 if (retval == -1)
38 ps->__count = 0;
39 _REENT_ERRNO(ptr) = EILSEQ;
40 return (size_t)(-1);
42 else
43 return (size_t)retval;
46 #ifndef _REENT_ONLY
47 size_t
48 mbrtowc (wchar_t *__restrict pwc,
49 const char *__restrict s,
50 size_t n,
51 mbstate_t *__restrict ps)
53 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
54 return _mbrtowc_r (_REENT, pwc, s, n, ps);
55 #else
56 int retval = 0;
57 struct _reent *reent = _REENT;
59 #ifdef _MB_CAPABLE
60 if (ps == NULL)
62 _REENT_CHECK_MISC(reent);
63 ps = &(_REENT_MBRTOWC_STATE(reent));
65 #endif
67 if (s == NULL)
68 retval = __MBTOWC (reent, NULL, "", 1, ps);
69 else
70 retval = __MBTOWC (reent, pwc, s, n, ps);
72 if (retval == -1)
74 ps->__count = 0;
75 _REENT_ERRNO(reent) = EILSEQ;
76 return (size_t)(-1);
78 else
79 return (size_t)retval;
80 #endif /* not PREFER_SIZE_OVER_SPEED */
82 #endif /* !_REENT_ONLY */