diff --git a/linebuffer.c b/linebuffer.c index df661f8..41dfa08 100644 --- a/linebuffer.c +++ b/linebuffer.c @@ -60,6 +60,7 @@ int linebuffer_put(struct linebuffer *buf, const char *src, unsigned int size) { int len = MIN(buf->size - buf->pos, size); memcpy(buf->data + buf->pos, src, len); + buf->pos += len; return len; } @@ -108,13 +109,9 @@ int linebuffer_writefd(struct linebuffer *buf, int fd) return len; } -int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...) +int linebuffer_vprintf(struct linebuffer *buf, const char *fmt, va_list az) { - va_list az; - va_start(az, fmt); int len = vsnprintf(buf->data + buf->pos, buf->size - buf->pos, fmt, az); - va_end(az); - if (len < 0 || len >= (buf->size - buf->pos)) return -1; @@ -122,6 +119,17 @@ int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...) return len; } +int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...) +{ + va_list az; + + va_start(az, fmt); + int ret = linebuffer_vprintf(buf, fmt, az); + va_end(az); + + return ret; +} + char * linebuffer_getline(struct linebuffer *buf, int *len) { buf->newline = memchr(buf->data, '\n', buf->pos); diff --git a/linebuffer.h b/linebuffer.h index c021ad3..022db21 100644 --- a/linebuffer.h +++ b/linebuffer.h @@ -1,6 +1,8 @@ #ifndef _LINEBUFFER_H_ #define _LINEBUFFER_H_ +#include + struct linebuffer { unsigned int size; unsigned int pos; @@ -19,6 +21,7 @@ int linebuffer_parsefd(struct linebuffer *buf, int fd); int linebuffer_writefd(struct linebuffer *buf, int fd); int linebuffer_put(struct linebuffer *buf, const char *src, unsigned int size); +int linebuffer_vprintf(struct linebuffer *buf, const char *fmt, va_list ap); int linebuffer_printf(struct linebuffer *buf, const char *fmt, ...); char * linebuffer_getline(struct linebuffer *buf, int *len);