xref: /freebsd/crypto/openssh/openbsd-compat/strtonum.c (revision 043840df5be0cf8490b48a08fe6d9c316f473f58)
1043840dfSDag-Erling Smørgrav /* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */
2043840dfSDag-Erling Smørgrav 
3043840dfSDag-Erling Smørgrav /*	$OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $	*/
4043840dfSDag-Erling Smørgrav 
5043840dfSDag-Erling Smørgrav /*
6043840dfSDag-Erling Smørgrav  * Copyright (c) 2004 Ted Unangst and Todd Miller
7043840dfSDag-Erling Smørgrav  * All rights reserved.
8043840dfSDag-Erling Smørgrav  *
9043840dfSDag-Erling Smørgrav  * Permission to use, copy, modify, and distribute this software for any
10043840dfSDag-Erling Smørgrav  * purpose with or without fee is hereby granted, provided that the above
11043840dfSDag-Erling Smørgrav  * copyright notice and this permission notice appear in all copies.
12043840dfSDag-Erling Smørgrav  *
13043840dfSDag-Erling Smørgrav  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14043840dfSDag-Erling Smørgrav  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15043840dfSDag-Erling Smørgrav  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16043840dfSDag-Erling Smørgrav  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17043840dfSDag-Erling Smørgrav  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18043840dfSDag-Erling Smørgrav  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19043840dfSDag-Erling Smørgrav  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20043840dfSDag-Erling Smørgrav  */
21043840dfSDag-Erling Smørgrav 
22043840dfSDag-Erling Smørgrav #include "includes.h"
23043840dfSDag-Erling Smørgrav #ifndef HAVE_STRTONUM
24043840dfSDag-Erling Smørgrav #include <limits.h>
25043840dfSDag-Erling Smørgrav 
26043840dfSDag-Erling Smørgrav #define INVALID 	1
27043840dfSDag-Erling Smørgrav #define TOOSMALL 	2
28043840dfSDag-Erling Smørgrav #define TOOLARGE 	3
29043840dfSDag-Erling Smørgrav 
30043840dfSDag-Erling Smørgrav long long
31043840dfSDag-Erling Smørgrav strtonum(const char *numstr, long long minval, long long maxval,
32043840dfSDag-Erling Smørgrav     const char **errstrp)
33043840dfSDag-Erling Smørgrav {
34043840dfSDag-Erling Smørgrav 	long long ll = 0;
35043840dfSDag-Erling Smørgrav 	char *ep;
36043840dfSDag-Erling Smørgrav 	int error = 0;
37043840dfSDag-Erling Smørgrav 	struct errval {
38043840dfSDag-Erling Smørgrav 		const char *errstr;
39043840dfSDag-Erling Smørgrav 		int err;
40043840dfSDag-Erling Smørgrav 	} ev[4] = {
41043840dfSDag-Erling Smørgrav 		{ NULL,		0 },
42043840dfSDag-Erling Smørgrav 		{ "invalid",	EINVAL },
43043840dfSDag-Erling Smørgrav 		{ "too small",	ERANGE },
44043840dfSDag-Erling Smørgrav 		{ "too large",	ERANGE },
45043840dfSDag-Erling Smørgrav 	};
46043840dfSDag-Erling Smørgrav 
47043840dfSDag-Erling Smørgrav 	ev[0].err = errno;
48043840dfSDag-Erling Smørgrav 	errno = 0;
49043840dfSDag-Erling Smørgrav 	if (minval > maxval)
50043840dfSDag-Erling Smørgrav 		error = INVALID;
51043840dfSDag-Erling Smørgrav 	else {
52043840dfSDag-Erling Smørgrav 		ll = strtoll(numstr, &ep, 10);
53043840dfSDag-Erling Smørgrav 		if (numstr == ep || *ep != '\0')
54043840dfSDag-Erling Smørgrav 			error = INVALID;
55043840dfSDag-Erling Smørgrav 		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
56043840dfSDag-Erling Smørgrav 			error = TOOSMALL;
57043840dfSDag-Erling Smørgrav 		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
58043840dfSDag-Erling Smørgrav 			error = TOOLARGE;
59043840dfSDag-Erling Smørgrav 	}
60043840dfSDag-Erling Smørgrav 	if (errstrp != NULL)
61043840dfSDag-Erling Smørgrav 		*errstrp = ev[error].errstr;
62043840dfSDag-Erling Smørgrav 	errno = ev[error].err;
63043840dfSDag-Erling Smørgrav 	if (error)
64043840dfSDag-Erling Smørgrav 		ll = 0;
65043840dfSDag-Erling Smørgrav 
66043840dfSDag-Erling Smørgrav 	return (ll);
67043840dfSDag-Erling Smørgrav }
68043840dfSDag-Erling Smørgrav 
69043840dfSDag-Erling Smørgrav #endif /* HAVE_STRTONUM */
70