Damien Miller | 57f3915 | 2005-11-24 19:58:19 +1100 | [diff] [blame] | 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 "includes.h" |
| 21 | |
| 22 | #ifndef HAVE_VASPRINTF |
| 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 vasprintf(char **str, const char *fmt, va_list ap) |
| 39 | { |
| 40 | int ret = -1; |
| 41 | va_list ap2; |
| 42 | char *string, *newstr; |
| 43 | size_t len; |
| 44 | |
| 45 | VA_COPY(ap2, ap); |
| 46 | if ((string = malloc(INIT_SZ)) == NULL) |
| 47 | goto fail; |
| 48 | |
| 49 | ret = vsnprintf(string, INIT_SZ, fmt, ap2); |
| 50 | if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */ |
| 51 | *str = string; |
| 52 | } else if (ret == INT_MAX) { /* shouldn't happen */ |
| 53 | goto fail; |
| 54 | } else { /* bigger than initial, realloc allowing for nul */ |
| 55 | len = (size_t)ret + 1; |
| 56 | if ((newstr = realloc(string, len)) == NULL) { |
| 57 | free(string); |
| 58 | goto fail; |
| 59 | } else { |
| 60 | va_end(ap2); |
| 61 | VA_COPY(ap2, ap); |
| 62 | ret = vsnprintf(newstr, len, fmt, ap2); |
| 63 | if (ret >= 0 && (size_t)ret < len) { |
| 64 | *str = newstr; |
| 65 | } else { /* failed with realloc'ed string, give up */ |
| 66 | free(newstr); |
| 67 | goto fail; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | va_end(ap2); |
| 72 | return (ret); |
| 73 | |
| 74 | fail: |
| 75 | *str = NULL; |
| 76 | errno = ENOMEM; |
| 77 | va_end(ap2); |
| 78 | return (-1); |
| 79 | } |
| 80 | #endif |
| 81 | |
| 82 | #ifndef HAVE_ASPRINTF |
| 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 | } |
| 95 | #endif |