Move macros de comandos para definir inteiros e strings.
[cascardo/declara.git] / util.c
diff --git a/util.c b/util.c
index 973da1e..90ca426 100644 (file)
--- a/util.c
+++ b/util.c
@@ -19,6 +19,7 @@
 #include "util.h"
 #include <stdlib.h>
 #include <errno.h>
+#include <string.h>
 
 int set_llong(char *str, long long *val)
 {
@@ -31,3 +32,27 @@ int set_llong(char *str, long long *val)
                return -ERANGE;
        return 0;
 }
+
+int set_int(char **args, int argc, int *val)
+{
+       char *end = NULL;
+       if (argc != 2)
+               return -EINVAL;
+       errno = 0;
+       *val = strtol(args[1], &end, 0);
+       if (end && *end)
+               return -EINVAL;
+       if (errno == ERANGE)
+               return -ERANGE;
+       return 0;
+}
+
+int set_string(char **args, int argc, char **str)
+{
+       if (argc != 2)
+               return -EINVAL;
+       *str = strdup(args[1]);
+       if (!*str)
+               return -errno;
+       return 0;
+}