2 * fillbuf.c - fill a buffer
4 /* $Id: fillbuf.c,v 1.7 1994/06/24 11:49:15 ceriel Exp $ */
10 int _read(int d, char *buf, int nbytes);
13 __fillbuf(register FILE *stream)
15 static unsigned char ch[FOPEN_MAX];
19 if (fileno(stream) < 0) return EOF;
20 if (io_testflag(stream, (_IOEOF | _IOERR ))) return EOF;
21 if (!io_testflag(stream, _IOREAD)) {
22 stream->_flags |= _IOERR;
25 if (io_testflag(stream, _IOWRITING)) {
26 stream->_flags |= _IOERR;
30 if (!io_testflag(stream, _IOREADING))
31 stream->_flags |= _IOREADING;
33 if (!io_testflag(stream, _IONBF) && !stream->_buf) {
34 stream->_buf = (unsigned char *) malloc(BUFSIZ);
36 stream->_flags |= _IONBF;
39 stream->_flags |= _IOMYBUF;
40 stream->_bufsiz = BUFSIZ;
44 /* flush line-buffered output when filling an input buffer */
45 for (i = 0; i < FOPEN_MAX; i++) {
46 if (__iotab[i] && io_testflag(__iotab[i], _IOLBF))
47 if (io_testflag(__iotab[i], _IOWRITING))
48 (void) fflush(__iotab[i]);
52 stream->_buf = &ch[fileno(stream)];
55 stream->_ptr = stream->_buf;
56 stream->_count = _read(stream->_fd, (char *)stream->_buf, stream->_bufsiz);
58 if (stream->_count <= 0){
59 if (stream->_count == 0) {
60 stream->_flags |= _IOEOF;
63 stream->_flags |= _IOERR;
69 return *stream->_ptr++;