Multicast test code.
[mctap.git] / src / utils.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "utils.h"
5
6 void _sizebuf2(void **buf, size_t *sz, size_t wanted, size_t el)
7 {
8     size_t n;
9     
10     n = *sz;
11     if(n == 0)
12         n = 1;
13     while(n < wanted)
14         n <<= 1;
15     if(n <= *sz)
16         return;
17     if(*buf != NULL)
18         *buf = srealloc(*buf, n * el);
19     else
20         *buf = smalloc(n * el);
21     *sz = n;
22 }
23
24 void _sizebuf(struct buffer *buf, size_t wanted, size_t el)
25 {
26     size_t n;
27     
28     n = buf->s;
29     if(n == 0)
30         n = 1;
31     while(n < wanted)
32         n <<= 1;
33     if(n <= buf->s)
34         return;
35     if(buf->b != NULL)
36         buf->b = srealloc(buf->b, n * el);
37     else
38         buf->b = smalloc(n * el);
39     buf->s = n;
40 }
41
42 char *vsprintf2(char *format, va_list al)
43 {
44     int ret;
45     char *buf;
46     va_list al2;
47     
48     va_copy(al2, al);
49     ret = vsnprintf(NULL, 0, format, al2);
50     va_end(al2);
51     buf = smalloc(ret + 1);
52     va_copy(al2, al);
53     vsnprintf(buf, ret + 1, format, al2);
54     va_end(al2);
55     return(buf);
56 }
57
58 char *sprintf2(char *format, ...)
59 {
60     va_list args;
61     char *buf;
62     
63     va_start(args, format);
64     buf = vsprintf2(format, args);
65     va_end(args);
66     return(buf);
67 }