/src/hpn-ssh/openbsd-compat/strtonum.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2004 Ted Unangst and Todd Miller |
5 | | * All rights reserved. |
6 | | * |
7 | | * Permission to use, copy, modify, and distribute this software for any |
8 | | * purpose with or without fee is hereby granted, provided that the above |
9 | | * copyright notice and this permission notice appear in all copies. |
10 | | * |
11 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
12 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
13 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
14 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
15 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
16 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
17 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
18 | | */ |
19 | | |
20 | | /* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */ |
21 | | |
22 | | #include "includes.h" |
23 | | |
24 | | #ifndef HAVE_STRTONUM |
25 | | #include <stdlib.h> |
26 | | #include <limits.h> |
27 | | #include <errno.h> |
28 | | |
29 | 0 | #define INVALID 1 |
30 | 0 | #define TOOSMALL 2 |
31 | 0 | #define TOOLARGE 3 |
32 | | |
33 | | long long |
34 | | strtonum(const char *numstr, long long minval, long long maxval, |
35 | | const char **errstrp) |
36 | 0 | { |
37 | 0 | long long ll = 0; |
38 | 0 | char *ep; |
39 | 0 | int error = 0; |
40 | 0 | struct errval { |
41 | 0 | const char *errstr; |
42 | 0 | int err; |
43 | 0 | } ev[4] = { |
44 | 0 | { NULL, 0 }, |
45 | 0 | { "invalid", EINVAL }, |
46 | 0 | { "too small", ERANGE }, |
47 | 0 | { "too large", ERANGE }, |
48 | 0 | }; |
49 | |
|
50 | 0 | ev[0].err = errno; |
51 | 0 | errno = 0; |
52 | 0 | if (minval > maxval) |
53 | 0 | error = INVALID; |
54 | 0 | else { |
55 | 0 | ll = strtoll(numstr, &ep, 10); |
56 | 0 | if (numstr == ep || *ep != '\0') |
57 | 0 | error = INVALID; |
58 | 0 | else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) |
59 | 0 | error = TOOSMALL; |
60 | 0 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) |
61 | 0 | error = TOOLARGE; |
62 | 0 | } |
63 | 0 | if (errstrp != NULL) |
64 | 0 | *errstrp = ev[error].errstr; |
65 | 0 | errno = ev[error].err; |
66 | 0 | if (error) |
67 | 0 | ll = 0; |
68 | |
|
69 | 0 | return (ll); |
70 | 0 | } |
71 | | |
72 | | #endif /* HAVE_STRTONUM */ |