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