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); }
int __cdecl _scprintf_p( const char* format, ... ) { va_list arglist; va_start(arglist, format); return _vscprintf_p(format, arglist); }
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); }
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; }