2000-02-18 03:39:52 +08:00
|
|
|
#include <stdlib.h>
|
2002-08-23 09:56:05 +08:00
|
|
|
#include <wchar.h>
|
2009-11-18 17:49:57 +08:00
|
|
|
#include "local.h"
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
size_t
|
|
|
|
_DEFUN (_wcstombs_r, (reent, s, pwcs, n, state),
|
|
|
|
struct _reent *r _AND
|
2013-11-18 Sahil Patnayakuni <sahilp@oarcorp.com>
* libc/include/stdlib.h, libc/stdlib/mbstowcs.c,
libc/stdlib/mbstowcs_r.c, libc/stdlib/mbtowc.c,
libc/stdlib/mbtowc_r.c, libc/stdlib/strtod.c,
libc/stdlib/strtol.c, libc/stdlib/strtold.c,
libc/stdlib/strtoll.c, libc/stdlib/strtoll_r.c,
libc/stdlib/strtoul.c, libc/stdlib/strtoull.c,
libc/stdlib/strtoull_r.c, libc/stdlib/wcstombs.c,
libc/stdlib/wcstombs_r.c: Add restrict keyword.
2013-11-19 01:26:52 +08:00
|
|
|
char *__restrict s _AND
|
|
|
|
const wchar_t *__restrict pwcs _AND
|
2000-02-18 03:39:52 +08:00
|
|
|
size_t n _AND
|
2002-08-23 09:56:05 +08:00
|
|
|
mbstate_t *state)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
char *ptr = s;
|
|
|
|
size_t max = n;
|
|
|
|
char buff[8];
|
2010-01-20 05:14:53 +08:00
|
|
|
int i, bytes, num_to_copy;
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2007-10-24 03:50:29 +08:00
|
|
|
if (s == NULL)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2007-10-24 03:50:29 +08:00
|
|
|
size_t num_bytes = 0;
|
|
|
|
while (*pwcs != 0)
|
2010-01-20 05:14:53 +08:00
|
|
|
{
|
|
|
|
bytes = __wctomb (r, buff, *pwcs++, __locale_charset (), state);
|
|
|
|
if (bytes == -1)
|
|
|
|
return -1;
|
|
|
|
num_bytes += bytes;
|
|
|
|
}
|
2007-10-24 03:50:29 +08:00
|
|
|
return num_bytes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n > 0)
|
|
|
|
{
|
2010-01-20 05:14:53 +08:00
|
|
|
bytes = __wctomb (r, buff, *pwcs, __locale_charset (), state);
|
2007-10-24 03:50:29 +08:00
|
|
|
if (bytes == -1)
|
|
|
|
return -1;
|
|
|
|
num_to_copy = (n > bytes ? bytes : (int)n);
|
|
|
|
for (i = 0; i < num_to_copy; ++i)
|
|
|
|
*ptr++ = buff[i];
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2007-10-24 03:50:29 +08:00
|
|
|
if (*pwcs == 0x00)
|
|
|
|
return ptr - s - (n >= bytes);
|
|
|
|
++pwcs;
|
|
|
|
n -= num_to_copy;
|
|
|
|
}
|
|
|
|
return max;
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
}
|