Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / wcstombs_r.c
blobc6a06a39a57ed98242501afbbe4522433bc3e73e
1 #include <stdlib.h>
2 #include <wchar.h>
3 #include "local.h"
5 size_t
6 _wcstombs_r (struct _reent *r,
7 char *__restrict s,
8 const wchar_t *__restrict pwcs,
9 size_t n,
10 mbstate_t *state)
12 char *ptr = s;
13 size_t max = n;
14 char buff[8];
15 int i, bytes, num_to_copy;
17 if (s == NULL)
19 size_t num_bytes = 0;
20 while (*pwcs != 0)
22 bytes = __WCTOMB (r, buff, *pwcs++, state);
23 if (bytes == -1)
24 return -1;
25 num_bytes += bytes;
27 return num_bytes;
29 else
31 while (n > 0)
33 bytes = __WCTOMB (r, buff, *pwcs, state);
34 if (bytes == -1)
35 return -1;
36 num_to_copy = (n > bytes ? bytes : (int)n);
37 for (i = 0; i < num_to_copy; ++i)
38 *ptr++ = buff[i];
40 if (*pwcs == 0x00)
41 return ptr - s - (n >= bytes);
42 ++pwcs;
43 n -= num_to_copy;
45 return max;