]> mj.ucw.cz Git - libucw.git/blob - lib/printf.c
When writing, the data needn't start at the beginning of the buffer.
[libucw.git] / lib / printf.c
1 /*
2  *      Sherlock Library -- auto-resizable printf() functions
3  *
4  *      (c) 2002, Robert Spalek <robert@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/printf.h"
12
13 #include <stdio.h>
14
15 byte *
16 vxprintf(char *msg, va_list v)
17 {
18         static byte *buf = NULL;
19         static int buf_len = 0;
20         int len;
21         if (!buf)
22         {
23                 buf_len = 1024;
24                 buf = xmalloc(buf_len);
25         }
26         while (1)
27         {
28                 len = vsnprintf(buf, buf_len, msg, v);
29                 if (len >= 0 && len < buf_len)
30                         return buf;
31                 else
32                 {
33                         buf_len *= 2;
34                         if (len >= buf_len)
35                                 buf_len = len + 1;
36                         buf = xrealloc(buf, buf_len);
37                 }
38         }
39 }
40
41 byte *
42 xprintf(char *msg, ...)
43 {
44         byte *txt;
45         va_list v;
46         va_start(v, msg);
47         txt = vxprintf(msg, v);
48         va_end(v);
49         return txt;
50 }