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