Corrige geração do código do ano.
[cascardo/declara.git] / lib / util.c
index 90ca426..bf3352f 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
 #include <errno.h>
 #include <string.h>
 
-int set_llong(char *str, long long *val)
+#include <fcntl.h>
+#include <unistd.h>
+
+int set_llong(char *arg, long long *val)
 {
        char *end = NULL;
        errno = 0;
-       *val = strtoll(str, &end, 0);
+       *val = strtoll(arg, &end, 0);
        if (end && *end)
                return -EINVAL;
        if (errno == ERANGE)
@@ -33,13 +36,11 @@ int set_llong(char *str, long long *val)
        return 0;
 }
 
-int set_int(char **args, int argc, int *val)
+int set_int(char *arg, int *val)
 {
        char *end = NULL;
-       if (argc != 2)
-               return -EINVAL;
        errno = 0;
-       *val = strtol(args[1], &end, 0);
+       *val = strtol(arg, &end, 0);
        if (end && *end)
                return -EINVAL;
        if (errno == ERANGE)
@@ -47,12 +48,25 @@ int set_int(char **args, int argc, int *val)
        return 0;
 }
 
-int set_string(char **args, int argc, char **str)
+int set_string(char *arg, char **str)
 {
-       if (argc != 2)
-               return -EINVAL;
-       *str = strdup(args[1]);
+       *str = strdup(arg);
        if (!*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;
+}