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