[hackers] [sbase] Add parseoffset() || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 30 Sep 2015 20:44:17 +0200 (CEST)

commit 007df69fc5987f30ef9494ccbbb87aa058d6b732
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed Sep 30 01:19:01 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Wed Sep 30 19:44:10 2015 +0100

    Add parseoffset()
    
    This is a utility function to allow easy parsing of file or other
    offsets, automatically taking in regard suffixes, proper bases and
    so on, for instance used in split(1) -b or od -j, -N(1).
    Of course, POSIX is very arbitrary when it comes to defining the
    parsing rules for different tools.
    The main focus here lies on being as flexible and consistent as
    possible. One central utility-function handling the parsing makes
    this stuff a lot more trivial.

diff --git a/Makefile b/Makefile
index 9f60a13..9d1293a 100644
--- a/Makefile
+++ b/Makefile
_AT_@ -56,6 +56,7 @@ LIBUTILSRC =\
         libutil/md5.c\
         libutil/mkdirp.c\
         libutil/mode.c\
+ libutil/parseoffset.c\
         libutil/putword.c\
         libutil/reallocarray.c\
         libutil/recurse.c\
diff --git a/libutil/parseoffset.c b/libutil/parseoffset.c
new file mode 100644
index 0000000..451d913
--- /dev/null
+++ b/libutil/parseoffset.c
_AT_@ -0,0 +1,59 @@
+/* See LICENSE file for copyright and license details. */
+#include <ctype.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../util.h"
+
+off_t
+parseoffset(const char *str)
+{
+ off_t res;
+ size_t scale;
+ int base = 10;
+ char *end;
+
+ /* bases */
+ if (!strncasecmp(str, "0x", strlen("0x"))) {
+ base = 16;
+ } else if (*str == '0') {
+ str++;
+ base = 8;
+ }
+
+ res = strtol(str, &end, base);
+ if (res < 0) {
+ weprintf("invalid file offset: %s\n", str);
+ return -1;
+ }
+
+ /* suffix */
+ if (*end) {
+ switch (toupper((int)*end)) {
+ case 'B':
+ scale = 512L;
+ break;
+ case 'K':
+ scale = 1024L;
+ break;
+ case 'M':
+ scale = 1024L * 1024L;
+ break;
+ case 'G':
+ scale = 1024L * 1024L * 1024L;
+ break;
+ default:
+ weprintf("invalid file offset suffix: %s\n", str);
+ return -1;
+ }
+ }
+
+ /* prevent overflow */
+ if (res > (SIZE_MAX / scale)) {
+ weprintf("file offset out of range: %s\n", str);
+ return -1;
+ }
+
+ return res;
+}
diff --git a/util.h b/util.h
index b0bf0f4..4c973d1 100644
--- a/util.h
+++ b/util.h
_AT_@ -68,6 +68,7 @@ void fnck(const char *, const char *, int (*)(const char *, const char *, int),
 mode_t getumask(void);
 char *humansize(off_t);
 mode_t parsemode(const char *, mode_t, mode_t);
+off_t parseoffset(const char *);
 void putword(FILE *, const char *);
 #undef strtonum
 long long strtonum(const char *, long long, long long, const char **);
Received on Wed Sep 30 2015 - 20:44:17 CEST

This archive was generated by hypermail 2.3.0 : Wed Sep 30 2015 - 20:48:29 CEST