_WRITE.c 3.1 KB

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