| 91 | } |
| 92 | |
| 93 | static int sprintf_auto_resize(char **headp, int *offp, int len, const char *fmt, const char *content) |
| 94 | { |
| 95 | char *head = *headp; |
| 96 | int off = *offp; |
| 97 | char dummy[1]; |
| 98 | int size = snprintf(dummy, 1, fmt, content); |
| 99 | int need = off + size + 1; |
| 100 | |
| 101 | if (need > len) { /* Resize the buffer to fit more content. */ |
| 102 | if (off < len) { /* The buffer on the stack isn't wide enough. Switch to malloc. */ |
| 103 | head = malloc(need); |
| 104 | memcpy(head, *headp, off); |
| 105 | } else { |
| 106 | head = realloc(head, need); |
| 107 | } |
| 108 | if (head == NULL) |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | off += snprintf(head + off, size + 1, fmt, content); |
| 113 | |
| 114 | *headp = head; |
| 115 | *offp = off; |
| 116 | return size; |
| 117 | } |
| 118 | |
| 119 | int logmsgv(loglvl lvl, const char *fmt, va_list args) |
| 120 | { |