diff options
| author | millert <> | 2004-08-03 19:38:01 +0000 |
|---|---|---|
| committer | millert <> | 2004-08-03 19:38:01 +0000 |
| commit | ea13120433234de14cfef017f9b900c5d4e3874e (patch) | |
| tree | fc9dbf4911b0ec70447a27591743adde8f6b908c /src/lib/libc/stdlib/strtonum.c | |
| parent | 14931f8ab7a337e1d1f0d6ff78c8d42ffeaf0189 (diff) | |
| download | openbsd-ea13120433234de14cfef017f9b900c5d4e3874e.tar.gz openbsd-ea13120433234de14cfef017f9b900c5d4e3874e.tar.bz2 openbsd-ea13120433234de14cfef017f9b900c5d4e3874e.zip | |
It's not really possible to make strtonum() deal with unsigned long
long values properly so don't bother trying. This greatly simplifies
the code. tedu@ OK with input from otto@ and others.
Diffstat (limited to 'src/lib/libc/stdlib/strtonum.c')
| -rw-r--r-- | src/lib/libc/stdlib/strtonum.c | 37 |
1 files changed, 10 insertions, 27 deletions
diff --git a/src/lib/libc/stdlib/strtonum.c b/src/lib/libc/stdlib/strtonum.c index a2dfed25ca..e426388ed0 100644 --- a/src/lib/libc/stdlib/strtonum.c +++ b/src/lib/libc/stdlib/strtonum.c | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | /* $OpenBSD: strtonum.c,v 1.5 2004/07/16 18:36:05 otto Exp $ */ | 1 | /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ |
| 2 | |||
| 2 | /* | 3 | /* |
| 3 | * Copyright (c) 2004 Ted Unangst and Todd Miller | 4 | * Copyright (c) 2004 Ted Unangst and Todd Miller |
| 4 | * All rights reserved. | 5 | * All rights reserved. |
| @@ -24,12 +25,11 @@ | |||
| 24 | #define TOOSMALL 2 | 25 | #define TOOSMALL 2 |
| 25 | #define TOOLARGE 3 | 26 | #define TOOLARGE 3 |
| 26 | 27 | ||
| 27 | unsigned long long | 28 | long long |
| 28 | strtonum(const char *numstr, long long minval, unsigned long long umaxval, | 29 | strtonum(const char *numstr, long long minval, long long maxval, |
| 29 | const char **errstrp) | 30 | const char **errstrp) |
| 30 | { | 31 | { |
| 31 | long long ll, maxval = (long long)umaxval; | 32 | long long ll = 0; |
| 32 | unsigned long long ull = 0; | ||
| 33 | char *ep; | 33 | char *ep; |
| 34 | int error = 0; | 34 | int error = 0; |
| 35 | struct errval { | 35 | struct errval { |
| @@ -44,24 +44,9 @@ strtonum(const char *numstr, long long minval, unsigned long long umaxval, | |||
| 44 | 44 | ||
| 45 | ev[0].err = errno; | 45 | ev[0].err = errno; |
| 46 | errno = 0; | 46 | errno = 0; |
| 47 | if (umaxval > LLONG_MAX ) { | 47 | if (minval > maxval) |
| 48 | if (minval < 0) { | 48 | error = INVALID; |
| 49 | error = INVALID; | 49 | else { |
| 50 | goto done; | ||
| 51 | } | ||
| 52 | ull = strtoull(numstr, &ep, 10); | ||
| 53 | if (numstr == ep || *ep != '\0') | ||
| 54 | error = INVALID; | ||
| 55 | else if ((ull == ULLONG_MAX && errno == ERANGE) || | ||
| 56 | ull > umaxval) | ||
| 57 | error = TOOLARGE; | ||
| 58 | else if (ull < minval) | ||
| 59 | error = TOOSMALL; | ||
| 60 | } else { | ||
| 61 | if (minval > maxval || maxval < minval) { | ||
| 62 | error = INVALID; | ||
| 63 | goto done; | ||
| 64 | } | ||
| 65 | ll = strtoll(numstr, &ep, 10); | 50 | ll = strtoll(numstr, &ep, 10); |
| 66 | if (numstr == ep || *ep != '\0') | 51 | if (numstr == ep || *ep != '\0') |
| 67 | error = INVALID; | 52 | error = INVALID; |
| @@ -69,14 +54,12 @@ strtonum(const char *numstr, long long minval, unsigned long long umaxval, | |||
| 69 | error = TOOSMALL; | 54 | error = TOOSMALL; |
| 70 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) | 55 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) |
| 71 | error = TOOLARGE; | 56 | error = TOOLARGE; |
| 72 | ull = (unsigned long long)ll; | ||
| 73 | } | 57 | } |
| 74 | done: | ||
| 75 | if (errstrp != NULL) | 58 | if (errstrp != NULL) |
| 76 | *errstrp = ev[error].errstr; | 59 | *errstrp = ev[error].errstr; |
| 77 | errno = ev[error].err; | 60 | errno = ev[error].err; |
| 78 | if (error) | 61 | if (error) |
| 79 | ull = 0; | 62 | ll = 0; |
| 80 | 63 | ||
| 81 | return (ull); | 64 | return (ll); |
| 82 | } | 65 | } |
