]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Get rid of asprintf usage and Windows compatibility wrapper
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 10 Jul 2023 06:15:57 +0000 (08:15 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 12 Jul 2023 09:07:57 +0000 (11:07 +0200)
cmake/config.h.in
src/Win32Util.cpp
src/Win32Util.hpp
src/util/environment.cpp

index 2778909eab08167d505b4cda708bb177af0f9531..5a106e6fe81367bd39270554fc54cf9386fcc50d 100644 (file)
@@ -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
index 2769f48a3374f884a214a70631196e4912e81c9c..d90cde1ab4c03e550cef4e71eb2728b7f4c69bc0 100644 (file)
@@ -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<char*>(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
index 2611759f85d4331eea4ebd86943444ff3b5c13b3..a246381fcc40c62122f817d867e2649d16b083e4 100644 (file)
@@ -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.
 //
 
 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"
index 1e4e70f860ea03403f40690432b417b0ec4d9cbc..516fd1398e736ddfe463197f968ffdacb651352f 100644 (file)
@@ -18,7 +18,6 @@
 
 #include "environment.hpp"
 
-#include <Win32Util.hpp> // for asprintf
 #include <core/exceptions.hpp>
 #include <core/wincompat.hpp>
 #include <fmtmacros.hpp>
@@ -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
 }