No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / external / bsd / openssh / dist / strtonum.c
blob5c54577113f4a0563ab1003fafb677109256f2ed
1 /* $NetBSD: strtonum.c,v 1.3 2009/02/16 20:53:55 christos 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 /* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */
23 #include "includes.h"
24 __RCSID("$NetBSD: strtonum.c,v 1.3 2009/02/16 20:53:55 christos Exp $");
26 #ifndef HAVE_STRTONUM
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <limits.h>
31 #include <errno.h>
32 #include <time.h>
33 #include "misc.h"
35 #define INVALID 1
36 #define TOOSMALL 2
37 #define TOOLARGE 3
39 long long
40 strtonum(const char *numstr, long long minval, long long maxval,
41 const char **errstrp)
43 long long ll = 0;
44 char *ep;
45 int error = 0;
46 struct errval {
47 const char *errstr;
48 int err;
49 } ev[4] = {
50 { NULL, 0 },
51 { "invalid", EINVAL },
52 { "too small", ERANGE },
53 { "too large", ERANGE },
56 ev[0].err = errno;
57 errno = 0;
58 if (minval > maxval)
59 error = INVALID;
60 else {
61 ll = strtoll(numstr, &ep, 10);
62 if (numstr == ep || *ep != '\0')
63 error = INVALID;
64 else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
65 error = TOOSMALL;
66 else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
67 error = TOOLARGE;
69 if (errstrp != NULL)
70 *errstrp = ev[error].errstr;
71 errno = ev[error].err;
72 if (error)
73 ll = 0;
75 return (ll);
78 #endif /* HAVE_STRTONUM */