1 #define _SNPRINTF_DLLIMPORT __declspec( dllexport )
2 
3 #include <stdarg.h>
4 #include <stdio.h>
5 
6 #include <tchar.h>
7 #include <systools/win32/snprintf.h>
8 
9 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
10 #pragma warning(disable:4273)		// inconsistent dll linkage
11 #endif
12 
13 #if (defined(_MSC_VER) && (_MSC_VER < 1300)) || (defined(__MINGW32_VERSION) && ((__MINGW32_MAJOR_VERSION < 3)||((__MINGW32_MAJOR_VERSION == 3)&&(__MINGW32_MINOR_VERSION < 18))))
14 
15 /*	The non-debug versions of _vscprintf/_scprintf are just calls
16 	to _vsprintf/_sprintf with string buffer pointer set to NULL,
17 	requires MSVCRT version 7.0 */
18 #ifdef __MINGW32__
19 static int __cdecl _vsctprintf( const TCHAR *format, va_list ap )
20 #else
21 static int __cdecl _vsctprintf( const _TXCHAR *format, va_list ap )
22 #endif
23 {
24 	FILE	*fp = _tfopen( _T("NUL"), _T("wb") );
25 
26 	if ( fp )
27 	{
28 		int	retval = _vftprintf( fp, format, ap );
29 		fclose( fp );
30 
31 		return retval;
32 	}
33 
34 	return -1;
35 }
36 #endif
37 
38 /*	This function retrieves the pointer to the last character of a buffer.
39 	That is the pointer to the last character of the buffer that fits
40 	completly into that	buffer or the position of the terminating zero.
41 
42 	buffer	Pointer to a _TXCHAR buffer to be examined
43 	count	size of the buffer to be examined
44 
45 	return	The pointer to the last character that fits into the buffer or
46 			NULL if count is zero or count is one and the first byte was a
47 			leading	DBCS character
48 */
49 
50 static _TCHAR *GetLastBufferChar( _TCHAR *buffer, size_t count )
51 {
52 	_TCHAR	*last = NULL;
53 	_TCHAR	*cur = buffer;
54 
55 	while ( (size_t)(cur - buffer) < count )
56 	{
57 		last = cur;
58 
59 		if ( !*last )
60 			break;
61 
62 		cur = _tcsinc(last);
63 	}
64 
65 	return last;
66 }
67 
68 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */
69 
70 _SNPRINTF_DLLIMPORT int __cdecl vsntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, va_list list )
71 {
72 	int		retval;
73 
74 	/*	First of all call the existing non POSIX standard function assuming
75 		the buffer size will be large enough */
76 
77 	retval = _vsntprintf( buffer, count, format, list );
78 
79 	if ( retval < 0 )
80 	{
81 		/*	If the buffer wasn't large enough ensure that the buffer will be
82 			zero terminated */
83 
84 		_TCHAR	*last = GetLastBufferChar( buffer, count );
85 		if (last )
86 			*last = 0;
87 
88 		/*	Retrieve the count of characters that would have been written
89 			if the buffer were large enough */
90 
91 		retval = _vsctprintf( format, list );
92 	}
93 	else if ( (size_t)retval == count && count )
94 	{
95 		/*	If the buffer was large enough but not large enough for the trailing
96 			zero make the buffer zero terminated */
97 
98 		_TCHAR	*last = GetLastBufferChar( buffer, count );
99 		if (last )
100 			*last = 0;
101 	}
102 
103 	return retval;
104 }
105 
106 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */
107 
108 _SNPRINTF_DLLIMPORT int __cdecl sntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, ... )
109 {
110 	va_list	list;
111 	int		retval;
112 
113 	va_start( list, format );
114 	retval = vsntprintf( buffer, count, format, list );
115 	va_end( list );
116 
117 	return retval;
118 }
119