home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Distributions / ucb / spencer_2bsd.tar.gz / 2bsd.tar / src / libNS / flsbuf.c < prev    next >
C/C++ Source or Header  |  1980-02-17  |  2KB  |  108 lines

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