summaryrefslogtreecommitdiffstats
path: root/tools/util.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--tools/util.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/tools/util.c b/tools/util.c
new file mode 100644
index 0000000..f2ef5fd
--- /dev/null
+++ b/tools/util.c
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <errno.h>
+
+#include "util.h"
+
+/*
+ * Safer wrapper of strtoll. Return 0 on success, otherwise -1.
+ * Idea from corosync-qdevice project
+ */
+int
+util_strtonum(const char *str, long long int min_val, long long int max_val,
+ long long int *res)
+{
+ long long int tmp_ll;
+ char *ep;
+
+ if (min_val > max_val) {
+ return (-1);
+ }
+
+ errno = 0;
+
+ tmp_ll = strtoll(str, &ep, 10);
+ if (ep == str || *ep != '\0' || errno != 0) {
+ return (-1);
+ }
+
+ if (tmp_ll < min_val || tmp_ll > max_val) {
+ return (-1);
+ }
+
+ *res = tmp_ll;
+
+ return (0);
+}