Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / mbsnrtowcs.c
blob2effc501cdfc05f4f8cafbff672794ebe6683123
1 /*
2 FUNCTION
3 <<mbsrtowcs>>, <<mbsnrtowcs>>---convert a character string to a wide-character string
5 INDEX
6 mbsrtowcs
7 INDEX
8 _mbsrtowcs_r
9 INDEX
10 mbsnrtowcs
11 INDEX
12 _mbsnrtowcs_r
14 SYNOPSIS
15 #include <wchar.h>
16 size_t mbsrtowcs(wchar_t *__restrict <[dst]>,
17 const char **__restrict <[src]>,
18 size_t <[len]>,
19 mbstate_t *__restrict <[ps]>);
21 #include <wchar.h>
22 size_t _mbsrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>,
23 const char **<[src]>, size_t <[len]>,
24 mbstate_t *<[ps]>);
26 #include <wchar.h>
27 size_t mbsnrtowcs(wchar_t *__ restrict <[dst]>,
28 const char **__restrict <[src]>, size_t <[nms]>,
29 size_t <[len]>, mbstate_t *__restrict <[ps]>);
31 #include <wchar.h>
32 size_t _mbsnrtowcs_r(struct _reent *<[ptr]>, wchar_t *<[dst]>,
33 const char **<[src]>, size_t <[nms]>,
34 size_t <[len]>, mbstate_t *<[ps]>);
36 DESCRIPTION
37 The <<mbsrtowcs>> function converts a sequence of multibyte characters
38 pointed to indirectly by <[src]> into a sequence of corresponding wide
39 characters and stores at most <[len]> of them in the wchar_t array pointed
40 to by <[dst]>, until it encounters a terminating null character ('\0').
42 If <[dst]> is NULL, no characters are stored.
44 If <[dst]> is not NULL, the pointer pointed to by <[src]> is updated to point
45 to the character after the one that conversion stopped at. If conversion
46 stops because a null character is encountered, *<[src]> is set to NULL.
48 The mbstate_t argument, <[ps]>, is used to keep track of the shift state. If
49 it is NULL, <<mbsrtowcs>> uses an internal, static mbstate_t object, which
50 is initialized to the initial conversion state at program startup.
52 The <<mbsnrtowcs>> function behaves identically to <<mbsrtowcs>>, except that
53 conversion stops after reading at most <[nms]> bytes from the buffer pointed
54 to by <[src]>.
56 RETURNS
57 The <<mbsrtowcs>> and <<mbsnrtowcs>> functions return the number of wide
58 characters stored in the array pointed to by <[dst]> if successful, otherwise
59 it returns (size_t)-1.
61 PORTABILITY
62 <<mbsrtowcs>> is defined by the C99 standard.
63 <<mbsnrtowcs>> is defined by the POSIX.1-2008 standard.
66 #include <reent.h>
67 #include <newlib.h>
68 #include <wchar.h>
69 #include <stdlib.h>
70 #include <stdio.h>
71 #include <errno.h>
73 #ifdef _REENT_THREAD_LOCAL
74 _Thread_local _mbstate_t _tls_mbsrtowcs_state;
75 #endif
77 size_t
78 _mbsnrtowcs_r (struct _reent *r,
79 wchar_t *dst,
80 const char **src,
81 size_t nms,
82 size_t len,
83 mbstate_t *ps)
85 wchar_t *ptr = dst;
86 const char *tmp_src;
87 size_t max;
88 size_t count = 0;
89 int bytes;
91 #ifdef _MB_CAPABLE
92 if (ps == NULL)
94 _REENT_CHECK_MISC(r);
95 ps = &(_REENT_MBSRTOWCS_STATE(r));
97 #endif
99 if (dst == NULL)
101 /* Ignore original len value and do not alter src pointer if the
102 dst pointer is NULL. */
103 len = (size_t)-1;
104 tmp_src = *src;
105 src = &tmp_src;
108 max = len;
109 while (len > 0)
111 bytes = _mbrtowc_r (r, ptr, *src, nms, ps);
112 if (bytes > 0)
114 *src += bytes;
115 nms -= bytes;
116 ++count;
117 ptr = (dst == NULL) ? NULL : ptr + 1;
118 --len;
120 else if (bytes == -2)
122 *src += nms;
123 return count;
125 else if (bytes == 0)
127 *src = NULL;
128 return count;
130 else
132 ps->__count = 0;
133 _REENT_ERRNO(r) = EILSEQ;
134 return (size_t)-1;
138 return (size_t)max;
141 #ifndef _REENT_ONLY
142 size_t
143 mbsnrtowcs (wchar_t *__restrict dst,
144 const char **__restrict src,
145 size_t nms,
146 size_t len,
147 mbstate_t *__restrict ps)
149 return _mbsnrtowcs_r (_REENT, dst, src, nms, len, ps);
151 #endif /* !_REENT_ONLY */