From bd99994bdb37f34b7afb68a47d1ddffb1891ff8b Mon Sep 17 00:00:00 2001 From: Roman Pen Date: Wed, 19 Aug 2015 12:33:06 +0200 Subject: [PATCH] lib/strntol: add 'strntol' function In future patches I will need it. Signed-off-by: Roman Pen Signed-off-by: Jens Axboe --- Makefile | 2 +- lib/strntol.c | 31 +++++++++++++++++++++++++++++++ lib/strntol.h | 6 ++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 lib/strntol.c create mode 100644 lib/strntol.h diff --git a/Makefile b/Makefile index 24663a48..fe0da438 100644 --- 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 index 00000000..713f63bb --- /dev/null +++ b/lib/strntol.c @@ -0,0 +1,31 @@ +#include +#include +#include + +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 index 00000000..68f5d1b7 --- /dev/null +++ b/lib/strntol.h @@ -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 -- 2.25.1