1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <errno.h>
#include <unistd.h>
#include "ibuf.h"
static const char errmsg[] = "ibuf_refill called with non-empty buffer!\n";
/** (Re)fill the buffer from the file descriptor.
\note This function may only be called when the buffer is completely empty.
*/
int ibuf_refill(ibuf* in)
{
iobuf* io;
unsigned oldlen;
unsigned rd;
io = &(in->io);
if (iobuf_bad(io)) return 0;
if (io->bufstart != 0) {
if (io->bufstart < io->buflen) {
write(1, errmsg, sizeof errmsg);
_exit(1);
/* io->buflen -= io->bufstart; */
/* memcpy(io->buffer, io->buffer+io->bufstart, io->buflen); */
}
else
io->buflen = 0;
io->bufstart = 0;
}
oldlen = io->buflen;
if(io->buflen < io->bufsize) {
if (io->timeout && !iobuf_timeout(io, 0)) return 0;
rd = in->readfn(io->fd, io->buffer+io->buflen, io->bufsize-io->buflen);
if(rd == (unsigned)-1)
IOBUF_SET_ERROR(io);
else if(rd == 0)
io->flags |= IOBUF_EOF;
else {
io->buflen += rd;
io->offset += rd;
}
}
return io->buflen > oldlen;
}
|