about summary refs log tree commit homepage
path: root/compat-util.h
diff options
context:
space:
mode:
Diffstat (limited to 'compat-util.h')
-rw-r--r--compat-util.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/compat-util.h b/compat-util.h
index 60c4081..8b70b52 100644
--- a/compat-util.h
+++ b/compat-util.h
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <errno.h>
 #include <assert.h>
+#include <stdint.h>
 
 #ifndef O_NOATIME
 #  define O_NOATIME 0
@@ -37,4 +38,21 @@ static inline size_t page_size(void)
 
 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
 
+/*
+ * converts a C string to a non-negative off_t value, taking base into
+ * account.  On error, it'll return a negative value and set errno
+ * to EINVAL
+ */
+static off_t cstr_to_off_t(const char *nptr, char **endptr, int base)
+{
+        if (sizeof(long) == 8 || (sizeof(long) == 4 && sizeof(off_t) == 4))
+                return (off_t)strtol(nptr, endptr, base);
+        else if (sizeof(off_t) == 8 && sizeof(long) == 4)
+                return (off_t)strtoll(nptr, endptr, base);
+
+        fprintf(stderr, "unrecognized sizes:\n\toff_t: %u\n\tlong: %u\n",
+                        sizeof(off_t), sizeof(long));
+        exit(1);
+}
+
 #endif /* OS_COMPAT_H */