summaryrefslogtreecommitdiffstats
path: root/tools/util.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-03 17:01:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-03 17:01:24 +0000
commit6dd3dfb79125cd02d02efbce435a6c82e5af92ef (patch)
tree45084fc83278586f6bbafcb935f92d53f71a6b03 /tools/util.c
parentInitial commit. (diff)
downloadcorosync-6dd3dfb79125cd02d02efbce435a6c82e5af92ef.tar.xz
corosync-6dd3dfb79125cd02d02efbce435a6c82e5af92ef.zip
Adding upstream version 3.1.8.upstream/3.1.8upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/util.c')
-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);
+}