summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/encryption.c2
-rw-r--r--src/shared.c38
-rw-r--r--src/shared.h5
3 files changed, 43 insertions, 2 deletions
diff --git a/src/encryption.c b/src/encryption.c
index c74c07e..f7fdcd4 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -1,5 +1,3 @@
1#define _GNU_SOURCE
2
3#include "encryption.h" 1#include "encryption.h"
4#include "shared.h" 2#include "shared.h"
5 3
diff --git a/src/shared.c b/src/shared.c
index 2beaeb8..2f6dd5b 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -34,3 +34,41 @@ void* xreallocarray(void *ptr, size_t nmemb, size_t size) {
34 34
35 return mem; 35 return mem;
36} 36}
37
38#if !defined _GNU_SOURCE
39
40int vasprintf(char **str, const char *format, va_list ap) {
41 va_list ap2;
42 int length, ret;
43
44 va_copy(ap2, ap);
45 if((length = vsnprintf(NULL, 0, format, ap2)) < 0)
46 return -1;
47 length++; // + 1 because sprintf does not count the null byte
48 va_end(ap2);
49
50 char *temp = reallocarray(*str, length, sizeof(char));
51 if(temp == NULL)
52 return -1;
53
54 if((ret = vsnprintf(temp, length, format, ap)) < 0) {
55 free(temp);
56 return -1;
57 } else {
58 *str = temp;
59 }
60
61 return ret;
62}
63
64int asprintf(char **str, const char *format, ...) {
65 va_list ap;
66
67 va_start(ap, format);
68 int ret = vasprintf(str, format, ap);
69 va_end(ap);
70
71 return ret;
72}
73
74#endif \ No newline at end of file
diff --git a/src/shared.h b/src/shared.h
index b10462e..684fb9f 100644
--- a/src/shared.h
+++ b/src/shared.h
@@ -15,4 +15,9 @@ void* xcalloc(size_t nmemb, size_t size);
15// `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` 15// `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___`
16void* xreallocarray(void *ptr, size_t nmemb, size_t size); 16void* xreallocarray(void *ptr, size_t nmemb, size_t size);
17 17
18#if !defined _GNU_SOURCE
19int vasprintf(char **str, const char *format, va_list ap);
20int asprintf(char **str, const char *format, ...);
21#endif
22
18#endif \ No newline at end of file 23#endif \ No newline at end of file