date and time created 80/12/21 16:50:12 by wnj
[unix-history] / usr / src / lib / libc / stdio / filbuf.c
CommitLineData
4216ca33
BJ
1/* @(#)filbuf.c 4.1 (Berkeley) %G% */
2#include <stdio.h>
3char *malloc();
4
5_filbuf(iop)
6register 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);
14tryagain:
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}