_WRITE.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /* Given a writing stream with no buffered output, write the
  9. * data in 'buf' (which may be the stream's bufstart) of size
  10. * 'bufsize' to the stream. If a write error occurs, set the
  11. * stream's error indicator and (if buffering) buffer as much
  12. * data as possible (FBF) or only up to '\n' (LBF) to implement
  13. * "as if fputc()" clause in the standard.
  14. *
  15. * Returns the number of bytes written and/or buffered.
  16. *
  17. * Notes:
  18. * Calling with bufsize == 0 is permitted, and buf is ignored in
  19. * that case.
  20. * We implement fflush() by setting bufpos to bufstart and passing
  21. * bufstart as the buf arg. If there is a write error, the
  22. * unwritten buffered data will simply be moved to the beginning
  23. * of the buffer. Since the data obviously fits in the buffer
  24. * and since there will be no '\n' chars in the buffer in the LBF
  25. * case, no data will be lost.
  26. * NOT THREADSAFE! Assumes stream already locked if necessary.
  27. */
  28. size_t attribute_hidden __stdio_WRITE(register FILE *stream,
  29. register const unsigned char *buf, size_t bufsize)
  30. {
  31. size_t todo;
  32. ssize_t rv, stodo;
  33. __STDIO_STREAM_VALIDATE(stream);
  34. assert(stream->__filedes >= -1);
  35. assert(__STDIO_STREAM_IS_WRITING(stream));
  36. assert(!__STDIO_STREAM_BUFFER_WUSED(stream)); /* Buffer must be empty. */
  37. todo = bufsize;
  38. while (todo != 0) {
  39. stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
  40. rv = __WRITE(stream, (char *) buf, stodo);
  41. if (rv >= 0) {
  42. #ifdef __UCLIBC_MJN3_ONLY__
  43. #warning TODO: Make custom stream write return check optional.
  44. #endif
  45. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  46. assert(rv <= stodo);
  47. if (rv > stodo) { /* Wrote more than stodo! */
  48. /* abort(); */
  49. }
  50. #endif
  51. todo -= rv;
  52. buf += rv;
  53. } else
  54. #ifdef __UCLIBC_MJN3_ONLY__
  55. #warning EINTR?
  56. #endif
  57. /* if (errno != EINTR) */
  58. {
  59. __STDIO_STREAM_SET_ERROR(stream);
  60. #ifdef __STDIO_BUFFERS
  61. stodo = __STDIO_STREAM_BUFFER_SIZE(stream);
  62. if (stodo != 0) {
  63. unsigned char *s;
  64. if (stodo > todo) {
  65. stodo = todo;
  66. }
  67. s = stream->__bufstart;
  68. do {
  69. *s = *buf;
  70. if ((*s == '\n')
  71. && __STDIO_STREAM_IS_LBF(stream)
  72. ) {
  73. break;
  74. }
  75. ++s;
  76. ++buf;
  77. } while (--stodo);
  78. stream->__bufpos = s;
  79. todo -= (s - stream->__bufstart);
  80. }
  81. #endif /* __STDIO_BUFFERS */
  82. bufsize -= todo;
  83. break;
  84. }
  85. }
  86. __STDIO_STREAM_VALIDATE(stream);
  87. return bufsize;
  88. }