]> mj.ucw.cz Git - libucw.git/blob - lib/fb-printf.c
- lizard_alloc() turns on the wrapper for SIGSEGV and lizard_free() restores
[libucw.git] / lib / fb-printf.c
1 /*
2  *      Sherlock Library -- Printf on Fastbuf Streams
3  *
4  *      (c) 2002 Martin Mares <mj@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/fastbuf.h"
12
13 #include <alloca.h>
14
15 int
16 vbprintf(struct fastbuf *b, byte *msg, va_list args)
17 {
18   byte *buf;
19   int r, len = 256;
20
21   while (1)
22     {
23       buf = alloca(len);
24       r = vsnprintf(buf, len, msg, args);
25       if (r < 0)
26         len += len;
27       else if (r < len)
28         {
29           bwrite(b, buf, r);
30           return r;
31         }
32       else
33         len = r+1;
34     }
35 }
36
37 int
38 bprintf(struct fastbuf *b, byte *msg, ...)
39 {
40   va_list args;
41   int res;
42
43   va_start(args, msg);
44   res = vbprintf(b, msg, args);
45   va_end(args);
46   return res;
47 }
48
49 #ifdef TEST
50
51 int main(void)
52 {
53   struct fastbuf *b = bfdopen_shared(1, 65536);
54   bprintf(b, "13=%d str=<%s> msg=%m\n", 13, "str");
55   bclose(b);
56   return 0;
57 }
58
59 #endif