1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #define _SNPRINTF_DLLIMPORT __declspec( dllexport )
23 
24 #include <stdarg.h>
25 #include <stdio.h>
26 
27 #include <tchar.h>
28 #include <systools/win32/snprintf.h>
29 
30 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
31 #pragma warning(disable:4273)		// inconsistent dll linkage
32 #endif
33 
34 #if (defined(_MSC_VER) && (_MSC_VER < 1300)) || (defined(__MINGW32_VERSION) && ((__MINGW32_MAJOR_VERSION < 3)||((__MINGW32_MAJOR_VERSION == 3)&&(__MINGW32_MINOR_VERSION < 18))))
35 
36 /*	The non-debug versions of _vscprintf/_scprintf are just calls
37 	to _vsprintf/_sprintf with string buffer pointer set to NULL,
38 	requires MSVCRT version 7.0 */
39 #ifdef __MINGW32__
_vsctprintf(const TCHAR * format,va_list ap)40 static int __cdecl _vsctprintf( const TCHAR *format, va_list ap )
41 #else
42 static int __cdecl _vsctprintf( const _TXCHAR *format, va_list ap )
43 #endif
44 {
45 	FILE	*fp = _tfopen( _T("NUL"), _T("wb") );
46 
47 	if ( fp )
48 	{
49 		int	retval = _vftprintf( fp, format, ap );
50 		fclose( fp );
51 
52 		return retval;
53 	}
54 
55 	return -1;
56 }
57 #endif
58 
59 /*	This function retrieves the pointer to the last character of a buffer.
60 	That is the pointer to the last character of the buffer that fits
61 	completly into that	buffer or the position of the terminating zero.
62 
63 	buffer	Pointer to a _TXCHAR buffer to be examined
64 	count	size of the buffer to be examined
65 
66 	return	The pointer to the last character that fits into the buffer or
67 			NULL if count is zero or count is one and the first byte was a
68 			leading	DBCS character
69 */
70 
GetLastBufferChar(_TCHAR * buffer,size_t count)71 static _TCHAR *GetLastBufferChar( _TCHAR *buffer, size_t count )
72 {
73 	_TCHAR	*last = NULL;
74 	_TCHAR	*cur = buffer;
75 
76 	while ( (size_t)(cur - buffer) < count )
77 	{
78 		last = cur;
79 
80 		if ( !*last )
81 			break;
82 
83 		cur = _tcsinc(last);
84 	}
85 
86 	return last;
87 }
88 
89 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */
90 
vsntprintf(_TCHAR * buffer,size_t count,const _TCHAR * format,va_list list)91 _SNPRINTF_DLLIMPORT int __cdecl vsntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, va_list list )
92 {
93 	int		retval;
94 
95 	/*	First of all call the existing non POSIX standard function assuming
96 		the buffer size will be large enough */
97 
98 	retval = _vsntprintf( buffer, count, format, list );
99 
100 	if ( retval < 0 )
101 	{
102 		/*	If the buffer wasn't large enough ensure that the buffer will be
103 			zero terminated */
104 
105 		_TCHAR	*last = GetLastBufferChar( buffer, count );
106 		if (last )
107 			*last = 0;
108 
109 		/*	Retrieve the count of characters that would have been written
110 			if the buffer were large enough */
111 
112 		retval = _vsctprintf( format, list );
113 	}
114 	else if ( (size_t)retval == count && count )
115 	{
116 		/*	If the buffer was large enough but not large enough for the trailing
117 			zero make the buffer zero terminated */
118 
119 		_TCHAR	*last = GetLastBufferChar( buffer, count );
120 		if (last )
121 			*last = 0;
122 	}
123 
124 	return retval;
125 }
126 
127 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */
128 
sntprintf(_TCHAR * buffer,size_t count,const _TCHAR * format,...)129 _SNPRINTF_DLLIMPORT int __cdecl sntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, ... )
130 {
131 	va_list	list;
132 	int		retval;
133 
134 	va_start( list, format );
135 	retval = vsntprintf( buffer, count, format, list );
136 	va_end( list );
137 
138 	return retval;
139 }
140