diff options
author | Ray Lai <ray@cvs.openbsd.org> | 2008-12-01 04:05:44 +0000 |
---|---|---|
committer | Ray Lai <ray@cvs.openbsd.org> | 2008-12-01 04:05:44 +0000 |
commit | adcea25f57a7ee884b33ef817265dc5ecf4f0d9c (patch) | |
tree | f067f099f030b33dbf57301c7d725c7ea4884310 /sbin/fdisk | |
parent | fbadbeeefe185d0b524e0d7c729461c9a3d69b92 (diff) |
Check for invalid integers passed getuint(); instead of getuint()
returning an error, just reprompt. If there is an error reading
from stdin, use the old (default) value.
Shrinks fdisk a few bytes!
OK deraadt.
Diffstat (limited to 'sbin/fdisk')
-rw-r--r-- | sbin/fdisk/misc.c | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/sbin/fdisk/misc.c b/sbin/fdisk/misc.c index 7909aaef717..7656e8d1a73 100644 --- a/sbin/fdisk/misc.c +++ b/sbin/fdisk/misc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.18 2006/11/08 23:22:26 deraadt Exp $ */ +/* $OpenBSD: misc.c,v 1.19 2008/12/01 04:05:43 ray Exp $ */ /* * Copyright (c) 1997 Tobias Weingartner @@ -30,9 +30,7 @@ #include <ctype.h> #include <stdlib.h> #include <string.h> -#include <errno.h> #include <sys/disklabel.h> -#include <limits.h> #include "misc.h" struct unit_type unit_types[] = { @@ -195,16 +193,17 @@ putlong(void *p, u_int32_t l) /* * adapted from sbin/disklabel/editor.c - * Returns UINT_MAX on error + * retries on error */ u_int32_t getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval, u_int32_t maxval, u_int32_t offset, int flags) { - char buf[BUFSIZ], *endptr, *p, operator = '\0'; + char buf[BUFSIZ], *p, operator; + const char *errstr; u_int32_t rval = oval; size_t n; - int mult = 1, secsize = unit_types[SECTORS].conversion; + int mult, secsize = unit_types[SECTORS].conversion; double d; int secpercyl; @@ -213,7 +212,9 @@ getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval, /* We only care about the remainder */ offset = offset % secpercyl; - buf[0] = '\0'; + restart: + mult = 1; + operator = '\0'; do { printf("%s: [%u] ", prompt, oval); if (fgets(buf, sizeof(buf), stdin) == NULL) { @@ -221,7 +222,6 @@ getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval, if (feof(stdin)) { clearerr(stdin); putchar('\n'); - return(UINT_MAX - 1); } } n = strlen(buf); @@ -273,15 +273,10 @@ getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval, if (*p == '+' || *p == '-') operator = *p++; - endptr = p; - errno = 0; - d = strtod(p, &endptr); - if (errno == ERANGE) - rval = UINT_MAX; /* too big/small */ - else if (*endptr != '\0') { - errno = EINVAL; /* non-numbers in str */ - rval = UINT_MAX; - } else { + d = strtonum(p, 0, maxval, &errstr); + if (errstr) + goto restart; + else { /* XXX - should check for overflow */ if (mult > 0) rval = d * mult; @@ -297,7 +292,7 @@ getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval, } } } - if ((flags & DO_ROUNDING) && rval < UINT_MAX) { + if (flags & DO_ROUNDING) { #ifndef CYLCHECK /* Round to nearest cylinder unless given in sectors */ if (mult != 1) |