summaryrefslogtreecommitdiff
path: root/src/shared.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared.c')
-rw-r--r--src/shared.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/shared.c b/src/shared.c
index 5e58de0..0c7c8e2 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -1,8 +1,10 @@
1#include "shared.h" 1#include "shared.h"
2 2
3#include <stdarg.h>
3#include <stdlib.h> 4#include <stdlib.h>
4#include <errno.h> 5#include <errno.h>
5#include <error.h> 6#include <error.h>
7#include <stdio.h>
6 8
7void* xcalloc(size_t nmemb, size_t size) { 9void* xcalloc(size_t nmemb, size_t size) {
8 void *mem = calloc(nmemb, size); 10 void *mem = calloc(nmemb, size);
@@ -17,4 +19,44 @@ void* xcalloc(size_t nmemb, size_t size) {
17 19
18 20
19 return mem; 21 return mem;
22}
23
24void* xreallocarray(void *ptr, size_t nmemb, size_t size) {
25 void *mem = reallocarray(ptr, nmemb, size);
26 if(mem == NULL) {
27 #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0
28 error(1, errno, "<xreallocarray> Could not allocate memory");
29
30 #endif
31
32 abort();
33 }
34
35 return mem;
36}
37
38int vsaprintf(char **str, const char *format, va_list ap) {
39 va_list ap2;
40 va_copy(ap2, ap);
41
42 int length = vsnprintf(NULL, 0, format, ap2) + 1; // + 1 because sprintf does not count the null byte
43 char *temp = reallocarray(*str, length, sizeof(char));
44 if(temp == NULL)
45 return -1;
46
47 int ret = vsnprintf(temp, length, format, ap);
48 *str = temp;
49
50 va_end(ap2);
51 return ret;
52}
53
54int saprintf(char **str, const char *format, ...) {
55 va_list ap;
56 va_start(ap, format);
57
58 int ret = vsaprintf(str, format, ap);
59
60 va_end(ap);
61 return ret;
20} \ No newline at end of file 62} \ No newline at end of file