From fefe45751774f1adeeb52b70c0e2fad8c859b991 Mon Sep 17 00:00:00 2001 From: crs Date: Wed, 12 Feb 2003 19:50:22 +0000 Subject: [PATCH] Added a simple implementation of vsnprintf for unix platforms without it using /dev/null, vfprintf(), and vsprintf(). --- lib/arch/vsnprintf.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/arch/vsnprintf.cpp b/lib/arch/vsnprintf.cpp index 4bd665d2..f06c0a30 100644 --- a/lib/arch/vsnprintf.cpp +++ b/lib/arch/vsnprintf.cpp @@ -28,7 +28,33 @@ ARCH_STRING::vsnprintf(char* str, int size, const char* fmt, va_list ap) return n; } -#else // !HAVE_VSNPRINTF +#elif UNIX_LIKE // !HAVE_VSNPRINTF + +#include + +int +ARCH_STRING::vsnprintf(char* str, int size, const char* fmt, va_list ap) +{ + static FILE* bitbucket = fopen("/dev/null", "w"); + if (bitbucket == NULL) { + // uh oh + if (size > 0) { + str[0] = '\0'; + } + return 0; + } + else { + // count the characters using the bitbucket + int n = vfprintf(bitbucket, fmt, ap); + if (n + 1 <= size) { + // it'll fit so print it into str + vsprintf(str, fmt, ap); + } + return n; + } +} + +#else // !HAVE_VSNPRINTF && !UNIX_LIKE // FIXME #error vsnprintf not implemented