No space calculation in wcstombs?

Christoph Ender
Tue Oct 23 15:57:00 GMT 2007


thanks for your quick answer. I've taken a look at the source
from the CVS and "fixed" the issue by replacing the contents of
the file "wcstombs_r.c" with the quick-and-dirty solution appended
below (which may still contain some serious flaw since I've never
fiddled with a c-lib before but it works for me).

Since I also need fwprintf and other functions which are currently
not implemented I might extend newlib a bit more to get my program
working. Would it make sense to mail the new code to some official
maintainer so that someday these functions might be available in a
regular release?



#include <stdlib.h>
#include <wchar.h>

_DEFUN (_wcstombs_r, (reent, s, pwcs, n, state),
         struct _reent *r    _AND
         char          *s    _AND
         const wchar_t *pwcs _AND
         size_t         n    _AND
         mbstate_t     *state)
   char *ptr = s;
   size_t max = n;
   char buff[8];
   int i, num_to_copy;
   int sum_bytes;

   if (s == NULL)
     sum_bytes = 0;

     while (*pwcs != 0)
       sum_bytes += _wctomb_r (r, buff, *pwcs, state);

     return sum_bytes;
     while (n > 0)
         int bytes = _wctomb_r (r, buff, *pwcs, state);
         if (bytes == -1)
           return -1;
         num_to_copy = (n > bytes ? bytes : (int)n);
         for (i = 0; i < num_to_copy; ++i)
           *ptr++ = buff[i];

         if (*pwcs == 0x00)
           return ptr - s - (n >= bytes);
         n -= num_to_copy;

     return max;


> On Oct 23 11:52, Christoph Ender wrote:
>> Hi all,
>> when converting wchar_t*- to (mb)char*-Strings I've been
>> so far using the wcstombs function in two steps: One call
>> [...]
>> I've just noticed that the first step does not work with
>> cygwin and  I've attached a small example to demonstrate
>> [...]

On October 23, 2007 at 12:42 Corinna Vinschen wrote:
> No, it's a newlib problem.  The wide character support in newlib
> is somewhat rudimentary.  The reason is that newlib is mainly
> targeting embedded systems which don't need full wchar support.
> However, as this is an open source project, patches are always
> welcome.
> Corinna

More information about the Newlib mailing list