flushbuf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * flushbuf.c - flush a buffer
  3. */
  4. /* $Id$ */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <unistd.h>
  8. #include "loc_incl.h"
  9. extern void (*_clean)(void);
  10. static int
  11. do_write(int d, char *buf, int nbytes)
  12. {
  13. int c;
  14. /* POSIX actually allows write() to return a positive value less
  15. than nbytes, so loop ...
  16. */
  17. while ((c = write(d, buf, nbytes)) > 0 && c < nbytes) {
  18. nbytes -= c;
  19. buf += c;
  20. }
  21. return c > 0;
  22. }
  23. int
  24. __flushbuf(int c, FILE * stream)
  25. {
  26. _clean = __cleanup;
  27. if (fileno(stream) < 0) return EOF;
  28. if (!io_testflag(stream, _IOWRITE)) return EOF;
  29. if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
  30. stream->_flags &= ~_IOREADING;
  31. stream->_flags |= _IOWRITING;
  32. if (!io_testflag(stream, _IONBF)) {
  33. if (!stream->_buf) {
  34. if (stream == stdout && isatty(fileno(stdout))) {
  35. if (!(stream->_buf =
  36. (unsigned char *) malloc(BUFSIZ))) {
  37. stream->_flags |= _IONBF;
  38. } else {
  39. stream->_flags |= _IOLBF|_IOMYBUF;
  40. stream->_bufsiz = BUFSIZ;
  41. stream->_count = -1;
  42. }
  43. } else {
  44. if (!(stream->_buf =
  45. (unsigned char *) malloc(BUFSIZ))) {
  46. stream->_flags |= _IONBF;
  47. } else {
  48. stream->_flags |= _IOMYBUF;
  49. stream->_bufsiz = BUFSIZ;
  50. if (!io_testflag(stream, _IOLBF))
  51. stream->_count = BUFSIZ - 1;
  52. else stream->_count = -1;
  53. }
  54. }
  55. stream->_ptr = stream->_buf;
  56. }
  57. }
  58. if (io_testflag(stream, _IONBF)) {
  59. char c1 = c;
  60. stream->_count = 0;
  61. if (io_testflag(stream, _IOAPPEND)) {
  62. if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
  63. stream->_flags |= _IOERR;
  64. return EOF;
  65. }
  66. }
  67. if (write(fileno(stream), &c1, 1) != 1) {
  68. stream->_flags |= _IOERR;
  69. return EOF;
  70. }
  71. return (unsigned char) c;
  72. } else if (io_testflag(stream, _IOLBF)) {
  73. *stream->_ptr++ = c;
  74. /* stream->_count has been updated in putc macro. */
  75. if (c == '\n' || stream->_count == -stream->_bufsiz) {
  76. int count = -stream->_count;
  77. stream->_ptr = stream->_buf;
  78. stream->_count = 0;
  79. if (io_testflag(stream, _IOAPPEND)) {
  80. if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
  81. stream->_flags |= _IOERR;
  82. return EOF;
  83. }
  84. }
  85. if (! do_write(fileno(stream), (char *)stream->_buf,
  86. count)) {
  87. stream->_flags |= _IOERR;
  88. return EOF;
  89. }
  90. }
  91. } else {
  92. int count = stream->_ptr - stream->_buf;
  93. stream->_count = stream->_bufsiz - 1;
  94. stream->_ptr = stream->_buf + 1;
  95. if (count > 0) {
  96. if (io_testflag(stream, _IOAPPEND)) {
  97. if (lseek(fileno(stream), 0L, SEEK_END) == -1) {
  98. stream->_flags |= _IOERR;
  99. return EOF;
  100. }
  101. }
  102. if (! do_write(fileno(stream), (char *)stream->_buf, count)) {
  103. *(stream->_buf) = c;
  104. stream->_flags |= _IOERR;
  105. return EOF;
  106. }
  107. }
  108. *(stream->_buf) = c;
  109. }
  110. return (unsigned char) c;
  111. }