secondary cache feature in vm.
[minix.git] / lib / libc / stdio / fillbuf.c
blob148f4b9ecf086111804770673bedd74821ff0ac8
1 /*
2 * fillbuf.c - fill a buffer
3 */
4 /* $Header$ */
6 #if defined(_POSIX_SOURCE)
7 #include <sys/types.h>
8 #endif
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "loc_incl.h"
13 ssize_t _read(ssize_t d, char *buf, size_t nbytes);
15 int
16 __fillbuf(register FILE *stream)
18 static unsigned char ch[FOPEN_MAX];
19 register int i;
21 stream->_count = 0;
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);
34 if (!stream->_buf) {
35 stream->_flags |= _IONBF;
37 else {
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]);
50 if (!stream->_buf) {
51 stream->_buf = &ch[fileno(stream)];
52 stream->_bufsiz = 1;
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;
61 else
62 stream->_flags |= _IOERR;
64 return EOF;
66 stream->_count--;
68 return *stream->_ptr++;