ubase

suckless linux base utils
git clone git://git.2f30.org/ubase.git
Log | Files | Refs | README | LICENSE

commit 6ea2b1aa53ec29d3cf9adc7c981bfa4a4c33f29b
parent 144a89326822d55c65997a37ea048622ceb96020
Author: sin <sin@2f30.org>
Date:   Wed Jun  4 13:07:34 +0100

Use estrtoul() in dd(1)

Allow specifications in hex and octal as well.

Diffstat:
Makefile | 1+
dd.c | 8++++----
util.h | 3+++
util/estrtoul.c | 26++++++++++++++++++++++++++
4 files changed, 34 insertions(+), 4 deletions(-)
diff --git 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 @@ -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 @@ -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 @@ -0,0 +1,26 @@ +/* See LICENSE file for copyright and license details. */ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> + +#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; +}