lib/strntol: add 'strntol' function
authorRoman Pen <r.peniaev@gmail.com>
Wed, 19 Aug 2015 10:33:06 +0000 (12:33 +0200)
committerJens Axboe <axboe@fb.com>
Fri, 4 Sep 2015 19:32:52 +0000 (13:32 -0600)
In future patches I will need it.

Signed-off-by: Roman Pen <r.peniaev@gmail.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
Makefile
lib/strntol.c [new file with mode: 0644]
lib/strntol.h [new file with mode: 0644]

index 24663a4851f9aeb0cc79b23f824f2c22fce019f2..fe0da438883f0258596ad6a239acf27f344bda46 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -39,7 +39,7 @@ endif
 SOURCE :=      gettime.c ioengines.c init.c stat.c log.c time.c filesetup.c \
                eta.c verify.c memory.c io_u.c parse.c mutex.c options.c \
                lib/rbtree.c smalloc.c filehash.c profile.c debug.c lib/rand.c \
-               lib/num2str.c lib/ieee754.c engines/cpu.c \
+               lib/num2str.c lib/ieee754.c lib/strntol.c engines/cpu.c \
                engines/mmap.c engines/sync.c engines/null.c engines/net.c \
                memalign.c server.c client.c iolog.c backend.c libfio.c flow.c \
                cconv.c lib/prio_tree.c json.c lib/zipf.c lib/axmap.c \
diff --git a/lib/strntol.c b/lib/strntol.c
new file mode 100644 (file)
index 0000000..713f63b
--- /dev/null
@@ -0,0 +1,31 @@
+#include <string.h>
+#include <stdlib.h>
+#include <limits.h>
+
+long strntol(const char *str, size_t sz, char **end, int base)
+{
+       /* Expect that digit representation of LONG_MAX/MIN
+        * not greater than this buffer */
+       char buf[24];
+       long ret;
+       const char *beg = str;
+
+       /* Catch up leading spaces */
+       for (; beg && sz && *beg == ' '; beg++, sz--)
+               ;
+
+       if (!sz || sz >= sizeof(buf)) {
+               if (end)
+                       *end = (char *)str;
+               return 0;
+       }
+
+       memcpy(buf, beg, sz);
+       buf[sz] = '\0';
+       ret = strtol(buf, end, base);
+       if (ret == LONG_MIN || ret == LONG_MAX)
+               return ret;
+       if (end)
+               *end = (char *)str + (*end - buf);
+       return ret;
+}
diff --git a/lib/strntol.h b/lib/strntol.h
new file mode 100644 (file)
index 0000000..68f5d1b
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef FIO_STRNTOL_H
+#define FIO_STRNTOL_H
+
+long strntol(const char *str, size_t sz, char **end, int base);
+
+#endif