]> mj.ucw.cz Git - libucw.git/blob - lib/printf.c
added v?xprintf() functions, they will be used in the filter dumper
[libucw.git] / lib / printf.c
1 /*
2  *      Sherlock Library -- auto-resizable printf() functions
3  *
4  *      (c) 2002, Robert Spalek <robert@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/printf.h"
9
10 #include <stdio.h>
11
12 byte *
13 vxprintf(char *msg, va_list v)
14 {
15         static byte *buf = NULL;
16         static int buf_len = 0;
17         int len;
18         if (!buf)
19         {
20                 buf_len = 1024;
21                 buf = xmalloc(buf_len);
22         }
23         while (1)
24         {
25                 len = vsnprintf(buf, buf_len, msg, v);
26                 if (len >= 0 && len < buf_len)
27                         return buf;
28                 else
29                 {
30                         buf_len *= 2;
31                         if (len >= buf_len)
32                                 buf_len = len + 1;
33                         buf = xrealloc(buf, buf_len);
34                 }
35         }
36 }
37
38 byte *
39 xprintf(char *msg, ...)
40 {
41         byte *txt;
42         va_list v;
43         va_start(v, msg);
44         txt = vxprintf(msg, v);
45         va_end(v);
46         return txt;
47 }