aboutsummaryrefslogtreecommitdiff
path: root/getul_n.c
diff options
context:
space:
mode:
Diffstat (limited to 'getul_n.c')
-rw-r--r--getul_n.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/getul_n.c b/getul_n.c
new file mode 100644
index 0000000..cf150c9
--- /dev/null
+++ b/getul_n.c
@@ -0,0 +1,33 @@
+#define _POSIX_C_SOURCE 200809L
+
+#include "utils.h"
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+int getul_n(const char *const s, unsigned long *const out)
+{
+ char *end;
+ unsigned long v;
+
+ errno = 0;
+ v = strtoull(s, &end, 10);
+
+ if (errno)
+ {
+ fprintf(stderr, "%s: strtoul(3) %s: %s\n", __func__, s,
+ strerror(errno));
+ return -1;
+ }
+ else if (*end || v > LONG_MAX)
+ {
+ fprintf(stderr, "%s: invalid number: %.*s\n", __func__,
+ (int)(end - s), s);
+ return -1;
+ }
+
+ *out = v;
+ return 0;
+}