xref: /openbsd-src/lib/libc/stdlib/strtonum.c (revision 0d943ef019900239de431dfc98899a9f48f183de)
1*0d943ef0Sguenther /*	$OpenBSD: strtonum.c,v 1.8 2015/09/13 08:31:48 guenther Exp $	*/
218a24122Smillert 
3912e8fc7Stedu /*
4912e8fc7Stedu  * Copyright (c) 2004 Ted Unangst and Todd Miller
5912e8fc7Stedu  * All rights reserved.
6912e8fc7Stedu  *
7912e8fc7Stedu  * Permission to use, copy, modify, and distribute this software for any
8912e8fc7Stedu  * purpose with or without fee is hereby granted, provided that the above
9912e8fc7Stedu  * copyright notice and this permission notice appear in all copies.
10912e8fc7Stedu  *
11912e8fc7Stedu  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12912e8fc7Stedu  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13912e8fc7Stedu  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14912e8fc7Stedu  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15912e8fc7Stedu  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16912e8fc7Stedu  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17912e8fc7Stedu  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18912e8fc7Stedu  */
19912e8fc7Stedu 
20912e8fc7Stedu #include <errno.h>
21912e8fc7Stedu #include <limits.h>
22912e8fc7Stedu #include <stdlib.h>
23912e8fc7Stedu 
24912e8fc7Stedu #define	INVALID		1
25912e8fc7Stedu #define	TOOSMALL	2
26912e8fc7Stedu #define	TOOLARGE	3
27912e8fc7Stedu 
2818a24122Smillert long long
strtonum(const char * numstr,long long minval,long long maxval,const char ** errstrp)2918a24122Smillert strtonum(const char *numstr, long long minval, long long maxval,
30912e8fc7Stedu     const char **errstrp)
31912e8fc7Stedu {
3218a24122Smillert 	long long ll = 0;
336b22fe52Smillert 	int error = 0;
34a2744935Stedu 	char *ep;
35912e8fc7Stedu 	struct errval {
36912e8fc7Stedu 		const char *errstr;
375c9e5585Smarc 		int err;
38912e8fc7Stedu 	} ev[4] = {
39912e8fc7Stedu 		{ NULL,		0 },
40912e8fc7Stedu 		{ "invalid",	EINVAL },
41912e8fc7Stedu 		{ "too small",	ERANGE },
42912e8fc7Stedu 		{ "too large",	ERANGE },
43912e8fc7Stedu 	};
44912e8fc7Stedu 
455c9e5585Smarc 	ev[0].err = errno;
46912e8fc7Stedu 	errno = 0;
47a2744935Stedu 	if (minval > maxval) {
48912e8fc7Stedu 		error = INVALID;
49a2744935Stedu 	} else {
506b22fe52Smillert 		ll = strtoll(numstr, &ep, 10);
51912e8fc7Stedu 		if (numstr == ep || *ep != '\0')
52912e8fc7Stedu 			error = INVALID;
53912e8fc7Stedu 		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
54912e8fc7Stedu 			error = TOOSMALL;
55912e8fc7Stedu 		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
56912e8fc7Stedu 			error = TOOLARGE;
57912e8fc7Stedu 	}
58912e8fc7Stedu 	if (errstrp != NULL)
59912e8fc7Stedu 		*errstrp = ev[error].errstr;
605c9e5585Smarc 	errno = ev[error].err;
61912e8fc7Stedu 	if (error)
6218a24122Smillert 		ll = 0;
63912e8fc7Stedu 
6418a24122Smillert 	return (ll);
65912e8fc7Stedu }
66*0d943ef0Sguenther DEF_WEAK(strtonum);
67