flushbuf.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * flushbuf.c - flush a buffer
  3. */
  4. /* $Header$ */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include "loc_incl.h"
  8. #include <sys/types.h>
  9. off_t lseek(int fildes, off_t offset, int whence);
  10. int write(int d, const char *buf, int nbytes);
  11. int isatty(int d);
  12. extern int (*_fflush)(FILE *stream);
  13. int
  14. __flushbuf(int c, FILE * stream)
  15. {
  16. _fflush = fflush;
  17. if (fileno(stream) < 0) return EOF;
  18. if (!io_testflag(stream, _IOWRITE)) return EOF;
  19. if (io_testflag(stream, _IOREADING) && !feof(stream)) return EOF;
  20. stream->_flags &= ~_IOREADING;
  21. stream->_flags |= _IOWRITING;
  22. if (!io_testflag(stream, _IONBF)) {
  23. if (!stream->_buf) {
  24. if (stream == stdout && isatty(fileno(stdout))) {
  25. if (!(stream->_buf =
  26. (unsigned char *) malloc(BUFSIZ))) {
  27. stream->_flags |= _IONBF;
  28. } else {
  29. stream->_flags |= _IOLBF;
  30. stream->_bufsiz = BUFSIZ;
  31. stream->_count = -1;
  32. }
  33. } else {
  34. if (!(stream->_buf =
  35. (unsigned char *) malloc(BUFSIZ))) {
  36. stream->_flags |= _IONBF;
  37. } else {
  38. stream->_flags |= _IOMYBUF;
  39. stream->_bufsiz = BUFSIZ;
  40. if (!io_testflag(stream, _IOLBF))
  41. stream->_count = BUFSIZ - 1;
  42. }
  43. }
  44. stream->_ptr = stream->_buf;
  45. }
  46. }
  47. if (io_testflag(stream, _IONBF)) {
  48. char c1 = c;
  49. stream->_count = 0;
  50. if (io_testflag(stream, _IOAPPEND)) {
  51. if (lseek(fileno(stream), 0L, 2) == -1) {
  52. stream->_flags |= _IOERR;
  53. return EOF;
  54. }
  55. }
  56. if (write(fileno(stream), &c1, 1) != 1) {
  57. stream->_flags |= _IOERR;
  58. return EOF;
  59. }
  60. return c;
  61. } else if (io_testflag(stream, _IOLBF)) {
  62. *stream->_ptr++ = c;
  63. if (c == '\n' || stream->_count == -stream->_bufsiz) {
  64. if (io_testflag(stream, _IOAPPEND)) {
  65. if (lseek(fileno(stream), 0L, 2) == -1) {
  66. stream->_flags |= _IOERR;
  67. return EOF;
  68. }
  69. }
  70. if (write(fileno(stream), (char *)stream->_buf,
  71. -stream->_count) != -stream->_count) {
  72. stream->_flags |= _IOERR;
  73. return EOF;
  74. } else {
  75. stream->_ptr = stream->_buf;
  76. stream->_count = 0;
  77. }
  78. }
  79. } else {
  80. int count = stream->_ptr - stream->_buf;
  81. stream->_count = stream->_bufsiz - 1;
  82. stream->_ptr = stream->_buf + 1;
  83. if (count > 0) {
  84. if (io_testflag(stream, _IOAPPEND)) {
  85. if (lseek(fileno(stream), 0L, 2) == -1) {
  86. stream->_flags |= _IOERR;
  87. return EOF;
  88. }
  89. }
  90. if (write(fileno(stream), (char *)stream->_buf, count)
  91. != count) {
  92. *(stream->_buf) = c;
  93. stream->_flags |= _IOERR;
  94. return EOF;
  95. }
  96. }
  97. *(stream->_buf) = c;
  98. }
  99. return c;
  100. }