X-Git-Url: http://git.madism.org/?a=blobdiff_plain;f=buffer.c;h=bdd5715d114a49e5abe2fab00f510eb949594698;hb=e79fdf45f5b9b14a452c9fe067e827d6a7d5e87d;hp=f6091fbaec855a3994b5f9e3e665b2a654819e05;hpb=9a4efa4f0dc893f243ee69d1b20f024666ca943d;p=apps%2Fpfixtools.git diff --git a/buffer.c b/buffer.c index f6091fb..bdd5715 100644 --- a/buffer.c +++ b/buffer.c @@ -1,5 +1,5 @@ /******************************************************************************/ -/* postlicyd: a postfix policy daemon with a lot of features */ +/* pfixtools: a collection of postfix related tools */ /* ~~~~~~~~~ */ /* ________________________________________________________________________ */ /* */ @@ -30,9 +30,12 @@ /******************************************************************************/ /* - * Copyright © 2006 Pierre Habouzit + * Copyright © 2006-2007 Pierre Habouzit */ +#include +#include + #include "buffer.h" #define BUFSIZ_INCREMENT 256 @@ -46,4 +49,34 @@ void buffer_resize(buffer_t *buf, ssize_t newsize) } } +void buffer_consume(buffer_t *buf, ssize_t len) { + if (len <= 0) + return; + + if (len >= buf->len) { + buffer_reset(buf); + return; + } + + 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; +}