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