Multicast test code.
[mctap.git] / src / utils.c
diff --git a/src/utils.c b/src/utils.c
new file mode 100644 (file)
index 0000000..d0219a0
--- /dev/null
@@ -0,0 +1,67 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "utils.h"
+
+void _sizebuf2(void **buf, size_t *sz, size_t wanted, size_t el)
+{
+    size_t n;
+    
+    n = *sz;
+    if(n == 0)
+       n = 1;
+    while(n < wanted)
+       n <<= 1;
+    if(n <= *sz)
+       return;
+    if(*buf != NULL)
+       *buf = srealloc(*buf, n * el);
+    else
+       *buf = smalloc(n * el);
+    *sz = n;
+}
+
+void _sizebuf(struct buffer *buf, size_t wanted, size_t el)
+{
+    size_t n;
+    
+    n = buf->s;
+    if(n == 0)
+       n = 1;
+    while(n < wanted)
+       n <<= 1;
+    if(n <= buf->s)
+       return;
+    if(buf->b != NULL)
+       buf->b = srealloc(buf->b, n * el);
+    else
+       buf->b = smalloc(n * el);
+    buf->s = n;
+}
+
+char *vsprintf2(char *format, va_list al)
+{
+    int ret;
+    char *buf;
+    va_list al2;
+    
+    va_copy(al2, al);
+    ret = vsnprintf(NULL, 0, format, al2);
+    va_end(al2);
+    buf = smalloc(ret + 1);
+    va_copy(al2, al);
+    vsnprintf(buf, ret + 1, format, al2);
+    va_end(al2);
+    return(buf);
+}
+
+char *sprintf2(char *format, ...)
+{
+    va_list args;
+    char *buf;
+    
+    va_start(args, format);
+    buf = vsprintf2(format, args);
+    va_end(args);
+    return(buf);
+}