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