_WRITE.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. do {
  39. if (todo == 0) { /* Done? */
  40. __STDIO_STREAM_VALIDATE(stream);
  41. return bufsize;
  42. }
  43. stodo = (todo <= SSIZE_MAX) ? todo : SSIZE_MAX;
  44. if ((rv = __WRITE(stream, buf, stodo)) >= 0) {
  45. #ifdef __UCLIBC_MJN3_ONLY__
  46. #warning TODO: Make custom stream write return check optional.
  47. #endif
  48. #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
  49. assert(rv <= stodo);
  50. if (rv > stodo) { /* Wrote more than stodo! */
  51. /* abort(); */
  52. }
  53. #endif
  54. todo -= rv;
  55. buf += rv;
  56. } else
  57. #ifdef __UCLIBC_MJN3_ONLY__
  58. #warning EINTR?
  59. #endif
  60. /* if (errno != EINTR) */
  61. {
  62. __STDIO_STREAM_SET_ERROR(stream);
  63. #ifdef __STDIO_BUFFERS
  64. if ((stodo = __STDIO_STREAM_BUFFER_SIZE(stream)) != 0) {
  65. unsigned char *s;
  66. if (stodo > todo) {
  67. stodo = todo;
  68. }
  69. s = stream->__bufstart;
  70. do {
  71. if (((*s = *buf) == '\n')
  72. && __STDIO_STREAM_IS_LBF(stream)
  73. ) {
  74. break;
  75. }
  76. ++s;
  77. ++buf;
  78. } while (--stodo);
  79. stream->__bufpos = s;
  80. todo -= (s - stream->__bufstart);
  81. }
  82. #endif /* __STDIO_BUFFERS */
  83. __STDIO_STREAM_VALIDATE(stream);
  84. return bufsize - todo;
  85. }
  86. } while (1);
  87. }