aboutsummaryrefslogtreecommitdiffstats
path: root/lib/util.c
diff options
context:
space:
mode:
authorJoanne Koong <joannelkoong@gmail.com>2024-10-07 13:37:20 -0700
committerBernd Schubert <bernd.schubert@fastmail.fm>2024-10-11 13:15:38 +0200
commit535808c4d93e4637577aa17bf8413a41920dd2d8 (patch)
tree823ce1f443beeb98ec5a1ae997375b8d70161014 /lib/util.c
parent55eb214db9fa6c16f0af6a4e1a70b56b959aee3e (diff)
downloadlibfuse-535808c4d93e4637577aa17bf8413a41920dd2d8.tar.gz
Add libfuse util strtol wrapper
Add a wrapper around strtol for more rigorous error checking and convert uses of atoi and strtol to use this instead.
Diffstat (limited to 'lib/util.c')
-rw-r--r--lib/util.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/lib/util.c b/lib/util.c
new file mode 100644
index 0000000..a529d38
--- /dev/null
+++ b/lib/util.c
@@ -0,0 +1,27 @@
+#include <stdlib.h>
+#include <errno.h>
+
+#include "util.h"
+
+int libfuse_strtol(const char *str, long *res)
+{
+ char *endptr;
+ int base = 10;
+ long val;
+
+ errno = 0;
+
+ if (!str)
+ return -EINVAL;
+
+ val = strtol(str, &endptr, base);
+
+ if (errno)
+ return -errno;
+
+ if (endptr == str || *endptr != '\0')
+ return -EINVAL;
+
+ *res = val;
+ return 0;
+}