xref: /csrg-svn/lib/libc/stdio/filbuf.c (revision 2002)
1*2002Swnj /* @(#)filbuf.c	4.1 (Berkeley) 12/21/80 */
2*2002Swnj #include	<stdio.h>
3*2002Swnj char	*malloc();
4*2002Swnj 
5*2002Swnj _filbuf(iop)
6*2002Swnj register FILE *iop;
7*2002Swnj {
8*2002Swnj 	static char smallbuf[_NFILE];
9*2002Swnj 
10*2002Swnj 	if ((iop->_flag&_IOREAD) == 0)
11*2002Swnj 		return(EOF);
12*2002Swnj 	if (iop->_flag&_IOSTRG)
13*2002Swnj 		return(EOF);
14*2002Swnj tryagain:
15*2002Swnj 	if (iop->_base==NULL) {
16*2002Swnj 		if (iop->_flag&_IONBF) {
17*2002Swnj 			iop->_base = &smallbuf[fileno(iop)];
18*2002Swnj 			goto tryagain;
19*2002Swnj 		}
20*2002Swnj 		if ((iop->_base = malloc(BUFSIZ)) == NULL) {
21*2002Swnj 			iop->_flag |= _IONBF;
22*2002Swnj 			goto tryagain;
23*2002Swnj 		}
24*2002Swnj 		iop->_flag |= _IOMYBUF;
25*2002Swnj 	}
26*2002Swnj 	iop->_ptr = iop->_base;
27*2002Swnj 	if (iop == stdin && (stdout->_flag&_IOLBF))
28*2002Swnj 		fflush(stdout);
29*2002Swnj 	iop->_cnt = read(fileno(iop), iop->_ptr, iop->_flag&_IONBF?1:BUFSIZ);
30*2002Swnj 	if (--iop->_cnt < 0) {
31*2002Swnj 		if (iop->_cnt == -1)
32*2002Swnj 			iop->_flag |= _IOEOF;
33*2002Swnj 		else
34*2002Swnj 			iop->_flag |= _IOERR;
35*2002Swnj 		iop->_cnt = 0;
36*2002Swnj 		return(-1);
37*2002Swnj 	}
38*2002Swnj 	return(*iop->_ptr++&0377);
39*2002Swnj }
40