xref: /csrg-svn/lib/libc/stdio/wbuf.c (revision 17951)
1*17951Sserge /* @(#)wbuf.c	4.9 (Berkeley) 02/13/85 */
22003Swnj #include	<stdio.h>
38326Smckusick #include	<sys/types.h>
48326Smckusick #include	<sys/stat.h>
52003Swnj 
62003Swnj char	*malloc();
72003Swnj 
82003Swnj _flsbuf(c, iop)
917415Sralph 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;
3217415Sralph 			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)
75*17951Sserge register FILE *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 fclose(iop)
93*17951Sserge 	register FILE *iop;
942003Swnj {
959721Sclemc 	register int r;
962003Swnj 
972003Swnj 	r = EOF;
983163Stoy 	if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) {
992003Swnj 		r = fflush(iop);
1002003Swnj 		if (close(fileno(iop)) < 0)
1012003Swnj 			r = EOF;
1022003Swnj 		if (iop->_flag&_IOMYBUF)
1032003Swnj 			free(iop->_base);
1042003Swnj 	}
1052003Swnj 	iop->_cnt = 0;
1069721Sclemc 	iop->_base = (char *)NULL;
1079721Sclemc 	iop->_ptr = (char *)NULL;
1089721Sclemc 	iop->_bufsiz = 0;
1099721Sclemc 	iop->_flag = 0;
1109721Sclemc 	iop->_file = 0;
1112003Swnj 	return(r);
1122003Swnj }
113