1043840dfSDag-Erling Smørgrav /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
2043840dfSDag-Erling Smørgrav
3043840dfSDag-Erling Smørgrav /*
4043840dfSDag-Erling Smørgrav * Copyright (c) 2004 Ted Unangst and Todd Miller
5043840dfSDag-Erling Smørgrav * All rights reserved.
6043840dfSDag-Erling Smørgrav *
7043840dfSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
8043840dfSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
9043840dfSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
10043840dfSDag-Erling Smørgrav *
11043840dfSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12043840dfSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13043840dfSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14043840dfSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15043840dfSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16043840dfSDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17043840dfSDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18043840dfSDag-Erling Smørgrav */
19043840dfSDag-Erling Smørgrav
20021d409fSDag-Erling Smørgrav /* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */
21021d409fSDag-Erling Smørgrav
22043840dfSDag-Erling Smørgrav #include "includes.h"
23761efaa7SDag-Erling Smørgrav
24043840dfSDag-Erling Smørgrav #ifndef HAVE_STRTONUM
25761efaa7SDag-Erling Smørgrav #include <stdlib.h>
26043840dfSDag-Erling Smørgrav #include <limits.h>
27761efaa7SDag-Erling Smørgrav #include <errno.h>
28043840dfSDag-Erling Smørgrav
29043840dfSDag-Erling Smørgrav #define INVALID 1
30043840dfSDag-Erling Smørgrav #define TOOSMALL 2
31043840dfSDag-Erling Smørgrav #define TOOLARGE 3
32043840dfSDag-Erling Smørgrav
33043840dfSDag-Erling Smørgrav long long
strtonum(const char * numstr,long long minval,long long maxval,const char ** errstrp)34043840dfSDag-Erling Smørgrav strtonum(const char *numstr, long long minval, long long maxval,
35043840dfSDag-Erling Smørgrav const char **errstrp)
36043840dfSDag-Erling Smørgrav {
37043840dfSDag-Erling Smørgrav long long ll = 0;
38043840dfSDag-Erling Smørgrav char *ep;
39043840dfSDag-Erling Smørgrav int error = 0;
40043840dfSDag-Erling Smørgrav struct errval {
41043840dfSDag-Erling Smørgrav const char *errstr;
42043840dfSDag-Erling Smørgrav int err;
43043840dfSDag-Erling Smørgrav } ev[4] = {
44043840dfSDag-Erling Smørgrav { NULL, 0 },
45043840dfSDag-Erling Smørgrav { "invalid", EINVAL },
46043840dfSDag-Erling Smørgrav { "too small", ERANGE },
47043840dfSDag-Erling Smørgrav { "too large", ERANGE },
48043840dfSDag-Erling Smørgrav };
49043840dfSDag-Erling Smørgrav
50043840dfSDag-Erling Smørgrav ev[0].err = errno;
51043840dfSDag-Erling Smørgrav errno = 0;
52043840dfSDag-Erling Smørgrav if (minval > maxval)
53043840dfSDag-Erling Smørgrav error = INVALID;
54043840dfSDag-Erling Smørgrav else {
55043840dfSDag-Erling Smørgrav ll = strtoll(numstr, &ep, 10);
56043840dfSDag-Erling Smørgrav if (numstr == ep || *ep != '\0')
57043840dfSDag-Erling Smørgrav error = INVALID;
58043840dfSDag-Erling Smørgrav else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
59043840dfSDag-Erling Smørgrav error = TOOSMALL;
60043840dfSDag-Erling Smørgrav else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
61043840dfSDag-Erling Smørgrav error = TOOLARGE;
62043840dfSDag-Erling Smørgrav }
63043840dfSDag-Erling Smørgrav if (errstrp != NULL)
64043840dfSDag-Erling Smørgrav *errstrp = ev[error].errstr;
65043840dfSDag-Erling Smørgrav errno = ev[error].err;
66043840dfSDag-Erling Smørgrav if (error)
67043840dfSDag-Erling Smørgrav ll = 0;
68043840dfSDag-Erling Smørgrav
69043840dfSDag-Erling Smørgrav return (ll);
70043840dfSDag-Erling Smørgrav }
71043840dfSDag-Erling Smørgrav
72043840dfSDag-Erling Smørgrav #endif /* HAVE_STRTONUM */
73