From c5f8f95059504748f07e0d3ca9d978669622428c Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 23 May 2026 01:28:05 -0400 Subject: initial commit Signed-off-by: Ian Moffett --- core/readbuf.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 core/readbuf.c (limited to 'core/readbuf.c') diff --git a/core/readbuf.c b/core/readbuf.c new file mode 100644 index 0000000..ee10362 --- /dev/null +++ b/core/readbuf.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2026, Chloe M. + * Provided under the BSD-3 clause + */ + +#include +#include +#include +#include +#include "cescal/readbuf.h" + +int +readbuf_init(struct readbuf *rb) +{ + if (rb == NULL) { + errno = EINVAL; + return -1; + } + + rb->size = 0; + rb->tail = 0; + return 0; +} + +char +readbuf_read(struct readbuf *rb, int fd) +{ + ssize_t nread = -1; + + if (rb == NULL || fd < 0) { + errno = EINVAL; + return '\0'; + } + +#define N_RESIDUAL(rb) ((rb)->size - (rb)->tail) + if (N_RESIDUAL(rb) == 0 || rb->size == 0) { + if ((nread = read(fd, rb->buf, READBUF_CAP)) <= 0) + return nread; + + rb->size = nread; + rb->tail = 0; + } +#undef N_RESIDUAL + + return rb->buf[rb->tail++]; +} -- cgit v1.2.3