read.c 471 Bytes
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#include "cbuf.h"


ssize_t
cbufRead(Cbuf this, int fd)
{
	ssize_t rrsize = 0;
	size_t  rsize  = cbufGetFree(this);

	if (0 == rsize) {
		errno = ECBUFOVFL;
		return -1;
	}

	rrsize = read(fd, cbufGetWrite(this), rsize);

	switch (rrsize) {
		case  0:
			rrsize = -2;
			// DROP THROUGH

		case -1:
			break;

		default:
			cbufIncWrite(this, rrsize);
			break;
	}

	return rrsize;
}

// vim: set ts=4 sw=4: