| 1 | /* $Id: maybe_con_fwrite.c 3188 2018-03-24 15:32:26Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * maybe_con_write - Optimized console output on windows.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2016-2018 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
|---|
| 10 | * copy of this software and associated documentation files (the "Software"),
|
|---|
| 11 | * to deal in the Software without restriction, including without limitation
|
|---|
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|---|
| 13 | * and/or sell copies of the Software, and to permit persons to whom the
|
|---|
| 14 | * Software is furnished to do so, subject to the following conditions:
|
|---|
| 15 | *
|
|---|
| 16 | * The above copyright notice and this permission notice shall be included
|
|---|
| 17 | * in all copies or substantial portions of the Software.
|
|---|
| 18 | *
|
|---|
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|---|
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|---|
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|---|
| 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|---|
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|---|
| 24 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|---|
| 25 | * IN THE SOFTWARE.
|
|---|
| 26 | *
|
|---|
| 27 | * Alternatively, the content of this file may be used under the terms of the
|
|---|
| 28 | * GPL version 2 or later, or LGPL version 2.1 or later.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | /*********************************************************************************************************************************
|
|---|
| 33 | * Header Files *
|
|---|
| 34 | *********************************************************************************************************************************/
|
|---|
| 35 | #include "console.h"
|
|---|
| 36 | #ifdef KBUILD_OS_WINDOWS
|
|---|
| 37 | # include <windows.h>
|
|---|
| 38 | #endif
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #ifdef _MSC_VER
|
|---|
| 41 | # include <conio.h>
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | /**
|
|---|
| 46 | * Drop-in fwrite replacement for optimizing console output on windows.
|
|---|
| 47 | *
|
|---|
| 48 | *
|
|---|
| 49 | * @returns Units written; 0 & errno on failure.
|
|---|
| 50 | * @param pvBuf What to write.
|
|---|
| 51 | * @param cbUnit How much to write in each unit.
|
|---|
| 52 | * @param cUnits How many units to write.
|
|---|
| 53 | * @param pFile The file to write to.
|
|---|
| 54 | */
|
|---|
| 55 | size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile)
|
|---|
| 56 | {
|
|---|
| 57 | #ifdef KBUILD_OS_WINDOWS
|
|---|
| 58 | /*
|
|---|
| 59 | * If it's a TTY, do our own conversion to wide char and
|
|---|
| 60 | * call WriteConsoleW directly.
|
|---|
| 61 | */
|
|---|
| 62 | if ( cbUnit > 0
|
|---|
| 63 | && cUnits > 0
|
|---|
| 64 | && (pFile == stdout || pFile == stderr))
|
|---|
| 65 | {
|
|---|
| 66 | int fd = fileno(pFile);
|
|---|
| 67 | if (fd >= 0)
|
|---|
| 68 | {
|
|---|
| 69 | HANDLE hCon = (HANDLE)_get_osfhandle(fd);
|
|---|
| 70 | if ( hCon != INVALID_HANDLE_VALUE
|
|---|
| 71 | && hCon != NULL)
|
|---|
| 72 | {
|
|---|
| 73 | if (is_console_handle((intptr_t)hCon))
|
|---|
| 74 | {
|
|---|
| 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)
|
|---|
| 79 | {
|
|---|
| 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));
|
|---|
| 87 | if (cwcToWrite > 0)
|
|---|
| 88 | {
|
|---|
| 89 | int rc;
|
|---|
| 90 | pawcTmp[cwcToWrite] = '\0';
|
|---|
| 91 |
|
|---|
| 92 | /* Let the CRT do the rest. At least the Visual C++ 2010 CRT
|
|---|
| 93 | sources indicates _cputws will do the right thing. */
|
|---|
| 94 | fflush(pFile);
|
|---|
| 95 | rc = _cputws(pawcTmp);
|
|---|
| 96 | free(pawcTmp);
|
|---|
| 97 | if (rc >= 0)
|
|---|
| 98 | return cUnits;
|
|---|
| 99 | return 0;
|
|---|
| 100 | }
|
|---|
| 101 | free(pawcTmp);
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | #endif
|
|---|
| 108 |
|
|---|
| 109 | /*
|
|---|
| 110 | * Semi regular write handling.
|
|---|
| 111 | */
|
|---|
| 112 | return fwrite(pvBuf, cbUnit, cUnits, pFile);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|