dumpfile: joga o conteúdo de um arquivo em um fd
[cascardo/declara.git] / lib / util.c
index 90ca426..eb2ee8e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (C) 2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *  Copyright (C) 2015-2016  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -21,6 +21,9 @@
 #include <errno.h>
 #include <string.h>
 
+#include <fcntl.h>
+#include <unistd.h>
+
 int set_llong(char *str, long long *val)
 {
        char *end = NULL;
@@ -56,3 +59,18 @@ int set_string(char **args, int argc, char **str)
                return -errno;
        return 0;
 }
+
+int dumpfile(int fd, char *filename)
+{
+       int inp;
+       char buffer[256];
+       int r;
+       inp = open(filename, O_RDONLY);
+       if (inp < 0)
+               return -errno;
+       while ((r = read(inp, buffer, sizeof(buffer))) > 0) {
+               write(fd, buffer, r);
+       }
+       close(inp);
+       return r;
+}