_wfwrite.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. extern size_t __wcsnrtombs(char *__restrict dst,
  16. const wchar_t **__restrict src,
  17. size_t NWC, size_t len, mbstate_t *__restrict ps) attribute_hidden;
  18. size_t attribute_hidden _wstdio_fwrite(const wchar_t *__restrict ws, size_t n,
  19. register FILE *__restrict stream)
  20. {
  21. size_t r, count;
  22. char buf[64];
  23. const wchar_t *pw;
  24. __STDIO_STREAM_VALIDATE(stream);
  25. #ifdef __STDIO_BUFFERS
  26. if (__STDIO_STREAM_IS_FAKE_VSWPRINTF(stream)) {
  27. /* We know buffer is wchar aligned for fake streams. */
  28. count = (((wchar_t *)(stream->__bufend))
  29. - ((wchar_t *)(stream->__bufpos)));
  30. if (count > n) {
  31. count = n;
  32. }
  33. if (count) {
  34. wmemcpy((wchar_t *)(stream->__bufpos), ws, count);
  35. stream->__bufpos = (char *)(((wchar_t *)(stream->__bufpos)) + count);
  36. }
  37. __STDIO_STREAM_VALIDATE(stream);
  38. return n;
  39. }
  40. #endif
  41. count = 0;
  42. if (__STDIO_STREAM_IS_WIDE_WRITING(stream)
  43. || !__STDIO_STREAM_TRANS_TO_WRITE(stream, __FLAG_WIDE)
  44. ) {
  45. pw = ws;
  46. while (n > count) {
  47. r = __wcsnrtombs(buf, &pw, n-count, sizeof(buf), &stream->__state);
  48. if (r != ((size_t) -1)) { /* No encoding errors */
  49. if (!r) {
  50. ++r; /* 0 is returned when nul is reached. */
  51. pw = ws + count + r; /* pw was set to NULL, so correct. */
  52. }
  53. if (__stdio_fwrite(buf, r, stream) == r) {
  54. count = pw - ws;
  55. continue;
  56. }
  57. }
  58. break;
  59. }
  60. /* Note: The count is incorrect if 0 < __stdio_fwrite return < r!!! */
  61. }
  62. __STDIO_STREAM_VALIDATE(stream);
  63. return count;
  64. }