ack/lang/cem/libcc.ansi/stdio/fillbuf.c

60 lines
1.2 KiB
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* fillbuf.c - fill a buffer
*/
/* $Header$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
int read(int d, char *buf, int nbytes);
int
_fillbuf(register FILE *stream)
{
static unsigned char ch[FOPEN_MAX];
stream->_count = 0;
if (fileno(stream) < 0) return EOF;
1989-06-26 10:37:05 +00:00
if (io_testflag(stream, (_IOEOF | _IOERR ))) return EOF;
if (!io_testflag(stream, _IOREAD)) return EOF;
if (io_testflag(stream, _IOWRITING)) return EOF;
1989-05-30 13:34:25 +00:00
1989-06-26 10:37:05 +00:00
if (!io_testflag(stream, _IOREADING))
stream->_flags |= _IOREADING;
1989-05-30 13:34:25 +00:00
if (!io_testflag(stream, _IONBF) && !stream->_buf) {
stream->_buf = (unsigned char *) malloc(BUFSIZ);
if (!stream->_buf) {
stream->_flags |= _IONBF;
}
else {
stream->_flags |= _IOMYBUF;
stream->_bufsiz = BUFSIZ;
}
}
if (!stream->_buf) {
stream->_buf = &ch[fileno(stream)];
stream->_bufsiz = 1;
}
stream->_ptr = stream->_buf;
stream->_count = read(stream->_fd, (char *)stream->_buf, stream->_bufsiz);
1989-06-26 10:37:05 +00:00
/*
fprintf(stderr,"read %d bytes, \"%.*s\"\n"
, stream->_count, stream->_count, stream->_buf);
*/
1989-05-30 13:34:25 +00:00
if (stream->_count <= 0){
if (stream->_count == 0) {
stream->_flags |= _IOEOF;
}
else
stream->_flags |= _IOERR;
1989-06-26 10:37:05 +00:00
return EOF;
1989-05-30 13:34:25 +00:00
}
stream->_count--;
return *stream->_ptr++;
}