diff options
Diffstat (limited to 'core/readbuf.c')
| -rw-r--r-- | core/readbuf.c | 46 |
1 files changed, 46 insertions, 0 deletions
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 <stdint.h> +#include <stddef.h> +#include <errno.h> +#include <unistd.h> +#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++]; +} |
