_wfwrite.c 1.7 KB

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