Commit | Line | Data |
---|---|---|
9acf07ab | 1 | /* @(#)filbuf.c 4.9 (Berkeley) %G% */ |
4216ca33 | 2 | #include <stdio.h> |
f4c06a32 KM |
3 | #include <sys/types.h> |
4 | #include <sys/stat.h> | |
4216ca33 BJ |
5 | char *malloc(); |
6 | ||
7 | _filbuf(iop) | |
8 | register FILE *iop; | |
9 | { | |
f4c06a32 KM |
10 | int size; |
11 | struct stat stbuf; | |
4216ca33 BJ |
12 | static char smallbuf[_NFILE]; |
13 | ||
d8af6b8b MT |
14 | if (iop->_flag & _IORW) |
15 | iop->_flag |= _IOREAD; | |
16 | ||
4216ca33 BJ |
17 | if ((iop->_flag&_IOREAD) == 0) |
18 | return(EOF); | |
9acf07ab | 19 | if (iop->_flag&(_IOSTRG|_IOEOF)) |
4216ca33 BJ |
20 | return(EOF); |
21 | tryagain: | |
22 | if (iop->_base==NULL) { | |
23 | if (iop->_flag&_IONBF) { | |
24 | iop->_base = &smallbuf[fileno(iop)]; | |
25 | goto tryagain; | |
26 | } | |
f4c06a32 KM |
27 | if (fstat(fileno(iop), &stbuf) < 0 || stbuf.st_blksize <= NULL) |
28 | size = BUFSIZ; | |
29 | else | |
30 | size = stbuf.st_blksize; | |
63b1c981 RC |
31 | if ((iop->_base = malloc(size)) == NULL) { |
32 | iop->_flag |= _IONBF; | |
33 | goto tryagain; | |
4216ca33 | 34 | } |
63b1c981 | 35 | iop->_flag |= _IOMYBUF; |
f4c06a32 | 36 | iop->_bufsiz = size; |
4216ca33 | 37 | } |
4e0517a3 KM |
38 | if (iop == stdin) { |
39 | if (stdout->_flag&_IOLBF) | |
40 | fflush(stdout); | |
41 | if (stderr->_flag&_IOLBF) | |
42 | fflush(stderr); | |
43 | } | |
f4c06a32 KM |
44 | iop->_cnt = read(fileno(iop), iop->_base, |
45 | iop->_flag & _IONBF ? 1 : iop->_bufsiz); | |
25d7b8e8 | 46 | iop->_ptr = iop->_base; |
4216ca33 | 47 | if (--iop->_cnt < 0) { |
d8af6b8b | 48 | if (iop->_cnt == -1) { |
4216ca33 | 49 | iop->_flag |= _IOEOF; |
d8af6b8b MT |
50 | if (iop->_flag & _IORW) |
51 | iop->_flag &= ~_IOREAD; | |
52 | } else | |
4216ca33 BJ |
53 | iop->_flag |= _IOERR; |
54 | iop->_cnt = 0; | |
55 | return(-1); | |
56 | } | |
57 | return(*iop->_ptr++&0377); | |
58 | } |