From: Pierre Habouzit Date: Sat, 3 Feb 2007 00:30:17 +0000 (+0100) Subject: add buffer_read. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=b5fcb83a2555a4aac4d298e7ed5487b3416ef6f6;p=apps%2Fpfixtools.git add buffer_read. Signed-off-by: Pierre Habouzit --- diff --git a/buffer.c b/buffer.c index 366b7d0..8342162 100644 --- a/buffer.c +++ b/buffer.c @@ -33,6 +33,9 @@ * Copyright © 2006 Pierre Habouzit */ +#include +#include + #include "buffer.h" #define BUFSIZ_INCREMENT 256 @@ -58,3 +61,22 @@ void buffer_consume(buffer_t *buf, ssize_t len) { memmove(buf->data, buf->data + len, buf->len + 1 - len); buf->len -= len; } + +ssize_t buffer_read(buffer_t *buf, int fd, ssize_t count) +{ + ssize_t res; + + if (count < 0) + count = BUFSIZ; + + buffer_ensure(buf, count); + + res = read(fd, buf->data + buf->len, count); + if (res < 0) { + buf->data[buf->len] = '\0'; + return res; + } + + buffer_extend(buf, res); + return res; +} diff --git a/buffer.h b/buffer.h index 6c546d9..d0e1c57 100644 --- a/buffer.h +++ b/buffer.h @@ -102,4 +102,6 @@ static inline void buffer_addch(buffer_t *buf, int c) { void buffer_consume(buffer_t *buf, ssize_t len); +ssize_t buffer_read(buffer_t *buf, int fd, ssize_t count); + #endif /* POSTLICYD_BUFFER_H */