1989-05-30 13:34:25 +00:00
|
|
|
/*
|
|
|
|
* fillbuf.c - fill a buffer
|
|
|
|
*/
|
1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2007-04-21 23:18:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2019-06-15 11:07:10 +00:00
|
|
|
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE
|
2018-06-23 16:54:40 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
int __fillbuf(register FILE* stream)
|
1989-05-30 13:34:25 +00:00
|
|
|
{
|
|
|
|
static unsigned char ch[FOPEN_MAX];
|
1990-06-21 11:13:23 +00:00
|
|
|
register int i;
|
1989-05-30 13:34:25 +00:00
|
|
|
|
|
|
|
stream->_count = 0;
|
2018-06-21 20:33:47 +00:00
|
|
|
if (fileno(stream) < 0)
|
|
|
|
return EOF;
|
|
|
|
if (io_testflag(stream, _IOEOF))
|
|
|
|
return EOF;
|
|
|
|
if (!io_testflag(stream, _IOREAD))
|
|
|
|
{
|
1993-10-07 09:44:03 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
return EOF;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
if (io_testflag(stream, _IOWRITING))
|
|
|
|
{
|
1993-10-07 09:44:03 +00:00
|
|
|
stream->_flags |= _IOERR;
|
|
|
|
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;
|
2018-06-21 20:33:47 +00:00
|
|
|
|
|
|
|
if (!io_testflag(stream, _IONBF) && !stream->_buf)
|
|
|
|
{
|
|
|
|
stream->_buf = (unsigned char*)malloc(BUFSIZ);
|
|
|
|
if (!stream->_buf)
|
|
|
|
{
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_flags |= _IONBF;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
else
|
|
|
|
{
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_flags |= _IOMYBUF;
|
|
|
|
stream->_bufsiz = BUFSIZ;
|
|
|
|
}
|
|
|
|
}
|
1990-06-21 11:13:23 +00:00
|
|
|
|
|
|
|
/* flush line-buffered output when filling an input buffer */
|
2018-06-21 20:33:47 +00:00
|
|
|
for (i = 0; i < FOPEN_MAX; i++)
|
|
|
|
{
|
1990-06-21 11:13:23 +00:00
|
|
|
if (__iotab[i] && io_testflag(__iotab[i], _IOLBF))
|
|
|
|
if (io_testflag(__iotab[i], _IOWRITING))
|
2018-06-21 20:33:47 +00:00
|
|
|
(void)fflush(__iotab[i]);
|
1989-12-18 15:04:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (!stream->_buf)
|
|
|
|
{
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_buf = &ch[fileno(stream)];
|
|
|
|
stream->_bufsiz = 1;
|
|
|
|
}
|
|
|
|
stream->_ptr = stream->_buf;
|
2018-06-21 20:33:47 +00:00
|
|
|
stream->_count = read(stream->_fd, (char*)stream->_buf, stream->_bufsiz);
|
1989-05-30 13:34:25 +00:00
|
|
|
|
2018-06-21 20:33:47 +00:00
|
|
|
if (stream->_count <= 0)
|
|
|
|
{
|
|
|
|
if (stream->_count == 0)
|
|
|
|
{
|
1989-05-30 13:34:25 +00:00
|
|
|
stream->_flags |= _IOEOF;
|
|
|
|
}
|
2018-06-21 20:33:47 +00:00
|
|
|
else
|
1989-05-30 13:34:25 +00:00
|
|
|
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++;
|
|
|
|
}
|
2018-06-23 16:54:40 +00:00
|
|
|
|
|
|
|
#endif
|