iofwrite.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 1993-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include "libioP.h"
  23. size_t
  24. _IO_fwrite (const void *buf, size_t size, size_t count, FILE *fp)
  25. {
  26. size_t request = size * count;
  27. size_t written = 0;
  28. CHECK_FILE (fp, 0);
  29. if (request == 0)
  30. return 0;
  31. _IO_acquire_lock (fp);
  32. if (_IO_vtable_offset (fp) != 0 || _IO_fwide (fp, -1) == -1)
  33. {
  34. /* Compute actually written bytes plus pending buffer
  35. contents. */
  36. uint64_t original_total_written
  37. = fp->_total_written + (fp->_IO_write_ptr - fp->_IO_write_base);
  38. written = _IO_sputn (fp, (const char *) buf, request);
  39. if (written == EOF)
  40. {
  41. /* An error happened and we need to find the appropriate return
  42. value. There 3 possible scenarios:
  43. 1. If the number of bytes written is between 0..[buffer content],
  44. we need to return 0 because none of the bytes from this
  45. request have been written;
  46. 2. If the number of bytes written is between
  47. [buffer content]+1..request-1, that means we managed to write
  48. data requested in this fwrite call;
  49. 3. We might have written all the requested data and got an error
  50. anyway. We can't return success, which means we still have to
  51. return less than request. */
  52. if (fp->_total_written > original_total_written)
  53. {
  54. written = fp->_total_written - original_total_written;
  55. /* If everything was reported as written and somehow an
  56. error occurred afterwards, avoid reporting success. */
  57. if (written == request)
  58. --written;
  59. }
  60. else
  61. /* Only already-pending buffer contents was written. */
  62. written = 0;
  63. }
  64. }
  65. _IO_release_lock (fp);
  66. /* We have written all of the input in case the return value indicates
  67. this. */
  68. if (written == request)
  69. return count;
  70. else
  71. return written / size;
  72. }
  73. libc_hidden_def (_IO_fwrite)
  74. # include <stdio.h>
  75. static_weak_alias (_IO_fwrite, fwrite)
  76. libc_hidden_weak (fwrite)
  77. # ifndef _IO_MTSAFE_IO
  78. weak_alias (_IO_fwrite, fwrite_unlocked)
  79. libc_hidden_weak (fwrite_unlocked)
  80. # endif