Changeset 2903 in kBuild
- Timestamp:
- Sep 9, 2016 5:38:50 PM (9 years ago)
- Location:
- trunk/src/lib
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/Makefile.kmk
r2900 r2903 41 41 maybe_con_write.c \ 42 42 maybe_con_fwrite.c \ 43 maybe_con_printf.c \ 43 44 kbuild_version.c 44 45 kUtil_SOURCES.win = \ -
trunk/src/lib/maybe_con_printf.c
r2900 r2903 46 46 47 47 /** 48 * Drop-in fwritereplacement for optimizing console output on windows.48 * Drop-in printf replacement for optimizing console output on windows. 49 49 * 50 50 * 51 * @returns Units written; 0 & errno on failure. 52 * @param pvBuf What to write. 53 * @param cbUnit How much to write in each unit. 54 * @param cUnits How many units to write. 55 * @param pFile The file to write to. 51 * @returns Number of bytes written. 52 * @param pszFormat The format string. 53 * @param ... The format arguments. 56 54 */ 57 size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile)55 int maybe_con_printf(const char *pszFormat, ...) 58 56 { 57 va_list va; 58 int rc; 59 59 60 #ifdef KBUILD_OS_WINDOWS 60 61 /* 61 * If it's a TTY, do our own conversion to wide char and 62 * call WriteConsoleW directly. 62 * Just try format it into a stack buffer. 63 63 */ 64 if ( cbUnit > 0 65 && cUnits > 0 66 && (pFile == stdout || pFile == stderr)) 64 extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *); 65 char szTmp[16384]; 66 va_start(va, pszFormat); 67 rc = vsnprintf(szTmp, sizeof(szTmp), pszFormat, va); 68 va_end(va); 69 if (rc > 0) 67 70 { 68 int fd = fileno(pFile); 69 if (fd >= 0) 70 { 71 if (isatty(fd)) 72 { 73 HANDLE hCon = (HANDLE)_get_osfhandle(fd); 74 if ( hCon != INVALID_HANDLE_VALUE 75 && hCon != NULL) 76 { 77 size_t cbToWrite = cbUnit * cUnits; 78 size_t cwcTmp = cbToWrite * 2 + 16; 79 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t)); 80 if (pawcTmp) 81 { 82 int cwcToWrite; 83 static UINT s_uConsoleCp = 0; 84 if (s_uConsoleCp == 0) 85 s_uConsoleCp = GetConsoleCP(); 86 87 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1)); 88 if (cwcToWrite > 0) 89 { 90 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT 91 sources indicates _cputws will do the right thing we want. */ 92 pawcTmp[cwcToWrite] = '\0'; 93 fflush(pFile); 94 if (_cputws(pawcTmp) >= 0) 95 return cUnits; 96 return 0; 97 } 98 } 99 } 100 } 101 } 71 size_t cchRet = maybe_con_fwrite(szTmp, 1, rc, stdout); 72 return cchRet > 0 ? (int)cchRet : -1; 102 73 } 103 74 #endif 104 75 105 76 /* 106 * Semi regular write handling.77 * call the 'v' function. 107 78 */ 108 return fwrite(pvBuf, cbUnit, cUnits, pFile); 79 va_start(va, pszFormat); 80 rc = vfprintf(stdout, pszFormat, va); 81 va_end(va); 82 return rc; 109 83 } 84
Note:
See TracChangeset
for help on using the changeset viewer.