home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / libc / stdio / flsbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-05-05  |  1.8 KB  |  112 lines

  1. #include    <stdio.h>
  2.  
  3. char    *malloc();
  4.  
  5. int
  6. _flsbuf(c, iop)
  7.     int c;
  8.     register FILE *iop;
  9. {
  10.     register char *base;
  11.     register n, rn;
  12.     char c1;
  13.     extern char _sobuf[];
  14.  
  15.     if (iop->_flag & _IORW) {
  16.         iop->_flag |= _IOWRT;
  17.         iop->_flag &= ~_IOEOF;
  18.     }
  19.  
  20. tryagain:
  21.     if (iop->_flag & _IONBF) {
  22.         c1 = c;
  23.         rn = 1;
  24.         n = write(fileno(iop), &c1, rn);
  25.         iop->_cnt = 0;
  26.     } else {
  27.         if ((base = iop->_base) == NULL) {
  28.             if (iop == stdout) {
  29.                 if (isatty(fileno(stdout))) {
  30.                     iop->_flag |= _IONBF;
  31.                     goto tryagain;
  32.                 }
  33.                 iop->_base = _sobuf;
  34.                 iop->_ptr = _sobuf;
  35.                 goto tryagain;
  36.             }
  37.             if ((iop->_base = base = malloc(BUFSIZ)) == NULL) {
  38.                 iop->_flag |= _IONBF;
  39.                 goto tryagain;
  40.             }
  41.             iop->_flag |= _IOMYBUF;
  42.             rn = n = 0;
  43.         } else if((rn = n = iop->_ptr - base) > 0) {
  44.             iop->_ptr = base;
  45.             n = write(fileno(iop), base, n);
  46.         }
  47.         iop->_cnt = BUFSIZ - 1;
  48.         *base++ = c;
  49.         iop->_ptr = base;
  50.     }
  51.     if (rn != n) {
  52.         iop->_flag |= _IOERR;
  53.         return(EOF);
  54.     }
  55.     return(c);
  56. }
  57.  
  58. int
  59. fflush(iop)
  60.     register FILE *iop;
  61. {
  62.     register char *base;
  63.     register n;
  64.  
  65.     if ((iop->_flag & (_IONBF|_IOWRT)) == _IOWRT
  66.      && (base = iop->_base) != NULL && (n = iop->_ptr - base) > 0) {
  67.         iop->_ptr = base;
  68.         iop->_cnt = BUFSIZ;
  69.         if (write(fileno(iop), base, n) != n) {
  70.             iop->_flag |= _IOERR;
  71.             return(EOF);
  72.         }
  73.     }
  74.     return(0);
  75. }
  76.  
  77. /*
  78.  * Flush buffers on exit
  79.  */
  80.  
  81. _cleanup()
  82. {
  83.     register FILE *iop;
  84.     extern FILE *_lastbuf;
  85.  
  86.     for(iop = _iob; iop < _lastbuf; iop++)
  87.         fclose(iop);
  88. }
  89.  
  90. int
  91. fclose(iop)
  92.     register FILE *iop;
  93. {
  94.     register r;
  95.  
  96.     r = EOF;
  97.     if (iop->_flag & (_IOREAD|_IOWRT|_IORW)
  98.         && (iop->_flag & _IOSTRG) == 0) {
  99.         r = fflush(iop);
  100.         if (close(fileno(iop)) < 0)
  101.             r = EOF;
  102.         if (iop->_flag & _IOMYBUF)
  103.             free(iop->_base);
  104.         if (iop->_flag & (_IOMYBUF|_IONBF))
  105.             iop->_base = NULL;
  106.     }
  107.     iop->_flag &=
  108.         ~(_IOREAD|_IOWRT|_IONBF|_IOMYBUF|_IOERR|_IOEOF|_IOSTRG|_IORW);
  109.     iop->_cnt = 0;
  110.     return(r);
  111. }
  112.