restore locking protocol to agree with /bin/mail -- someday v6mail also
[unix-history] / usr / src / lib / libc / stdio / filbuf.c
CommitLineData
25d7b8e8 1/* @(#)filbuf.c 4.3 (Berkeley) %G% */
4216ca33
BJ
2#include <stdio.h>
3char *malloc();
4
5_filbuf(iop)
6register FILE *iop;
7{
8 static char smallbuf[_NFILE];
9
d8af6b8b
MT
10 if (iop->_flag & _IORW)
11 iop->_flag |= _IOREAD;
12
4216ca33
BJ
13 if ((iop->_flag&_IOREAD) == 0)
14 return(EOF);
15 if (iop->_flag&_IOSTRG)
16 return(EOF);
17tryagain:
18 if (iop->_base==NULL) {
19 if (iop->_flag&_IONBF) {
20 iop->_base = &smallbuf[fileno(iop)];
21 goto tryagain;
22 }
23 if ((iop->_base = malloc(BUFSIZ)) == NULL) {
24 iop->_flag |= _IONBF;
25 goto tryagain;
26 }
27 iop->_flag |= _IOMYBUF;
28 }
4216ca33
BJ
29 if (iop == stdin && (stdout->_flag&_IOLBF))
30 fflush(stdout);
25d7b8e8
KS
31 iop->_cnt = read(fileno(iop), iop->_base, iop->_flag&_IONBF?1:BUFSIZ);
32 iop->_ptr = iop->_base;
4216ca33 33 if (--iop->_cnt < 0) {
d8af6b8b 34 if (iop->_cnt == -1) {
4216ca33 35 iop->_flag |= _IOEOF;
d8af6b8b
MT
36 if (iop->_flag & _IORW)
37 iop->_flag &= ~_IOREAD;
38 } else
4216ca33
BJ
39 iop->_flag |= _IOERR;
40 iop->_cnt = 0;
41 return(-1);
42 }
43 return(*iop->_ptr++&0377);
44}