]> git.ozlabs.org Git - ccan/blobdiff - tools/_infotojson/utils.c
commiting _info to json convertor
[ccan] / tools / _infotojson / utils.c
diff --git a/tools/_infotojson/utils.c b/tools/_infotojson/utils.c
new file mode 100644 (file)
index 0000000..9f99972
--- /dev/null
@@ -0,0 +1,55 @@
+#include "utils.h"
+#include <stdio.h>
+#include <string.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
+#include "utils.h"
+
+void * palloc(int size)
+{
+       void *p;
+       p = malloc(size);
+       if(p == NULL) {
+               printf("Error Malloc does not allocate\n");
+               exit(1);
+       }
+       return p;
+}
+
+char *aprintf(const char *fmt, ...)
+{
+       char *ret;
+       va_list arglist;
+
+       va_start(arglist, fmt);
+       vasprintf(&ret, fmt, arglist);
+       va_end(arglist);
+       return ret;
+}
+
+int strreplace(char * str, char src, char dest)
+{
+       int i;
+       for(i = 0; str[i]; i++)
+               if(str[i] == src)
+                       str[i] = dest;
+}
+
+void *_realloc_array(void *ptr, size_t size, size_t num)
+{
+        if (num >= SIZE_MAX/size)
+                return NULL;
+        return realloc_nofail(ptr, size * num);
+}
+
+void *realloc_nofail(void *ptr, size_t size)
+{
+        ptr = realloc(ptr, size);
+       if (ptr)
+               return ptr;
+       printf("realloc of %zu failed", size);
+}