836ffa8d077436950846565f08ac358838cf647e
[libfirm] / ir / obstack / obstack_printf.c
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include "obstack.h"
6
7 #ifdef _WIN32
8 #define vsnprintf _vsnprintf
9 #endif
10
11 int obstack_printf(struct obstack *obst, const char *fmt, ...)
12 {
13         char    buf[128];
14         char   *buffer = buf;
15         size_t  size   = sizeof(buf);
16         va_list ap;
17         int     len;
18
19         for (;;) {
20                 va_start(ap, fmt);
21                 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
22                 va_end(ap);
23
24                 /* snprintf should return -1 only in the error case, but older glibcs
25                  * and probably other systems are buggy in this respect and return -1 if
26                  * the buffer was too small. We only abort for LARGE unrealistic buffer
27                  * sizes here */
28                 if (len < 0) {
29                         if (buffer != buf)
30                                 free(buffer);
31                         if (size > 65536)
32                                 return -1;
33                         size *= 2;
34                 } else if ((size_t)len >= size) {
35                         /* If we come here more than once, vsnprintf() returned garbage */
36                         assert(buffer == buf);
37                         size = (size_t)len + 1;
38                 } else {
39                         break;
40                 }
41                 buffer = malloc(size);
42         }
43
44         obstack_grow(obst, buffer, len);
45         if (buffer != buf)
46                 free(buffer);
47
48         return len;
49 }