1 /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
4 * Copyright (c) 2004 Ted Unangst and Todd Miller
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #if HAVE_NBTOOL_CONFIG_H
20 #include "nbtool_config.h"
22 #include <sys/cdefs.h>
23 __RCSID("$NetBSD: strtonum.c,v 1.2 2009/10/26 21:14:18 christos Exp $");
33 strtonum(const char *numstr
, long long minval
, long long maxval
,
34 const char **errstrp
);
36 strtonum(const char *numstr
, long long minval
, long long maxval
,
47 { "invalid", EINVAL
},
48 { "too small", ERANGE
},
49 { "too large", ERANGE
},
57 ll
= strtoll(numstr
, &ep
, 10);
58 if (numstr
== ep
|| *ep
!= '\0')
60 else if ((ll
== LLONG_MIN
&& errno
== ERANGE
) || ll
< minval
)
62 else if ((ll
== LLONG_MAX
&& errno
== ERANGE
) || ll
> maxval
)
66 *errstrp
= ev
[error
].errstr
;
67 errno
= ev
[error
].err
;