From 5a5e604eac5cf7473c2d129b38b517dc1968c441 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Mon, 6 Jan 2025 21:19:38 -0600 Subject: Use GNU macro (v)asprintf only if _GNU_SOURCE is defined --- src/encryption.c | 2 -- src/shared.c | 38 ++++++++++++++++++++++++++++++++++++++ src/shared.h | 5 +++++ 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 @@ -#define _GNU_SOURCE - #include "encryption.h" #include "shared.h" 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) { return mem; } + +#if !defined _GNU_SOURCE + +int vasprintf(char **str, const char *format, va_list ap) { + va_list ap2; + int length, ret; + + va_copy(ap2, ap); + if((length = vsnprintf(NULL, 0, format, ap2)) < 0) + return -1; + length++; // + 1 because sprintf does not count the null byte + va_end(ap2); + + char *temp = reallocarray(*str, length, sizeof(char)); + if(temp == NULL) + return -1; + + if((ret = vsnprintf(temp, length, format, ap)) < 0) { + free(temp); + return -1; + } else { + *str = temp; + } + + return ret; +} + +int asprintf(char **str, const char *format, ...) { + va_list ap; + + va_start(ap, format); + int ret = vasprintf(str, format, ap); + va_end(ap); + + return ret; +} + +#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); // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` void* xreallocarray(void *ptr, size_t nmemb, size_t size); +#if !defined _GNU_SOURCE +int vasprintf(char **str, const char *format, va_list ap); +int asprintf(char **str, const char *format, ...); +#endif + #endif \ No newline at end of file -- cgit v1.2.3