1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
5 * Copyright 2003, 2004, 2005, 2007, 2008 Massachusetts Institute of Technology.
8 * Export of this software from the United States of America may
9 * require a specific license from the United States Government.
10 * It is the responsibility of any person or organization contemplating
11 * export to obtain such a license before exporting.
13 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
14 * distribute this software and its documentation for any purpose and
15 * without fee is hereby granted, provided that the above copyright
16 * notice appear in all copies and that both that copyright notice and
17 * this permission notice appear in supporting documentation, and that
18 * the name of M.I.T. not be used in advertising or publicity pertaining
19 * to distribution of the software without specific, written prior
20 * permission. Furthermore if you modify this software you must label
21 * your software as modified software and not distribute it in such a
22 * fashion that it might be confused with the original M.I.T. software.
23 * M.I.T. makes no representations about the suitability of
24 * this software for any purpose. It is provided "as is" without express
25 * or implied warranty.
28 * Provide {,v}asprintf for platforms that don't have them.
37 # define SIZE_MAX ((size_t)((size_t)0 - 1))
39 #if defined(HAS_VA_COPY) || defined(va_copy)
42 #define va_copy(dest, src) ((dest) = (src))
44 #if 0 /* Given that this used to specify 'memcmp' instead of 'memcpy', it is safe to say it has not really been tested and we should not use it */
45 /* Assume array type, but still simply copyable.
47 There is, theoretically, the possibility that va_start will
48 allocate some storage pointed to by the va_list, and in that case
49 we'll just lose. If anyone cares, we could try to devise a test
51 #define va_copy(dest, src) memcpy(dest, src, sizeof(va_list)) /* was 'memcmp' which is almost certainly not right...*/
54 /* On error: BSD: Set *ret to NULL. GNU: *ret is undefined.
56 Since we want to be able to use the GNU version directly, we need
57 provide only the weaker guarantee in this version. */
59 vasprintf(char **ret, const char *format, va_list ap)
62 char *str = NULL, *nstr;
67 if (len >= SIZE_MAX || len == 0)
69 nstr = realloc(str, len);
74 /* In theory, by c99 rules, vsnprintf() may destructively modify the passed in va_list. Therefore, we va_copy it first.
75 * In practice, the va_list _isn't_ modified on windows and windows does not provide a native va_copy(), but the va_list
76 * is just a pointer, which is why the va_copy() we defined above works. If there were a platform where vsnprintf, etc,
77 * destructively modified the va_list _and_ it didn't define a va_copy() macro _and_ it didn't have a vasprintf(),
78 * we could be in trouble. But I don't think I'll be losing any sleep. */
80 len2 = vsnprintf(str, len, format, ap2);
82 /* ISO C vsnprintf returns the needed length. Some old
83 vsnprintf implementations return -1 on truncation. */
85 /* Don't know how much space we need, just that we didn't
86 supply enough; get a bigger buffer and try again. */
87 if (len <= SIZE_MAX/2)
89 else if (len < SIZE_MAX)
93 } else if ((unsigned int) len2 >= SIZE_MAX) {
94 /* Need more space than we can request. */
96 } else if ((size_t) len2 >= len) {
97 /* Need more space, but we know how much. */
98 len = (size_t) len2 + 1;
104 /* We might've allocated more than we need, if we're still using
105 the initial guess, or we got here by doubling. */
106 if ((size_t) len2 < len - 1) {
107 nstr = realloc(str, (size_t) len2 + 1);