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
|
2017-12-04 11:43:30 +08:00
|
|
|
_wcstombs_r (struct _reent *r,
|
2017-12-04 09:31:41 +08:00
|
|
|
char *__restrict s,
|
|
|
|
const wchar_t *__restrict pwcs,
|
|
|
|
size_t n,
|
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
|
|
|
{
|
2016-07-21 04:05:59 +08:00
|
|
|
bytes = __WCTOMB (r, buff, *pwcs++, state);
|
2010-01-20 05:14:53 +08:00
|
|
|
if (bytes == -1)
|
|
|
|
return -1;
|
|
|
|
num_bytes += bytes;
|
|
|
|
}
|
2007-10-24 03:50:29 +08:00
|
|
|
return num_bytes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (n > 0)
|
|
|
|
{
|
2016-07-21 04:05:59 +08:00
|
|
|
bytes = __WCTOMB (r, buff, *pwcs, 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
|
|
|
}
|
|
|
|
}
|