rb_read.c 570 Bytes
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

#include "ringbuffer.h"

ssize_t
rbRead(Ringbuffer this, int fd)
{
	ssize_t rrsize = 0;
	size_t  rsize  = this->bsize - this->bused;

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

	rrsize = read(fd, this->buffer + this->bend, rsize);

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

		case -1:
			break;

		default:
			this->bend  += rrsize;
			this->bused += rrsize;

			if (this->bend >= this->bsize) {
				this->bend -= this->bsize;
			}

			break;
	}

	return rrsize;
}

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