int cub_vsnprintf (char *buffer, size_t count, const char *format, va_list argptr) { int len = _vscprintf_p (format, argptr) + 1; if (len > count) { char *cp = malloc (len); if (cp == NULL) { return -1; } len = _vsprintf_p (cp, len, format, argptr); if (len < 0) { free (cp); return len; } memcpy (buffer, cp, count - 1); buffer[count - 1] = 0; free (cp); return count; } return _vsprintf_p (buffer, count, format, argptr); }
extern void DBG(const char *format, ...) { #if DEBUG va_list ap; char msg[256]; va_start(ap, format); _vsprintf_p(msg, 256, format, ap); va_end(ap); elog(NOTICE, msg); #endif }
static inline void hlogv(const char *format, va_list args) { char message[1024] = ""; int num = _vsprintf_p(message, 1024, format, args); if (num) { if (!ipc_pipe_client_write(&pipe, message, num + 1)) { ipc_pipe_client_free(&pipe); } DbgOut(message); DbgOut("\n"); } }
int vasprintf (char **ptr, const char *format, va_list ap) { int len; len = _vscprintf_p (format, ap) + 1; *ptr = (char *) malloc (len * sizeof (char)); if (!*ptr) { return -1; } return _vsprintf_p (*ptr, len, format, ap); }
void DebugPrintf(const char *fmt, ...) { va_list vl; va_start(vl, fmt); int size = _vscprintf(fmt, vl); if (size > 0) { char *buf = (char *)::HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size + 2); if (buf != nullptr) { _vsprintf_p(buf, size + 1, fmt, vl); ::OutputDebugStringA(buf); ::HeapFree(GetProcessHeap(), 0, buf); } } va_end(vl); }
int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...) { // https://msdn.microsoft.com/en-us/library/bt7tawza.aspx // Many of the MSVC / Windows printf-style functions do not support positional // arguments (eg. "%1$s"). We just forward the call to vsnprintf for sane // platforms, but defer to _vsprintf_p on MSVC / Windows. // https://github.com/FFmpeg/FFmpeg/blob/5ae9fa13f5ac640bec113120d540f70971aa635d/compat/msvcrt/snprintf.c#L46 // _vsprintf_p has to be shimmed with _vscprintf_p on -1 (for an example see // above FFmpeg link). va_list args; va_start(args, fmt); #ifndef _MSC_VER int c = vsnprintf(buf, buf_size, fmt, args); #else // _MSC_VER int c = _vsprintf_p(buf, buf_size, fmt, args); if (c == -1) c = _vscprintf_p(fmt, args); #endif // _MSC_VER va_end(args); return c; }
// Emulate snprintf() on Windows, _snprintf() doesn't zero-terminate the buffer on overflow... int snprintf(char* buf, size_t len, const char* fmt, ...) { va_list ap; int n; va_start(ap, fmt); n = _vsprintf_p(buf, len, fmt, ap); va_end(ap); /* It's a sad fact of life that no one ever checks the return value of * snprintf(). Zero-terminating the buffer hopefully reduces the risk * of gaping security holes. */ if (n < 0) { if (len > 0) { buf[0] = '\0'; } } return n; }