flushbuf.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* $Id$ */
  2. #include <stdio.h>
  3. int
  4. _flushbuf(c, iop)
  5. register FILE *iop;
  6. {
  7. if (fileno(iop) < 0) return EOF;
  8. if (! io_testflag(iop, IO_UNBUFF)) {
  9. if (iop->_buf == 0) {
  10. if (iop == stdout && isatty(fileno(stdout))) {
  11. iop->_flags |= IO_UNBUFF;
  12. }
  13. else {
  14. extern char *malloc();
  15. if (!(iop->_buf = (unsigned char *) malloc(_BUFSIZ))) {
  16. iop->_flags |= IO_UNBUFF;
  17. }
  18. else {
  19. iop->_flags |= IO_MYBUF;
  20. iop->_bufsiz = _BUFSIZ;
  21. iop->_count = _BUFSIZ-1;
  22. }
  23. }
  24. iop->_ptr = iop->_buf;
  25. }
  26. }
  27. if (io_testflag(iop, IO_UNBUFF)) {
  28. char c1 = c;
  29. iop->_count = 0;
  30. if (write(fileno(iop), &c1, 1) != 1) {
  31. iop->_flags |= IO_ERR;
  32. return EOF;
  33. }
  34. return c;
  35. }
  36. else {
  37. int count = iop->_ptr - iop->_buf;
  38. iop->_count = iop->_bufsiz - 1;
  39. iop->_ptr = iop->_buf + 1;
  40. if (count > 0) {
  41. if (write(fileno(iop), iop->_buf, count) != count) {
  42. *(iop->_buf) = c;
  43. iop->_flags |= IO_ERR;
  44. return EOF;
  45. }
  46. }
  47. *(iop->_buf) = c;
  48. }
  49. return c;
  50. }
  51. _cleanup()
  52. {
  53. register int i;
  54. for ( i = 0 ; i < _NFILES ; i++ )
  55. if ( _io_table[i] != NULL )
  56. fclose(_io_table[i]);
  57. }