source: trunk/libs/newlib/src/newlib/libc/stdlib/wcstombs_r.c @ 471

Last change on this file since 471 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 970 bytes
Line 
1#include <stdlib.h>
2#include <wchar.h>
3#include "local.h"
4
5size_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)
11{
12  char *ptr = s;
13  size_t max = n;
14  char buff[8];
15  int i, bytes, num_to_copy;
16
17  if (s == NULL)
18    {
19      size_t num_bytes = 0;
20      while (*pwcs != 0)
21        {
22          bytes = __WCTOMB (r, buff, *pwcs++, state);
23          if (bytes == -1)
24            return -1;
25          num_bytes += bytes;
26        }
27      return num_bytes;
28    }
29  else
30    {
31      while (n > 0)
32        {
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];
39         
40          if (*pwcs == 0x00)
41            return ptr - s - (n >= bytes);
42          ++pwcs;
43          n -= num_to_copy;
44        }
45      return max;
46    }
47} 
Note: See TracBrowser for help on using the repository browser.