Add a function to drop privileges.
[apps/pfixtools.git] / buffer.c
index 366b7d0..bdd5715 100644 (file)
--- 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                  */
 /*          ~~~~~~~~~                                                         */
 /*  ________________________________________________________________________  */
 /*                                                                            */
 /******************************************************************************/
 
 /*
- * Copyright © 2006 Pierre Habouzit
+ * Copyright © 2006-2007 Pierre Habouzit
  */
 
+#include <stdio.h>
+#include <unistd.h>
+
 #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;
+}