/ compat / strtonum.c
strtonum.c
 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.7 2013/04/17 18:40:58 tedu Exp $
18   */
19  
20  #include "bsd_compat.h"
21  
22  #if !HAVE_STRTONUM
23  
24  #include <errno.h>
25  #include <limits.h>
26  #include <stdlib.h>
27  
28  #define	INVALID		1
29  #define	TOOSMALL	2
30  #define	TOOLARGE	3
31  
32  long long
33  strtonum(const char *numstr, long long minval, long long maxval,
34      const char **errstrp)
35  {
36  	long long ll = 0;
37  	int error = 0;
38  	char *ep;
39  	struct errval {
40  		const char *errstr;
41  		int err;
42  	} ev[4] = {
43  		{ NULL,		0 },
44  		{ "invalid",	EINVAL },
45  		{ "too small",	ERANGE },
46  		{ "too large",	ERANGE },
47  	};
48  
49  	ev[0].err = errno;
50  	errno = 0;
51  	if (minval > maxval) {
52  		error = INVALID;
53  	} else {
54  		ll = strtoll(numstr, &ep, 10);
55  		if (errno == EINVAL || numstr == ep || *ep != '\0')
56  			error = INVALID;
57  		else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
58  			error = TOOSMALL;
59  		else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
60  			error = TOOLARGE;
61  	}
62  	if (errstrp != NULL)
63  		*errstrp = ev[error].errstr;
64  	errno = ev[error].err;
65  	if (error)
66  		ll = 0;
67  
68  	return (ll);
69  }
70  
71  #endif