VirtualBox

source: kBuild/trunk/src/lib/maybe_con_write.c

Last change on this file was 3547, checked in by bird, 3 years ago

lib: Changed the console-optimization wrappers to use the get_crt_codepage() function as that's more appropriate than GetConsoleCP() for use with _cputws(). Also made them avoid the heap for smaller writes that could fit into a reasonable (2KiB) stack buffer.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1/* $Id: maybe_con_write.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 <[email protected]>
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>
42typedef unsigned int to_write_t;
43#else
44typedef size_t to_write_t;
45#endif
46
47
48/**
49 * Drop-in write replacement for optimizing console output on windows.
50 *
51 * @returns Number of bytes written, -1 + errno on failure.
52 * @param fd The file descript to write to.
53 * @param pvBuf What to write.
54 * @param cbToWrite How much to write.
55 */
56ssize_t maybe_con_write(int fd, void const *pvBuf, size_t cbToWrite)
57{
58 ssize_t cbWritten;
59
60#ifdef KBUILD_OS_WINDOWS
61 /*
62 * If it's a TTY, do our own conversion to wide char and
63 * call WriteConsoleW directly.
64 */
65 if (cbToWrite > 0 && cbToWrite < INT_MAX / 2)
66 {
67 HANDLE hCon = (HANDLE)_get_osfhandle(fd);
68 if ( hCon != INVALID_HANDLE_VALUE
69 && hCon != NULL)
70 {
71 if (is_console_handle((intptr_t)hCon))
72 {
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]))
78 {
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));
89 if (cwcToWrite > 0)
90 {
91 int rc;
92 pawcBuf[cwcToWrite] = '\0';
93
94 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT
95 sources indicates _cputws will do the right thing. */
96 rc = _cputws(pawcBuf);
97 if (pawcBufFree)
98 free(pawcBufFree);
99 if (rc >= 0)
100 return cbToWrite;
101 return -1;
102 }
103 free(pawcBufFree);
104 }
105 }
106 }
107 }
108#endif
109
110 /*
111 * Semi regular write handling.
112 */
113 cbWritten = write(fd, pvBuf, (to_write_t)cbToWrite);
114 if (cbWritten == (ssize_t)cbToWrite)
115 { /* likely */ }
116 else if (cbWritten >= 0 || errno == EINTR)
117 {
118 if (cbWritten < 0)
119 cbWritten = 0;
120 while (cbWritten < (ssize_t)cbToWrite)
121 {
122 ssize_t cbThis = write(fd, (char *)pvBuf + cbWritten, (to_write_t)(cbToWrite - cbWritten));
123 if (cbThis >= 0)
124 cbWritten += cbThis;
125 else if (errno != EINTR)
126 return -1;
127 }
128 }
129 return cbWritten;
130}
131
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette