diff options
author | Damien Miller <djm@mindrot.org> | 2005-05-26 20:48:25 +1000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2005-05-26 20:48:25 +1000 |
commit | de3cb0a3dc1bd98762afa3d71f3ffcdb76029fad (patch) | |
tree | 8c0729f879a4458a36ef691480c553382ac2775a /openbsd-compat | |
parent | 84ce9b455d04e7f145d43ef8dac2ddc59e41802d (diff) |
- (djm) [configure.ac openbsd-compat/Makefile.in]
[openbsd-compat/openbsd-compat.h openbsd-compat/strtonum.c]
Add strtonum(3) from OpenBSD libc, new code needs it.
Unfortunately Linux forces us to do a bizarre dance with compiler
options to get LLONG_MIN/MAX; Spotted by and ok dtucker@
Diffstat (limited to 'openbsd-compat')
-rw-r--r-- | openbsd-compat/Makefile.in | 4 | ||||
-rw-r--r-- | openbsd-compat/openbsd-compat.h | 6 | ||||
-rw-r--r-- | openbsd-compat/strtonum.c | 69 |
3 files changed, 76 insertions, 3 deletions
diff --git a/openbsd-compat/Makefile.in b/openbsd-compat/Makefile.in index 0f34f2240..8368aa2ce 100644 --- a/openbsd-compat/Makefile.in +++ b/openbsd-compat/Makefile.in | |||
@@ -1,4 +1,4 @@ | |||
1 | # $Id: Makefile.in,v 1.31 2004/08/15 08:41:00 djm Exp $ | 1 | # $Id: Makefile.in,v 1.32 2005/05/26 10:48:25 djm Exp $ |
2 | 2 | ||
3 | sysconfdir=@sysconfdir@ | 3 | sysconfdir=@sysconfdir@ |
4 | piddir=@piddir@ | 4 | piddir=@piddir@ |
@@ -16,7 +16,7 @@ RANLIB=@RANLIB@ | |||
16 | INSTALL=@INSTALL@ | 16 | INSTALL=@INSTALL@ |
17 | LDFLAGS=-L. @LDFLAGS@ | 17 | LDFLAGS=-L. @LDFLAGS@ |
18 | 18 | ||
19 | OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtoul.o vis.o | 19 | OPENBSD=base64.o basename.o bindresvport.o daemon.o dirname.o getcwd.o getgrouplist.o getopt.o getrrsetbyname.o glob.o inet_aton.o inet_ntoa.o inet_ntop.o mktemp.o readpassphrase.o realpath.o rresvport.o setenv.o setproctitle.o sigact.o strlcat.o strlcpy.o strmode.o strsep.o strtonum.o strtoul.o vis.o |
20 | 20 | ||
21 | COMPAT=bsd-arc4random.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o xmmap.o xcrypt.o | 21 | COMPAT=bsd-arc4random.o bsd-closefrom.o bsd-cray.o bsd-cygwin_util.o bsd-getpeereid.o bsd-misc.o bsd-nextstep.o bsd-openpty.o bsd-snprintf.o bsd-waitpid.o fake-rfc2553.o xmmap.o xcrypt.o |
22 | 22 | ||
diff --git a/openbsd-compat/openbsd-compat.h b/openbsd-compat/openbsd-compat.h index 89d1454e0..a4cfa6c4d 100644 --- a/openbsd-compat/openbsd-compat.h +++ b/openbsd-compat/openbsd-compat.h | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $Id: openbsd-compat.h,v 1.26 2004/08/15 08:41:00 djm Exp $ */ | 1 | /* $Id: openbsd-compat.h,v 1.27 2005/05/26 10:48:25 djm Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Copyright (c) 1999-2003 Damien Miller. All rights reserved. | 4 | * Copyright (c) 1999-2003 Damien Miller. All rights reserved. |
@@ -152,6 +152,10 @@ int openpty(int *, int *, char *, struct termios *, struct winsize *); | |||
152 | int snprintf(char *, size_t, const char *, ...); | 152 | int snprintf(char *, size_t, const char *, ...); |
153 | #endif | 153 | #endif |
154 | 154 | ||
155 | #ifndef HAVE_STRTONUM | ||
156 | long long strtonum(const char *, long long, long long, const char **); | ||
157 | #endif | ||
158 | |||
155 | #ifndef HAVE_VSNPRINTF | 159 | #ifndef HAVE_VSNPRINTF |
156 | int vsnprintf(char *, size_t, const char *, va_list); | 160 | int vsnprintf(char *, size_t, const char *, va_list); |
157 | #endif | 161 | #endif |
diff --git a/openbsd-compat/strtonum.c b/openbsd-compat/strtonum.c new file mode 100644 index 000000000..b681ed83b --- /dev/null +++ b/openbsd-compat/strtonum.c | |||
@@ -0,0 +1,69 @@ | |||
1 | /* OPENBSD ORIGINAL: lib/libc/stdlib/strtonum.c */ | ||
2 | |||
3 | /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ | ||
4 | |||
5 | /* | ||
6 | * Copyright (c) 2004 Ted Unangst and Todd Miller | ||
7 | * All rights reserved. | ||
8 | * | ||
9 | * Permission to use, copy, modify, and distribute this software for any | ||
10 | * purpose with or without fee is hereby granted, provided that the above | ||
11 | * copyright notice and this permission notice appear in all copies. | ||
12 | * | ||
13 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
14 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
15 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
16 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
17 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
18 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
19 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
20 | */ | ||
21 | |||
22 | #include "includes.h" | ||
23 | #ifndef HAVE_STRTONUM | ||
24 | #include <limits.h> | ||
25 | |||
26 | #define INVALID 1 | ||
27 | #define TOOSMALL 2 | ||
28 | #define TOOLARGE 3 | ||
29 | |||
30 | long long | ||
31 | strtonum(const char *numstr, long long minval, long long maxval, | ||
32 | const char **errstrp) | ||
33 | { | ||
34 | long long ll = 0; | ||
35 | char *ep; | ||
36 | int error = 0; | ||
37 | struct errval { | ||
38 | const char *errstr; | ||
39 | int err; | ||
40 | } ev[4] = { | ||
41 | { NULL, 0 }, | ||
42 | { "invalid", EINVAL }, | ||
43 | { "too small", ERANGE }, | ||
44 | { "too large", ERANGE }, | ||
45 | }; | ||
46 | |||
47 | ev[0].err = errno; | ||
48 | errno = 0; | ||
49 | if (minval > maxval) | ||
50 | error = INVALID; | ||
51 | else { | ||
52 | ll = strtoll(numstr, &ep, 10); | ||
53 | if (numstr == ep || *ep != '\0') | ||
54 | error = INVALID; | ||
55 | else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) | ||
56 | error = TOOSMALL; | ||
57 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) | ||
58 | error = TOOLARGE; | ||
59 | } | ||
60 | if (errstrp != NULL) | ||
61 | *errstrp = ev[error].errstr; | ||
62 | errno = ev[error].err; | ||
63 | if (error) | ||
64 | ll = 0; | ||
65 | |||
66 | return (ll); | ||
67 | } | ||
68 | |||
69 | #endif /* HAVE_STRTONUM */ | ||