From 6ea2b1aa53ec29d3cf9adc7c981bfa4a4c33f29b Mon Sep 17 00:00:00 2001 From: sin Date: Wed, 4 Jun 2014 13:07:34 +0100 Subject: [PATCH] Use estrtoul() in dd(1) Allow specifications in hex and octal as well. --- Makefile | 1 + dd.c | 8 ++++---- util.h | 3 +++ util/estrtoul.c | 26 ++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 util/estrtoul.c diff --git a/Makefile b/Makefile index 04fe64d..a68d3ce 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,7 @@ LIB = \ util/ealloc.o \ util/eprintf.o \ util/estrtol.o \ + util/estrtoul.o \ util/explicit_bzero.o \ util/proc.o \ util/putword.o \ diff --git a/dd.c b/dd.c index 9942253..2685d54 100644 --- a/dd.c +++ b/dd.c @@ -237,15 +237,15 @@ main(int argc, char *argv[]) else if (sscanf(argv[i], "of=%1023c", buf) == 1) config.out = strdup(buf); else if (sscanf(argv[i], "skip=%1023c", buf) == 1) - config.skip = strtoul(buf, NULL, 10); + config.skip = estrtoul(buf, 0); else if (sscanf(argv[i], "seek=%1023c", buf) == 1) - config.seek = strtoul(buf, NULL, 10); + config.seek = estrtoul(buf, 0); else if (sscanf(argv[i], "count=%1023c", buf) == 1) - config.count = strtoul(buf, NULL, 10); + config.count = estrtoul(buf, 0); else if (strcmp(argv[i], "direct") == 0) config.direct = 1; else if (sscanf(argv[i], "bs=%1023c", buf) == 1) - config.bs = strtoul(buf, NULL, 10); + config.bs = estrtoul(buf, 0); else if (strcmp(argv[i], "bs") == 0) config.bs = 0; else if (strcmp(argv[i], "quiet") == 0) diff --git a/util.h b/util.h index 0ed7f6b..f43c6ee 100644 --- a/util.h +++ b/util.h @@ -27,6 +27,9 @@ char *estrdup(const char *); /* estrtol.c */ long estrtol(const char *, int); +/* estrtoul.c */ +unsigned long estrtoul(const char *, int); + /* explicit_bzero.c */ #undef explicit_bzero void explicit_bzero(void *, size_t); diff --git a/util/estrtoul.c b/util/estrtoul.c new file mode 100644 index 0000000..f56a720 --- /dev/null +++ b/util/estrtoul.c @@ -0,0 +1,26 @@ +/* See LICENSE file for copyright and license details. */ +#include +#include +#include + +#include "../util.h" + +unsigned long +estrtoul(const char *s, int base) +{ + char *end; + unsigned long n; + + errno = 0; + n = strtoul(s, &end, base); + if(*end != '\0') { + if(base == 0) + eprintf("%s: not an integer\n", s); + else + eprintf("%s: not a base %d integer\n", s, base); + } + if(errno != 0) + eprintf("%s:", s); + + return n; +}