2 * fillbuf.c - fill a buffer
6 #if defined(_POSIX_SOURCE)
13 ssize_t
_read(ssize_t d
, char *buf
, size_t nbytes
);
16 __fillbuf(register FILE *stream
)
18 static unsigned char ch
[FOPEN_MAX
];
22 if (fileno(stream
) < 0) return EOF
;
23 if (io_testflag(stream
, (_IOEOF
| _IOERR
))) return EOF
;
24 if (!io_testflag(stream
, _IOREAD
))
25 { stream
->_flags
|= _IOERR
; return EOF
; }
26 if (io_testflag(stream
, _IOWRITING
))
27 { stream
->_flags
|= _IOERR
; return EOF
; }
29 if (!io_testflag(stream
, _IOREADING
))
30 stream
->_flags
|= _IOREADING
;
32 if (!io_testflag(stream
, _IONBF
) && !stream
->_buf
) {
33 stream
->_buf
= (unsigned char *) malloc(BUFSIZ
);
35 stream
->_flags
|= _IONBF
;
38 stream
->_flags
|= _IOMYBUF
;
39 stream
->_bufsiz
= BUFSIZ
;
43 /* flush line-buffered output when filling an input buffer */
44 for (i
= 0; i
< FOPEN_MAX
; i
++) {
45 if (__iotab
[i
] && io_testflag(__iotab
[i
], _IOLBF
))
46 if (io_testflag(__iotab
[i
], _IOWRITING
))
47 (void) fflush(__iotab
[i
]);
51 stream
->_buf
= &ch
[fileno(stream
)];
54 stream
->_ptr
= stream
->_buf
;
55 stream
->_count
= _read(stream
->_fd
, (char *)stream
->_buf
, stream
->_bufsiz
);
57 if (stream
->_count
<= 0){
58 if (stream
->_count
== 0) {
59 stream
->_flags
|= _IOEOF
;
62 stream
->_flags
|= _IOERR
;
68 return *stream
->_ptr
++;