_WRITE.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. __STDIO_STREAM_SET_ERROR(stream);
  55. /* We buffer data on "transient" errors, but discard it
  56. * on "hard" ones. Example of a hard error:
  57. *
  58. * close(fileno(stdout));
  59. * printf("Hi there 1\n"); // EBADF
  60. * dup2(good_fd, fileno(stdout));
  61. * printf("Hi there 2\n"); // buffers new data
  62. *
  63. * This program should not print "Hi there 1" to good_fd.
  64. * The rationale is that the caller of writing operation
  65. * should check for error and act on it.
  66. * If he didn't, then future users of the stream
  67. * have no idea what to do.
  68. * It's least confusing to at least not burden them with
  69. * some hidden buffered crap in the buffer.
  70. */
  71. if (errno != EINTR && errno != EAGAIN) {
  72. /* do we have other "soft" errors? */
  73. break;
  74. }
  75. #ifdef __STDIO_BUFFERS
  76. stodo = __STDIO_STREAM_BUFFER_SIZE(stream);
  77. if (stodo != 0) {
  78. unsigned char *s;
  79. if (stodo > todo) {
  80. stodo = todo;
  81. }
  82. s = stream->__bufstart;
  83. do {
  84. *s = *buf;
  85. if ((*s == '\n')
  86. && __STDIO_STREAM_IS_LBF(stream)
  87. ) {
  88. break;
  89. }
  90. ++s;
  91. ++buf;
  92. } while (--stodo);
  93. stream->__bufpos = s;
  94. todo -= (s - stream->__bufstart);
  95. }
  96. #endif /* __STDIO_BUFFERS */
  97. bufsize -= todo;
  98. break;
  99. }
  100. }
  101. __STDIO_STREAM_VALIDATE(stream);
  102. return bufsize;
  103. }