xref: /netbsd-src/usr.bin/m4/lib/strtonum.c (revision 315c7490d2bf841fb2c781a53547925ddf7b7e2d)
171dafaa1Schristos /*	$OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $	*/
271dafaa1Schristos 
371dafaa1Schristos /*
471dafaa1Schristos  * Copyright (c) 2004 Ted Unangst and Todd Miller
571dafaa1Schristos  * All rights reserved.
671dafaa1Schristos  *
771dafaa1Schristos  * Permission to use, copy, modify, and distribute this software for any
871dafaa1Schristos  * purpose with or without fee is hereby granted, provided that the above
971dafaa1Schristos  * copyright notice and this permission notice appear in all copies.
1071dafaa1Schristos  *
1171dafaa1Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1271dafaa1Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1371dafaa1Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1471dafaa1Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1571dafaa1Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1671dafaa1Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1771dafaa1Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1871dafaa1Schristos  */
19*315c7490Schristos #if HAVE_NBTOOL_CONFIG_H
20*315c7490Schristos #include "nbtool_config.h"
21*315c7490Schristos #endif
22*315c7490Schristos #include <sys/cdefs.h>
23*315c7490Schristos __RCSID("$NetBSD: strtonum.c,v 1.2 2009/10/26 21:14:18 christos Exp $");
2471dafaa1Schristos #include <errno.h>
2571dafaa1Schristos #include <limits.h>
2671dafaa1Schristos #include <stdlib.h>
2771dafaa1Schristos 
2871dafaa1Schristos #define INVALID 	1
2971dafaa1Schristos #define TOOSMALL 	2
3071dafaa1Schristos #define TOOLARGE 	3
3171dafaa1Schristos 
3271dafaa1Schristos long long
3371dafaa1Schristos strtonum(const char *numstr, long long minval, long long maxval,
34*315c7490Schristos     const char **errstrp);
35*315c7490Schristos long long
strtonum(const char * numstr,long long minval,long long maxval,const char ** errstrp)36*315c7490Schristos strtonum(const char *numstr, long long minval, long long maxval,
3771dafaa1Schristos     const char **errstrp)
3871dafaa1Schristos {
3971dafaa1Schristos 	long long ll = 0;
4071dafaa1Schristos 	char *ep;
4171dafaa1Schristos 	int error = 0;
4271dafaa1Schristos 	struct errval {
4371dafaa1Schristos 		const char *errstr;
4471dafaa1Schristos 		int err;
4571dafaa1Schristos 	} ev[4] = {
4671dafaa1Schristos 		{ NULL,		0 },
4771dafaa1Schristos 		{ "invalid",	EINVAL },
4871dafaa1Schristos 		{ "too small",	ERANGE },
4971dafaa1Schristos 		{ "too large",	ERANGE },
5071dafaa1Schristos 	};
5171dafaa1Schristos 
5271dafaa1Schristos 	ev[0].err = errno;
5371dafaa1Schristos 	errno = 0;
5471dafaa1Schristos 	if (minval > maxval)
5571dafaa1Schristos 		error = INVALID;
5671dafaa1Schristos 	else {
5771dafaa1Schristos 		ll = strtoll(numstr, &ep, 10);
5871dafaa1Schristos 		if (numstr == ep || *ep != '\0')
5971dafaa1Schristos 			error = INVALID;
6071dafaa1Schristos 		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
6171dafaa1Schristos 			error = TOOSMALL;
6271dafaa1Schristos 		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
6371dafaa1Schristos 			error = TOOLARGE;
6471dafaa1Schristos 	}
6571dafaa1Schristos 	if (errstrp != NULL)
6671dafaa1Schristos 		*errstrp = ev[error].errstr;
6771dafaa1Schristos 	errno = ev[error].err;
6871dafaa1Schristos 	if (error)
6971dafaa1Schristos 		ll = 0;
7071dafaa1Schristos 
7171dafaa1Schristos 	return (ll);
7271dafaa1Schristos }
7371dafaa1Schristos 
74