netinet/in.h: add IPPROTO_MH
[uclibc-ng.git] / libc / stdio / _wfwrite.c
blob65b38e7e789bbacde960556bdeab72d70d74abb7
1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org>
3 * GNU Library General Public License (LGPL) version 2 or later.
5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details.
6 */
8 #include "_stdio.h"
9 #include <wchar.h>
11 #ifndef __UCLIBC_HAS_WCHAR__
12 #error wide function when no wide support!
13 #endif
15 size_t attribute_hidden _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
16 register FILE *__restrict stream)
18 size_t r, count;
19 char buf[64];
20 const wchar_t *pw;
22 __STDIO_STREAM_VALIDATE(stream);
24 #ifdef __STDIO_BUFFERS
25 if (__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)) {
26 /* We know buffer is wchar aligned for fake streams. */
27 count = (((wchar_t *)(stream->__bufend))
28 - ((wchar_t *)(stream->__bufpos)));
29 if (count > n) {
30 count = n;
32 if (count) {
33 wmemcpy((wchar_t *)(stream->__bufpos), ws, count);
34 stream->__bufpos = (unsigned char *)(((wchar_t *)(stream->__bufpos)) + count);
36 __STDIO_STREAM_VALIDATE(stream);
37 return n;
39 #endif
41 count = 0;
43 if (__STDIO_STREAM_IS_WIDE_WRITING(stream)
44 || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_WIDE)
45 ) {
47 pw = ws;
48 while (n > count) {
49 r = wcsnrtombs(buf, &pw, n-count, sizeof(buf), &stream->__state);
50 if (r != ((size_t) -1)) { /* No encoding errors */
51 if (!r) {
52 ++r; /* 0 is returned when nul is reached. */
53 pw = ws + count + r; /* pw was set to NULL, so correct. */
55 if (__stdio_fwrite((const unsigned char *)buf, r, stream) == r) {
56 count = pw - ws;
57 continue;
60 break;
63 /* Note: The count is incorrect if 0 < __stdio_fwrite return < r!!! */
66 __STDIO_STREAM_VALIDATE(stream);
67 return count;