va_copy() fix for vasprintf
[moonshot.git] / moonshot / mech_eap / vasprintf.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * printf.c
4  *
5  * Copyright 2003, 2004, 2005, 2007, 2008 Massachusetts Institute of Technology.
6  * All Rights Reserved.
7  *
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.
12  *
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.
26  *
27  *
28  * Provide {,v}asprintf for platforms that don't have them.
29  */
30
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <malloc.h>
35
36 #ifndef SIZE_MAX
37 # define SIZE_MAX ((size_t)((size_t)0 - 1))
38 #endif
39 #if defined(HAS_VA_COPY) || defined(va_copy)
40 /* Do nothing.  */
41 #else
42 #define va_copy(dest, src)      ((dest) = (src))
43 #endif
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.
46
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
50    for that case.  */
51 #define va_copy(dest, src)      memcpy(dest, src, sizeof(va_list)) /* was 'memcmp' which is almost certainly not right...*/
52 #endif
53
54 /* On error: BSD: Set *ret to NULL.  GNU: *ret is undefined.
55
56    Since we want to be able to use the GNU version directly, we need
57    provide only the weaker guarantee in this version.  */
58 int
59 vasprintf(char **ret, const char *format, va_list ap)
60 {
61     va_list ap2;
62     char *str = NULL, *nstr;
63     size_t len = 80;
64     int len2;
65
66     while (1) {
67         if (len >= SIZE_MAX || len == 0)
68             goto fail;
69         nstr = realloc(str, len);
70         if (nstr == NULL)
71             goto fail;
72         str = nstr;
73
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. */
79         va_copy(ap2, ap);
80         len2 = vsnprintf(str, len, format, ap2);
81         va_end(ap2);
82         /* ISO C vsnprintf returns the needed length.  Some old
83            vsnprintf implementations return -1 on truncation.  */
84         if (len2 < 0) {
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)
88                 len *= 2;
89             else if (len < SIZE_MAX)
90                 len = SIZE_MAX;
91             else
92                 goto fail;
93         } else if ((unsigned int) len2 >= SIZE_MAX) {
94             /* Need more space than we can request.  */
95             goto fail;
96         } else if ((size_t) len2 >= len) {
97             /* Need more space, but we know how much.  */
98             len = (size_t) len2 + 1;
99         } else {
100             /* Success!  */
101             break;
102         }
103     }
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);
108         if (nstr)
109             str = nstr;
110     }
111     *ret = str;
112     return len2;
113
114 fail:
115     free(str);
116     return -1;
117 }