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