- Timestamp:
- Jan 29, 2022 2:39:47 AM (3 years ago)
- Location:
- trunk/src/lib
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/console.h
r3188 r3547 41 41 # include <unistd.h> 42 42 #endif 43 #ifdef KBUILD_OS_WINDOWS 44 # include "get_codepage.h" 45 #endif 43 46 44 47 … … 49 52 extern ssize_t maybe_con_write(int fd, void const *pvBuf, size_t cbToWrite); 50 53 extern size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile); 51 52 54 #endif 53 55 -
trunk/src/lib/maybe_con_fwrite.c
r3188 r3547 57 57 #ifdef KBUILD_OS_WINDOWS 58 58 /* 59 * If it's a TTY, do our own conversion to wide char and 60 * call WriteConsoleW directly. 59 * If it's a TTY, do our own conversion to wide char and call _cputws. 61 60 */ 62 61 if ( cbUnit > 0 63 62 && cUnits > 0 63 && cbUnit < (unsigned)INT_MAX / 4 64 && cUnits < (unsigned)INT_MAX / 4 64 65 && (pFile == stdout || pFile == stderr)) 65 66 { … … 73 74 if (is_console_handle((intptr_t)hCon)) 74 75 { 75 size_t cbToWrite = cbUnit * cUnits; 76 size_t cwcTmp = cbToWrite * 2 + 16; 77 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t)); 78 if (pawcTmp) 76 /* Use a stack buffer if we can, falling back on the heap for larger writes: */ 77 wchar_t awcBuf[1024]; 78 wchar_t *pawcBuf; 79 wchar_t *pawcBufFree = NULL; 80 size_t cbToWrite = cbUnit * cUnits; 81 size_t cwcBuf = cbToWrite * 2 + 16; 82 if (cwcBuf < sizeof(awcBuf) / sizeof(awcBuf[0])) 79 83 { 80 int cwcToWrite; 81 static UINT s_uConsoleCp = 0; 82 if (s_uConsoleCp == 0) 83 s_uConsoleCp = GetConsoleCP(); 84 85 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, 86 pawcTmp, (int)(cwcTmp - 1)); 84 pawcBuf = awcBuf; 85 cwcBuf = sizeof(awcBuf) / sizeof(awcBuf[0]); 86 } 87 else 88 pawcBufFree = pawcBuf = (wchar_t *)malloc(cwcBuf * sizeof(wchar_t)); 89 if (pawcBuf) 90 { 91 int cwcToWrite = MultiByteToWideChar(get_crt_codepage(), 0 /*dwFlags*/, 92 pvBuf, (int)cbToWrite, 93 pawcBuf, (int)(cwcBuf - 1)); 87 94 if (cwcToWrite > 0) 88 95 { 89 96 int rc; 90 pawc Tmp[cwcToWrite] = '\0';97 pawcBuf[cwcToWrite] = '\0'; 91 98 92 99 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT 93 100 sources indicates _cputws will do the right thing. */ 94 101 fflush(pFile); 95 rc = _cputws(pawcTmp); 96 free(pawcTmp); 102 rc = _cputws(pawcBuf); 103 if (pawcBufFree) 104 free(pawcBufFree); 97 105 if (rc >= 0) 98 106 return cUnits; 99 107 return 0; 100 108 } 101 free(pawc Tmp);109 free(pawcBufFree); 102 110 } 103 111 } -
trunk/src/lib/maybe_con_write.c
r3188 r3547 63 63 * call WriteConsoleW directly. 64 64 */ 65 if (cbToWrite > 0 )65 if (cbToWrite > 0 && cbToWrite < INT_MAX / 2) 66 66 { 67 67 HANDLE hCon = (HANDLE)_get_osfhandle(fd); … … 71 71 if (is_console_handle((intptr_t)hCon)) 72 72 { 73 size_t cwcTmp = cbToWrite * 2 + 16; 74 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t)); 75 if (pawcTmp) 73 wchar_t awcBuf[1024]; 74 wchar_t *pawcBuf; 75 wchar_t *pawcBufFree = NULL; 76 size_t cwcBuf = cbToWrite * 2 + 16; 77 if (cwcBuf < sizeof(awcBuf) / sizeof(awcBuf[0])) 76 78 { 77 int cwcToWrite; 78 static UINT s_uConsoleCp = 0; 79 if (s_uConsoleCp == 0) 80 s_uConsoleCp = GetConsoleCP(); 81 82 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, 83 pawcTmp, (int)(cwcTmp - 1)); 79 pawcBuf = awcBuf; 80 cwcBuf = sizeof(awcBuf) / sizeof(awcBuf[0]); 81 } 82 else 83 pawcBufFree = pawcBuf = (wchar_t *)malloc(cwcBuf * sizeof(wchar_t)); 84 if (pawcBuf) 85 { 86 int cwcToWrite = MultiByteToWideChar(get_crt_codepage(), 0 /*dwFlags*/, 87 pvBuf, (int)cbToWrite, 88 pawcBuf, (int)(cwcBuf - 1)); 84 89 if (cwcToWrite > 0) 85 90 { 91 int rc; 92 pawcBuf[cwcToWrite] = '\0'; 93 86 94 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT 87 95 sources indicates _cputws will do the right thing. */ 88 pawcTmp[cwcToWrite] = '\0'; 89 if (_cputws(pawcTmp) >= 0) 96 rc = _cputws(pawcBuf); 97 if (pawcBufFree) 98 free(pawcBufFree); 99 if (rc >= 0) 90 100 return cbToWrite; 91 101 return -1; 92 102 } 103 free(pawcBufFree); 93 104 } 94 105 } -
trunk/src/lib/msc_buffered_printf.c
r3188 r3547 39 39 #include <conio.h> 40 40 #include <malloc.h> 41 #include <locale.h> 41 42 #include "console.h" 42 43 … … 169 170 int __cdecl puts(const char *pszString) 170 171 { 171 size_t cchString = strlen(pszString);172 size_t cch;173 174 172 /* 175 173 * If it's a TTY, we convert it to a wide char string with a newline … … 177 175 * buffering due to the added newline. 178 176 */ 179 if (*pszString != '\0') 177 size_t cchString = strlen(pszString); 178 size_t cch; 179 if (cchString > 0 && cchString < INT_MAX / 2) 180 180 { 181 181 int fd = fileno(stdout); … … 188 188 && hCon != NULL) 189 189 { 190 /* We need to append a newline, so we can just as well do the conversion here. */ 191 size_t cwcTmp = cchString * 2 + 16 + 2; 192 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t)); 193 if (pawcTmp) 190 wchar_t awcBuf[1024]; 191 wchar_t *pawcBuf; 192 wchar_t *pawcBufFree = NULL; 193 size_t cwcBuf = cchString * 2 + 16 + 1; /* +1 for added newline */ 194 if (cwcBuf < sizeof(awcBuf) / sizeof(awcBuf[0])) 194 195 { 195 int cwcToWrite; 196 static UINT s_uConsoleCp = 0; 197 if (s_uConsoleCp == 0) 198 s_uConsoleCp = GetConsoleCP(); 199 200 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pszString, (int)cchString, pawcTmp, 201 (int)(cwcTmp - 2)); 196 pawcBuf = awcBuf; 197 cwcBuf = sizeof(awcBuf) / sizeof(awcBuf[0]); 198 } 199 else 200 pawcBufFree = pawcBuf = (wchar_t *)malloc(cwcBuf * sizeof(wchar_t)); 201 if (pawcBuf) 202 { 203 int cwcToWrite = MultiByteToWideChar(get_crt_codepage(), 0 /*dwFlags*/, 204 pszString, (int)cchString, 205 pawcBuf, (int)(cwcBuf - 1)); 202 206 if (cwcToWrite > 0) 203 207 { 204 208 int rc; 205 206 pawcTmp[cwcToWrite++] = '\n'; 207 pawcTmp[cwcToWrite] = '\0'; 209 pawcBuf[cwcToWrite++] = '\n'; 210 pawcBuf[cwcToWrite] = '\0'; 208 211 209 212 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT 210 sources indicates _cputws will do the right thing we want. */213 sources indicates _cputws will do the right thing. */ 211 214 fflush(stdout); 212 rc = _cputws(pawcTmp); 213 free(pawcTmp); 214 return rc; 215 rc = _cputws(pawcBuf); 216 if (pawcBufFree) 217 free(pawcBufFree); 218 if (rc >= 0) 219 return 0; 220 return -1; 215 221 } 216 free(pawc Tmp);222 free(pawcBufFree); 217 223 } 218 224 }
Note:
See TracChangeset
for help on using the changeset viewer.