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 /* uwinapi implements C99 snprintf/vsnprintf because VC9's CRT has neither. 35 A CRT from VS2015 onwards supplies both itself, and defining ours on top of 36 its declarations is a redefinition with different linkage (C2375) -- the 37 same conflict systools/win32/snprintf.h avoids for the DECLARATIONS, here 38 for the DEFINITIONS. On such a runtime this file therefore contributes 39 nothing and the CRT's own implementations are used; on every runtime the 40 XP-era targets build against it is unchanged. Nothing is lost by leaving it 41 out: these symbols are exported only by Uwinapi.def (x86), never by 42 Uwinapi64.def, which exports SHCreateItemFromParsingName alone. */ 43 #if !defined(_MSC_VER) || (_MSC_VER < 1900) 44 45 #if (defined(_MSC_VER) && (_MSC_VER < 1300)) || (defined(__MINGW32_VERSION) && ((__MINGW32_MAJOR_VERSION < 3)||((__MINGW32_MAJOR_VERSION == 3)&&(__MINGW32_MINOR_VERSION < 18)))) 46 47 /* The non-debug versions of _vscprintf/_scprintf are just calls 48 to _vsprintf/_sprintf with string buffer pointer set to NULL, 49 requires MSVCRT version 7.0 */ 50 #ifdef __MINGW32__ 51 static int __cdecl _vsctprintf( const TCHAR *format, va_list ap ) 52 #else 53 static int __cdecl _vsctprintf( const _TXCHAR *format, va_list ap ) 54 #endif 55 { 56 FILE *fp = _tfopen( _T("NUL"), _T("wb") ); 57 58 if ( fp ) 59 { 60 int retval = _vftprintf( fp, format, ap ); 61 fclose( fp ); 62 63 return retval; 64 } 65 66 return -1; 67 } 68 #endif 69 70 /* This function retrieves the pointer to the last character of a buffer. 71 That is the pointer to the last character of the buffer that fits 72 completely into that buffer or the position of the terminating zero. 73 74 buffer Pointer to a _TXCHAR buffer to be examined 75 count size of the buffer to be examined 76 77 return The pointer to the last character that fits into the buffer or 78 NULL if count is zero or count is one and the first byte was a 79 leading DBCS character 80 */ 81 82 static _TCHAR *GetLastBufferChar( _TCHAR *buffer, size_t count ) 83 { 84 _TCHAR *last = NULL; 85 _TCHAR *cur = buffer; 86 87 while ( (size_t)(cur - buffer) < count ) 88 { 89 last = cur; 90 91 if ( !*last ) 92 break; 93 94 cur = _tcsinc(last); 95 } 96 97 return last; 98 } 99 100 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */ 101 102 _SNPRINTF_DLLIMPORT int __cdecl vsntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, va_list list ) 103 { 104 int retval; 105 106 /* First of all call the existing non POSIX standard function assuming 107 the buffer size will be large enough */ 108 109 retval = _vsntprintf( buffer, count, format, list ); 110 111 if ( retval < 0 ) 112 { 113 /* If the buffer wasn't large enough ensure that the buffer will be 114 zero terminated */ 115 116 _TCHAR *last = GetLastBufferChar( buffer, count ); 117 if (last ) 118 *last = 0; 119 120 /* Retrieve the count of characters that would have been written 121 if the buffer were large enough */ 122 123 retval = _vsctprintf( format, list ); 124 } 125 else if ( (size_t)retval == count && count ) 126 { 127 /* If the buffer was large enough but not large enough for the trailing 128 zero make the buffer zero terminated */ 129 130 _TCHAR *last = GetLastBufferChar( buffer, count ); 131 if (last ) 132 *last = 0; 133 } 134 135 return retval; 136 } 137 138 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */ 139 140 _SNPRINTF_DLLIMPORT int __cdecl sntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, ... ) 141 { 142 va_list list; 143 int retval; 144 145 va_start( list, format ); 146 retval = vsntprintf( buffer, count, format, list ); 147 va_end( list ); 148 149 return retval; 150 } 151 152 #endif /* CRT without a conforming snprintf */ 153