1*17415Sralph /* @(#)wbuf.c 4.8 (Berkeley) 11/21/84 */ 22003Swnj #include <stdio.h> 38326Smckusick #include <sys/types.h> 48326Smckusick #include <sys/stat.h> 52003Swnj 62003Swnj char *malloc(); 72003Swnj 82003Swnj _flsbuf(c, iop) 9*17415Sralph unsigned char c; 102003Swnj register FILE *iop; 112003Swnj { 122003Swnj register char *base; 132003Swnj register n, rn; 142003Swnj char c1; 158326Smckusick int size; 168326Smckusick struct stat stbuf; 172003Swnj 183163Stoy if (iop->_flag & _IORW) { 193163Stoy iop->_flag |= _IOWRT; 2013493Ssam iop->_flag &= ~(_IOEOF|_IOREAD); 213163Stoy } 223163Stoy 232003Swnj if ((iop->_flag&_IOWRT)==0) 242003Swnj return(EOF); 252003Swnj tryagain: 262003Swnj if (iop->_flag&_IOLBF) { 272003Swnj base = iop->_base; 282003Swnj *iop->_ptr++ = c; 298326Smckusick if (iop->_ptr >= base+iop->_bufsiz || c == '\n') { 302003Swnj n = write(fileno(iop), base, rn = iop->_ptr - base); 312003Swnj iop->_ptr = base; 32*17415Sralph iop->_cnt = 0; 332003Swnj } else 342003Swnj rn = n = 0; 352003Swnj } else if (iop->_flag&_IONBF) { 362003Swnj c1 = c; 372003Swnj rn = 1; 382003Swnj n = write(fileno(iop), &c1, rn); 392003Swnj iop->_cnt = 0; 402003Swnj } else { 412003Swnj if ((base=iop->_base)==NULL) { 428326Smckusick if (fstat(fileno(iop), &stbuf) < 0 || 438326Smckusick stbuf.st_blksize <= NULL) 448326Smckusick size = BUFSIZ; 458326Smckusick else 468326Smckusick size = stbuf.st_blksize; 478326Smckusick if ((iop->_base=base=malloc(size)) == NULL) { 482003Swnj iop->_flag |= _IONBF; 492003Swnj goto tryagain; 502003Swnj } 512003Swnj iop->_flag |= _IOMYBUF; 528326Smckusick iop->_bufsiz = size; 5316571Sralph if (iop==stdout && isatty(fileno(stdout))) { 5416571Sralph iop->_flag |= _IOLBF; 5516571Sralph iop->_ptr = base; 5616571Sralph goto tryagain; 5716571Sralph } 582003Swnj rn = n = 0; 592003Swnj } else if ((rn = n = iop->_ptr - base) > 0) { 602003Swnj iop->_ptr = base; 612003Swnj n = write(fileno(iop), base, n); 622003Swnj } 638326Smckusick iop->_cnt = iop->_bufsiz-1; 642003Swnj *base++ = c; 652003Swnj iop->_ptr = base; 662003Swnj } 672003Swnj if (rn != n) { 682003Swnj iop->_flag |= _IOERR; 692003Swnj return(EOF); 702003Swnj } 712003Swnj return(c); 722003Swnj } 732003Swnj 742003Swnj fflush(iop) 752003Swnj register struct _iobuf *iop; 762003Swnj { 772003Swnj register char *base; 782003Swnj register n; 792003Swnj 802003Swnj if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT 812003Swnj && (base=iop->_base)!=NULL && (n=iop->_ptr-base)>0) { 822003Swnj iop->_ptr = base; 838326Smckusick iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : iop->_bufsiz; 842003Swnj if (write(fileno(iop), base, n)!=n) { 852003Swnj iop->_flag |= _IOERR; 862003Swnj return(EOF); 872003Swnj } 882003Swnj } 892003Swnj return(0); 902003Swnj } 912003Swnj 922003Swnj /* 932003Swnj * Flush buffers on exit 942003Swnj */ 952003Swnj 962003Swnj _cleanup() 972003Swnj { 982003Swnj register struct _iobuf *iop; 992003Swnj extern struct _iobuf *_lastbuf; 1002003Swnj 1012003Swnj for (iop = _iob; iop < _lastbuf; iop++) 1022003Swnj fclose(iop); 1032003Swnj } 1042003Swnj 1052003Swnj fclose(iop) 1069739Ssam register struct _iobuf *iop; 1072003Swnj { 1089721Sclemc register int r; 1092003Swnj 1102003Swnj r = EOF; 1113163Stoy if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) { 1122003Swnj r = fflush(iop); 1132003Swnj if (close(fileno(iop)) < 0) 1142003Swnj r = EOF; 1152003Swnj if (iop->_flag&_IOMYBUF) 1162003Swnj free(iop->_base); 1172003Swnj } 1182003Swnj iop->_cnt = 0; 1199721Sclemc iop->_base = (char *)NULL; 1209721Sclemc iop->_ptr = (char *)NULL; 1219721Sclemc iop->_bufsiz = 0; 1229721Sclemc iop->_flag = 0; 1239721Sclemc iop->_file = 0; 1242003Swnj return(r); 1252003Swnj } 126