Use estrtoul() in dd(1)

Allow specifications in hex and octal as well.
This commit is contained in:
sin 2014-06-04 13:07:34 +01:00
parent 144a893268
commit 6ea2b1aa53
4 changed files with 34 additions and 4 deletions

View File

@ -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 \

8
dd.c
View File

@ -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)

3
util.h
View File

@ -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);

26
util/estrtoul.c Normal file
View File

@ -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;
}