_fwrite.c 1.8 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. #ifdef __STDIO_BUFFERS
  9. /* Either buffer data or (commit buffer if necessary and) write. */
  10. size_t attribute_hidden __stdio_fwrite(const unsigned char * __restrict buffer,
  11. size_t bytes,
  12. register FILE * __restrict stream)
  13. {
  14. size_t pending;
  15. const unsigned char *p;
  16. __STDIO_STREAM_VALIDATE(stream);
  17. assert(__STDIO_STREAM_IS_WRITING(stream));
  18. assert(buffer);
  19. assert(bytes);
  20. if (!__STDIO_STREAM_IS_NBF(stream)) { /* FBF or LBF. */
  21. if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)) {
  22. pending = __STDIO_STREAM_BUFFER_WAVAIL(stream);
  23. if (pending > bytes) {
  24. pending = bytes;
  25. }
  26. memcpy(stream->__bufpos, buffer, pending);
  27. stream->__bufpos += pending;
  28. __STDIO_STREAM_VALIDATE(stream);
  29. return bytes;
  30. }
  31. /* RETRY: */
  32. if (bytes <= __STDIO_STREAM_BUFFER_WAVAIL(stream)) {
  33. memcpy(stream->__bufpos, buffer, bytes);
  34. stream->__bufpos += bytes;
  35. if (__STDIO_STREAM_IS_LBF(stream)
  36. && memrchr(buffer, '\n', bytes) /* Search backwards. */
  37. ) {
  38. if ((pending = __STDIO_COMMIT_WRITE_BUFFER(stream)) > 0) {
  39. if (pending > bytes) {
  40. pending = bytes;
  41. }
  42. buffer += (bytes - pending);
  43. if ((p = memchr(buffer, '\n', pending)) != NULL) {
  44. pending = (buffer + pending) - p;
  45. bytes -= pending;
  46. stream->__bufpos -= pending;
  47. }
  48. }
  49. }
  50. __STDIO_STREAM_VALIDATE(stream);
  51. return bytes;
  52. }
  53. /* FBF or LBF and not enough space in buffer. */
  54. if (__STDIO_STREAM_BUFFER_WUSED(stream)) { /* Buffered data. */
  55. if (__STDIO_COMMIT_WRITE_BUFFER(stream)) { /* Commit failed! */
  56. return 0;
  57. }
  58. /* goto RETRY; */
  59. }
  60. }
  61. return __stdio_WRITE(stream, buffer, bytes);
  62. }
  63. #endif