Changeset 3547 for trunk/src/lib/msc_buffered_printf.c
- Timestamp:
- Jan 29, 2022, 3:39:47 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.