| 9 | #define TAIL 8 |
| 10 | |
| 11 | void msprintf(struct mstring *s, const char *fmt, ...) |
| 12 | { |
| 13 | static char buf[4096]; /* a big static buffer */ |
| 14 | va_list args; |
| 15 | int len; |
| 16 | |
| 17 | if (!s || !s->base) return; |
| 18 | va_start(args, fmt); |
| 19 | vsprintf(buf, fmt, args); |
| 20 | va_end(args); |
| 21 | |
| 22 | len = strlen(buf); |
| 23 | if (len > (s->end - s->ptr)) { |
| 24 | int cp = s->ptr - s->base, cl = s->end - s->base, nl = cl; |
| 25 | while (len > (nl - cp)) |
| 26 | nl = nl + nl + TAIL; |
| 27 | if ((s->base = realloc(s->base, nl))) { |
| 28 | s->ptr = s->base + cp; |
| 29 | s->end = s->base + nl; } |
| 30 | else { |
| 31 | s->ptr = s->end = 0; |
| 32 | return; } } |
| 33 | memcpy(s->ptr, buf, len); |
| 34 | s->ptr += len; |
| 35 | } |
| 36 | |
| 37 | int mputchar(struct mstring *s, int ch) |
| 38 | { |
no test coverage detected