diff options
author | Maria Matejka <mq@ucw.cz> | 2022-05-30 15:15:19 +0200 |
---|---|---|
committer | Maria Matejka <mq@ucw.cz> | 2022-05-30 15:15:19 +0200 |
commit | 921344c3ba5a0e30f04511d2039dff79b6f82dd9 (patch) | |
tree | 64c496aa9c26c38cd0f5219c79e3d2863997d2c8 /lib/printf.c | |
parent | b4336b0880dee2a039ca55321f4344444512f97d (diff) | |
parent | 3a6eda995ecfcebff3130d86ee3baeab12a41335 (diff) |
Merge commit '3a6eda995ecfcebff3130d86ee3baeab12a41335' into haugesund
Diffstat (limited to 'lib/printf.c')
-rw-r--r-- | lib/printf.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/printf.c b/lib/printf.c index 236df427..424d545f 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -568,3 +568,51 @@ buffer_puts(buffer *buf, const char *str) buf->pos = (bp < be) ? bp : buf->end; } + +#define POOL_PRINTF_MAXBUF 1024 + +char *mb_vsprintf(pool *p, const char *fmt, va_list args) +{ + char buf[POOL_PRINTF_MAXBUF]; + int count = bvsnprintf(buf, POOL_PRINTF_MAXBUF, fmt, args); + + if (count < 0) + bug("Attempted to mb_vsprintf() a too long string"); + + char *out = mb_alloc(p, count + 1); + memcpy(out, buf, count + 1); + return out; +} + +char *mb_sprintf(pool *p, const char *fmt, ...) +{ + va_list args; + char *out; + va_start(args, fmt); + out = mb_vsprintf(p, fmt, args); + va_end(args); + return out; +} + +char *lp_vsprintf(linpool *p, const char *fmt, va_list args) +{ + char buf[POOL_PRINTF_MAXBUF]; + int count = bvsnprintf(buf, POOL_PRINTF_MAXBUF, fmt, args); + + if (count < 0) + bug("Attempted to mb_vsprintf() a too long string"); + + char *out = lp_alloc(p, count + 1); + memcpy(out, buf, count + 1); + return out; +} + +char *lp_sprintf(linpool *p, const char *fmt, ...) +{ + va_list args; + char *out; + va_start(args, fmt); + out = lp_vsprintf(p, fmt, args); + va_end(args); + return out; +} |