diff options
-rw-r--r-- | configure.ac.tpl | 5 | ||||
-rw-r--r-- | crypto/Makefile.am.tpl | 4 | ||||
-rw-r--r-- | crypto/compat/asprintf.c | 94 | ||||
-rw-r--r-- | include/Makefile.am | 1 | ||||
-rw-r--r-- | include/stdio.h | 11 |
5 files changed, 115 insertions, 0 deletions
diff --git a/configure.ac.tpl b/configure.ac.tpl index bb5e04a..de3995c 100644 --- a/configure.ac.tpl +++ b/configure.ac.tpl | |||
@@ -72,6 +72,11 @@ AC_CHECK_FUNC(strndup, | |||
72 | AC_DEFINE(NO_STRNDUP) | 72 | AC_DEFINE(NO_STRNDUP) |
73 | AM_CONDITIONAL(NO_STRNDUP, true)) | 73 | AM_CONDITIONAL(NO_STRNDUP, true)) |
74 | 74 | ||
75 | AC_CHECK_FUNC(asprintf, | ||
76 | AM_CONDITIONAL(NO_ASPRINTF, false), | ||
77 | AC_DEFINE(NO_ASPRINTF) | ||
78 | AM_CONDITIONAL(NO_ASPRINTF, true)) | ||
79 | |||
75 | AC_CHECK_FUNC(reallocarray, | 80 | AC_CHECK_FUNC(reallocarray, |
76 | AM_CONDITIONAL(NO_REALLOCARRAY, false), | 81 | AM_CONDITIONAL(NO_REALLOCARRAY, false), |
77 | AC_DEFINE(NO_REALLOCARRAY) | 82 | AC_DEFINE(NO_REALLOCARRAY) |
diff --git a/crypto/Makefile.am.tpl b/crypto/Makefile.am.tpl index 6ac6c4f..7688fb9 100644 --- a/crypto/Makefile.am.tpl +++ b/crypto/Makefile.am.tpl | |||
@@ -38,6 +38,10 @@ libcompat_la_SOURCES += compat/strndup.c | |||
38 | libcompat_la_SOURCES += compat/strnlen.c | 38 | libcompat_la_SOURCES += compat/strnlen.c |
39 | endif | 39 | endif |
40 | 40 | ||
41 | if NO_ASPRINTF | ||
42 | libcompat_la_SOURCES += compat/asprintf.c | ||
43 | endif | ||
44 | |||
41 | if NO_REALLOCARRAY | 45 | if NO_REALLOCARRAY |
42 | libcompat_la_SOURCES += compat/reallocarray.c | 46 | libcompat_la_SOURCES += compat/reallocarray.c |
43 | endif | 47 | endif |
diff --git a/crypto/compat/asprintf.c b/crypto/compat/asprintf.c new file mode 100644 index 0000000..45a539d --- /dev/null +++ b/crypto/compat/asprintf.c | |||
@@ -0,0 +1,94 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2004 Darren Tucker. | ||
3 | * | ||
4 | * Based originally on asprintf.c from OpenBSD: | ||
5 | * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
6 | * | ||
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. | ||
10 | * | ||
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. | ||
18 | */ | ||
19 | |||
20 | #include <errno.h> | ||
21 | #include <stdarg.h> | ||
22 | #include <stdlib.h> | ||
23 | |||
24 | #ifndef VA_COPY | ||
25 | # ifdef HAVE_VA_COPY | ||
26 | # define VA_COPY(dest, src) va_copy(dest, src) | ||
27 | # else | ||
28 | # ifdef HAVE___VA_COPY | ||
29 | # define VA_COPY(dest, src) __va_copy(dest, src) | ||
30 | # else | ||
31 | # define VA_COPY(dest, src) (dest) = (src) | ||
32 | # endif | ||
33 | # endif | ||
34 | #endif | ||
35 | |||
36 | #define INIT_SZ 128 | ||
37 | |||
38 | int | ||
39 | vasprintf(char **str, const char *fmt, va_list ap) | ||
40 | { | ||
41 | int ret = -1; | ||
42 | va_list ap2; | ||
43 | char *string, *newstr; | ||
44 | size_t len; | ||
45 | |||
46 | VA_COPY(ap2, ap); | ||
47 | if ((string = malloc(INIT_SZ)) == NULL) | ||
48 | goto fail; | ||
49 | |||
50 | ret = vsnprintf(string, INIT_SZ, fmt, ap2); | ||
51 | if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ | ||
52 | *str = string; | ||
53 | } else if (ret == INT_MAX || ret < 0) { /* Bad length */ | ||
54 | free(string); | ||
55 | goto fail; | ||
56 | } else { /* bigger than initial, realloc allowing for nul */ | ||
57 | len = (size_t)ret + 1; | ||
58 | if ((newstr = realloc(string, len)) == NULL) { | ||
59 | free(string); | ||
60 | goto fail; | ||
61 | } else { | ||
62 | va_end(ap2); | ||
63 | VA_COPY(ap2, ap); | ||
64 | ret = vsnprintf(newstr, len, fmt, ap2); | ||
65 | if (ret >= 0 && (size_t)ret < len) { | ||
66 | *str = newstr; | ||
67 | } else { /* failed with realloc'ed string, give up */ | ||
68 | free(newstr); | ||
69 | goto fail; | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | va_end(ap2); | ||
74 | return (ret); | ||
75 | |||
76 | fail: | ||
77 | *str = NULL; | ||
78 | errno = ENOMEM; | ||
79 | va_end(ap2); | ||
80 | return (-1); | ||
81 | } | ||
82 | |||
83 | int asprintf(char **str, const char *fmt, ...) | ||
84 | { | ||
85 | va_list ap; | ||
86 | int ret; | ||
87 | |||
88 | *str = NULL; | ||
89 | va_start(ap, fmt); | ||
90 | ret = vasprintf(str, fmt, ap); | ||
91 | va_end(ap); | ||
92 | |||
93 | return ret; | ||
94 | } | ||
diff --git a/include/Makefile.am b/include/Makefile.am index 889f775..d4487d7 100644 --- a/include/Makefile.am +++ b/include/Makefile.am | |||
@@ -2,6 +2,7 @@ SUBDIRS = openssl | |||
2 | 2 | ||
3 | noinst_HEADERS = err.h | 3 | noinst_HEADERS = err.h |
4 | noinst_HEADERS += pqueue.h | 4 | noinst_HEADERS += pqueue.h |
5 | noinst_HEADERS += stdio.h | ||
5 | noinst_HEADERS += stdlib.h | 6 | noinst_HEADERS += stdlib.h |
6 | noinst_HEADERS += string.h | 7 | noinst_HEADERS += string.h |
7 | noinst_HEADERS += unistd.h | 8 | noinst_HEADERS += unistd.h |
diff --git a/include/stdio.h b/include/stdio.h new file mode 100644 index 0000000..e23fbe5 --- /dev/null +++ b/include/stdio.h | |||
@@ -0,0 +1,11 @@ | |||
1 | #include_next <stdio.h> | ||
2 | |||
3 | #ifndef LIBCRYPTOCOMPAT_STDIO_H | ||
4 | #define LIBCRYPTOCOMPAT_STDIO_H | ||
5 | |||
6 | #ifdef NO_ASPRINTF | ||
7 | int vasprintf(char **str, const char *fmt, va_list ap); | ||
8 | int asprintf(char **str, const char *fmt, ...); | ||
9 | #endif | ||
10 | |||
11 | #endif | ||