_fwrite.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #ifdef __UCLIBC_MJN3_ONLY__
  22. #warning CONSIDER: Try to consolidate some of the code?
  23. #endif
  24. if (__STDIO_STREAM_IS_FAKE_VSNPRINTF(stream)) {
  25. pending = __STDIO_STREAM_BUFFER_WAVAIL(stream);
  26. if (pending > bytes) {
  27. pending = bytes;
  28. }
  29. memcpy(stream->__bufpos, buffer, pending);
  30. stream->__bufpos += pending;
  31. __STDIO_STREAM_VALIDATE(stream);
  32. return bytes;
  33. }
  34. /* RETRY: */
  35. if (bytes <= __STDIO_STREAM_BUFFER_WAVAIL(stream)) {
  36. memcpy(stream->__bufpos, buffer, bytes);
  37. stream->__bufpos += bytes;
  38. if (__STDIO_STREAM_IS_LBF(stream)
  39. && memrchr(buffer, '\n', bytes) /* Search backwards. */
  40. ) {
  41. if ((pending = __STDIO_COMMIT_WRITE_BUFFER(stream)) > 0) {
  42. if (pending > bytes) {
  43. pending = bytes;
  44. }
  45. buffer += (bytes - pending);
  46. if ((p = memchr(buffer, '\n', pending)) != NULL) {
  47. pending = (buffer + pending) - p;
  48. bytes -= pending;
  49. stream->__bufpos -= pending;
  50. }
  51. }
  52. }
  53. __STDIO_STREAM_VALIDATE(stream);
  54. return bytes;
  55. }
  56. /* FBF or LBF and not enough space in buffer. */
  57. if (__STDIO_STREAM_BUFFER_WUSED(stream)) { /* Buffered data. */
  58. if (__STDIO_COMMIT_WRITE_BUFFER(stream)) { /* Commit failed! */
  59. return 0;
  60. }
  61. #ifdef __UCLIBC_MJN3_ONLY__
  62. #warning CONSIDER: Do we want to try again if data now fits in buffer?
  63. #endif
  64. /* goto RETRY; */
  65. }
  66. }
  67. return __stdio_WRITE(stream, buffer, bytes);
  68. }
  69. #endif