From: Joel Rosdahl Date: Mon, 10 Jul 2023 06:15:57 +0000 (+0200) Subject: refactor: Get rid of asprintf usage and Windows compatibility wrapper X-Git-Tag: v4.9~138 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3f2b98e12585b0f337d83b945d10956d2cbe65d1;p=thirdparty%2Fccache.git refactor: Get rid of asprintf usage and Windows compatibility wrapper --- diff --git a/cmake/config.h.in b/cmake/config.h.in index 2778909ea..5a106e6fe 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -14,9 +14,8 @@ # define __STDC_FORMAT_MACROS 1 #endif -// For example for vasprintf under i686-w64-mingw32-g++-posix. The later -// definition of _XOPEN_SOURCE disables certain features on Linux, so we need -// _GNU_SOURCE to re-enable them (makedev, tm_zone). +// The later definition of _XOPEN_SOURCE disables certain features on Linux, so +// we need _GNU_SOURCE to re-enable them. #define _GNU_SOURCE 1 // The later definition of _XOPEN_SOURCE and _POSIX_C_SOURCE disables certain diff --git a/src/Win32Util.cpp b/src/Win32Util.cpp index 2769f48a3..d90cde1ab 100644 --- a/src/Win32Util.cpp +++ b/src/Win32Util.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2022 Joel Rosdahl and other contributors +// Copyright (C) 2020-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -143,42 +143,3 @@ localtime_r(time_t* _clock, struct tm* _result) return p; } - -// From: https://stackoverflow.com/a/40160038/262458 -#ifdef _MSC_VER -int -vasprintf(char** strp, const char* fmt, va_list ap) -{ - // _vscprintf tells you how big the buffer needs to be - int len = _vscprintf(fmt, ap); - if (len == -1) { - return -1; - } - size_t size = (size_t)len + 1; - char* str = static_cast(malloc(size)); - if (!str) { - return -1; - } - // vsprintf_s is the "secure" version of vsprintf - int r = vsprintf_s(str, len + 1, fmt, ap); - if (r == -1) { - free(str); - return -1; - } - *strp = str; - return r; -} -#endif - -// Also from: https://stackoverflow.com/a/40160038/262458 -#ifdef _MSC_VER -int -asprintf(char** strp, const char* fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - int r = vasprintf(strp, fmt, ap); - va_end(ap); - return r; -} -#endif diff --git a/src/Win32Util.hpp b/src/Win32Util.hpp index 2611759f8..a246381fc 100644 --- a/src/Win32Util.hpp +++ b/src/Win32Util.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2020-2022 Joel Rosdahl and other contributors +// Copyright (C) 2020-2023 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -26,10 +26,6 @@ struct tm* localtime_r(time_t* _clock, struct tm* _result); -# ifdef _MSC_VER -int asprintf(char** strp, const char* fmt, ...); -# endif - namespace Win32Util { // Add ".exe" suffix to `program` if it doesn't already end with ".exe", ".bat" diff --git a/src/util/environment.cpp b/src/util/environment.cpp index 1e4e70f86..516fd1398 100644 --- a/src/util/environment.cpp +++ b/src/util/environment.cpp @@ -18,7 +18,6 @@ #include "environment.hpp" -#include // for asprintf #include #include #include @@ -85,9 +84,8 @@ setenv(const std::string& name, const std::string& value) #ifdef HAVE_SETENV ::setenv(name.c_str(), value.c_str(), true); #else - char* string; - asprintf(&string, "%s=%s", name.c_str(), value.c_str()); - putenv(string); // Leak to environment. + auto string = FMT("{}={}", name, value); + putenv(strdup(string.c_str())); // Leak to environment. #endif }