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