1 | /* $Id: maybe_con_fwrite.c 3547 2022-01-29 02:39:47Z 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 call _cputws.
|
---|
60 | */
|
---|
61 | if ( cbUnit > 0
|
---|
62 | && cUnits > 0
|
---|
63 | && cbUnit < (unsigned)INT_MAX / 4
|
---|
64 | && cUnits < (unsigned)INT_MAX / 4
|
---|
65 | && (pFile == stdout || pFile == stderr))
|
---|
66 | {
|
---|
67 | int fd = fileno(pFile);
|
---|
68 | if (fd >= 0)
|
---|
69 | {
|
---|
70 | HANDLE hCon = (HANDLE)_get_osfhandle(fd);
|
---|
71 | if ( hCon != INVALID_HANDLE_VALUE
|
---|
72 | && hCon != NULL)
|
---|
73 | {
|
---|
74 | if (is_console_handle((intptr_t)hCon))
|
---|
75 | {
|
---|
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]))
|
---|
83 | {
|
---|
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));
|
---|
94 | if (cwcToWrite > 0)
|
---|
95 | {
|
---|
96 | int rc;
|
---|
97 | pawcBuf[cwcToWrite] = '\0';
|
---|
98 |
|
---|
99 | /* Let the CRT do the rest. At least the Visual C++ 2010 CRT
|
---|
100 | sources indicates _cputws will do the right thing. */
|
---|
101 | fflush(pFile);
|
---|
102 | rc = _cputws(pawcBuf);
|
---|
103 | if (pawcBufFree)
|
---|
104 | free(pawcBufFree);
|
---|
105 | if (rc >= 0)
|
---|
106 | return cUnits;
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 | free(pawcBufFree);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * Semi regular write handling.
|
---|
119 | */
|
---|
120 | return fwrite(pvBuf, cbUnit, cUnits, pFile);
|
---|
121 | }
|
---|
122 |
|
---|