mirror of
https://github.com/MariaDB/server.git
synced 2025-01-26 00:34:18 +01:00
Add ut_snprintf() function. On Windows this needs to be implemented
using auxiliary functions because there is no snprintf-variant on Windows that behaves exactly as specified in the standard: * Always return the number of characters that would have been printed if the size were unlimited (not including the final `\0'). * Always '\0'-terminate the result * Do not touch the buffer if size=0, only return the number of characters that would have been printed. Can be used to estimate the size needed and to allocate it dynamically. See http://www.freebsd.org/cgi/query-pr.cgi?pr=87260 for the reason why 2 ap variables are used. Approved by: Heikki
This commit is contained in:
parent
8785af49a3
commit
6a0f6df938
2 changed files with 62 additions and 0 deletions
|
@ -290,6 +290,24 @@ ut_copy_file(
|
|||
FILE* dest, /* in: output file */
|
||||
FILE* src); /* in: input file to be appended to output */
|
||||
|
||||
/**************************************************************************
|
||||
snprintf(). */
|
||||
|
||||
#ifdef __WIN__
|
||||
int
|
||||
ut_snprintf(
|
||||
/* out: number of characters that would
|
||||
have been printed if the size were
|
||||
unlimited, not including the terminating
|
||||
'\0'. */
|
||||
char* str, /* out: string */
|
||||
size_t size, /* in: str size */
|
||||
const char* fmt, /* in: format */
|
||||
...); /* in: format values */
|
||||
#else
|
||||
#define ut_snprintf snprintf
|
||||
#endif /* __WIN__ */
|
||||
|
||||
#ifndef UNIV_NONINL
|
||||
#include "ut0ut.ic"
|
||||
#endif
|
||||
|
|
44
ut/ut0ut.c
44
ut/ut0ut.c
|
@ -528,3 +528,47 @@ ut_copy_file(
|
|||
}
|
||||
} while (len > 0);
|
||||
}
|
||||
|
||||
/**************************************************************************
|
||||
snprintf(). */
|
||||
|
||||
#ifdef __WIN__
|
||||
#include <stdarg.h>
|
||||
int
|
||||
ut_snprintf(
|
||||
/* out: number of characters that would
|
||||
have been printed if the size were
|
||||
unlimited, not including the terminating
|
||||
'\0'. */
|
||||
char* str, /* out: string */
|
||||
size_t size, /* in: str size */
|
||||
const char* fmt, /* in: format */
|
||||
...) /* in: format values */
|
||||
{
|
||||
int res;
|
||||
va_list ap1;
|
||||
va_list ap2;
|
||||
|
||||
va_start(ap1, fmt);
|
||||
va_start(ap2, fmt);
|
||||
|
||||
res = _vscprintf(fmt, ap1);
|
||||
|
||||
if (res == -1) {
|
||||
return(-1);
|
||||
}
|
||||
|
||||
if (size > 0) {
|
||||
_vsnprintf(str, size, fmt, ap2);
|
||||
|
||||
if ((size_t)res >= size) {
|
||||
str[size - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
va_end(ap1);
|
||||
va_end(ap2);
|
||||
|
||||
return(res);
|
||||
}
|
||||
#endif /* __WIN__ */
|
||||
|
|
Loading…
Add table
Reference in a new issue