source: trunk/minix/lib/stdio/fillbuf.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 1.6 KB
Line 
1/*
2 * fillbuf.c - fill a buffer
3 */
4/* $Header: /cvsup/minix/src/lib/stdio/fillbuf.c,v 1.1.1.1 2005/04/21 14:56:35 beng Exp $ */
5
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"
12
13ssize_t _read(ssize_t d, char *buf, size_t nbytes);
14
15int
16__fillbuf(register FILE *stream)
17{
18 static unsigned char ch[FOPEN_MAX];
19 register int i;
20
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; }
28
29 if (!io_testflag(stream, _IOREADING))
30 stream->_flags |= _IOREADING;
31
32 if (!io_testflag(stream, _IONBF) && !stream->_buf) {
33 stream->_buf = (unsigned char *) malloc(BUFSIZ);
34 if (!stream->_buf) {
35 stream->_flags |= _IONBF;
36 }
37 else {
38 stream->_flags |= _IOMYBUF;
39 stream->_bufsiz = BUFSIZ;
40 }
41 }
42
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]);
48 }
49
50 if (!stream->_buf) {
51 stream->_buf = &ch[fileno(stream)];
52 stream->_bufsiz = 1;
53 }
54 stream->_ptr = stream->_buf;
55 stream->_count = _read(stream->_fd, (char *)stream->_buf, stream->_bufsiz);
56
57 if (stream->_count <= 0){
58 if (stream->_count == 0) {
59 stream->_flags |= _IOEOF;
60 }
61 else
62 stream->_flags |= _IOERR;
63
64 return EOF;
65 }
66 stream->_count--;
67
68 return *stream->_ptr++;
69}
Note: See TracBrowser for help on using the repository browser.