Changeset 3635 in kBuild
- Timestamp:
- Nov 2, 2024 1:52:02 AM (4 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/msc_buffered_printf.c
r3547 r3635 42 42 #include "console.h" 43 43 44 #undef printf45 #undef vprintf46 #undef fprintf47 #undef puts48 #undef fputs49 44 #pragma warning(disable: 4273) /* inconsistent dll linkage*/ 50 45 … … 56 51 57 52 53 54 #if _MSC_VER >= 1400 55 typedef int (__cdecl *PFN_STDIO_COMMON_VFPRINTF_T)(unsigned __int64, FILE *, const char *, _locale_t, va_list); 56 57 static PFN_STDIO_COMMON_VFPRINTF_T g_pfnFallback_vfprintf = NULL; 58 59 DLL_IMPORT 60 int __cdecl __stdio_common_vfprintf(unsigned __int64 fOptions, FILE *pFile, const char *pszFormat, _locale_t hLocale, va_list va) 61 { 62 /* 63 * Make sure we've got the fallback function before we start. 64 */ 65 PFN_STDIO_COMMON_VFPRINTF_T pfnFallback = g_pfnFallback_vfprintf; 66 if (g_pfnFallback_vfprintf) 67 { /* likely */ } 68 else 69 { 70 HANDLE hmodCrt = GetModuleHandleW(L"api-ms-win-crt-stdio-l1-1-0.dll"); 71 if (!hmodCrt) 72 { 73 hmodCrt = GetModuleHandleW(L"ucrtbase.dll"); 74 if (!hmodCrt) 75 { 76 hmodCrt = LoadLibraryW(L"api-ms-win-crt-stdio-l1-1-0.dll"); 77 if (!hmodCrt) 78 { 79 static char const s_szMsg[] = "fatal error! Failed to load the UCRT DLL and therefore no __stdio_common_vfprintf fallback!\r\n"; 80 DWORD cbIgn = 0; 81 WriteFile(GetStdHandle(STD_ERROR_HANDLE ), s_szMsg, sizeof(s_szMsg) - 1, &cbIgn, NULL); 82 TerminateProcess(GetCurrentProcess(), 998); 83 } 84 } 85 } 86 87 pfnFallback = (PFN_STDIO_COMMON_VFPRINTF_T)GetProcAddress(hmodCrt, "__stdio_common_vfprintf"); 88 if (!pfnFallback) 89 { 90 static char const s_szMsg[] = "fatal error! Failed resolve __stdio_common_vfprintf in the UCRT DLL!\r\n"; 91 DWORD cbIgn = 0; 92 WriteFile(GetStdHandle(STD_ERROR_HANDLE ), s_szMsg, sizeof(s_szMsg) - 1, &cbIgn, NULL); 93 TerminateProcess(GetCurrentProcess(), 997); 94 } 95 96 g_pfnFallback_vfprintf = pfnFallback; 97 } 98 99 /* 100 * If it's a TTY, try format into a stack buffer and output using our 101 * console optimized fwrite wrapper. 102 */ 103 if (*pszFormat != '\0') 104 { 105 if (hLocale == NULL) 106 { 107 int fd = fileno(pFile); 108 if (fd >= 0) 109 { 110 if (is_console(fd)) 111 { 112 char *pszTmp = (char *)alloca(16384); 113 va_list va2 = va; 114 int cchRet = vsnprintf(pszTmp, 16384, pszFormat, va2); 115 if (cchRet < 16384 - 1) 116 return (int)maybe_con_fwrite(pszTmp, cchRet, 1, stdout); 117 } 118 } 119 } 120 } 121 122 /* 123 * Fallback. 124 */ 125 return g_pfnFallback_vfprintf(fOptions, pFile, pszFormat, hLocale, va); 126 } 127 128 129 void * const __imp___stdio_common_vfprintf = (void *)(uintptr_t)__stdio_common_vfprintf; 130 131 132 #else /* < _MSC_VER <= 1400 */ 133 134 # undef printf 135 # undef vprintf 136 # undef fprintf 137 # undef puts 138 # undef fputs 58 139 59 140 /** … … 265 346 void * const __imp_fputs = (void *)(uintptr_t)fputs; 266 347 348 #endif /* Old MSC */
Note:
See TracChangeset
for help on using the changeset viewer.