VirtualBox

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

Last change on this file since 3007 was 2900, checked in by bird, 8 years ago

output optimizations

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1/* $Id: maybe_con_write.c 2900 2016-09-09 14:42:06Z bird $ */
2/** @file
3 * maybe_con_write - Optimized console output on windows.
4 */
5
6/*
7 * Copyright (c) 2016 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#ifdef KBUILD_OS_WINDOWS
36# include <windows.h>
37#endif
38#include <errno.h>
39#ifdef _MSC_VER
40# include <io.h>
41# include <conio.h>
42typedef intptr_t ssize_t;
43typedef unsigned int to_write_t;
44#else
45# include <unistd.h>
46typedef size_t to_write_t;
47#endif
48
49
50/**
51 * Drop-in write replacement for optimizing console output on windows.
52 *
53 * @returns Number of bytes written, -1 + errno on failure.
54 * @param fd The file descript to write to.
55 * @param pvBuf What to write.
56 * @param cbToWrite How much to write.
57 */
58ssize_t maybe_con_write(int fd, void *pvBuf, size_t cbToWrite)
59{
60 ssize_t cbWritten;
61#ifdef KBUILD_OS_WINDOWS
62 /*
63 * If it's a TTY, do our own conversion to wide char and
64 * call WriteConsoleW directly.
65 */
66 if (cbToWrite > 0 && isatty(fd))
67 {
68 HANDLE hCon = (HANDLE)_get_osfhandle(fd);
69 if ( hCon != INVALID_HANDLE_VALUE
70 && hCon != NULL)
71 {
72 size_t cwcTmp = cbToWrite * 2 + 16;
73 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
74 if (pawcTmp)
75 {
76 int cwcToWrite;
77 static UINT s_uConsoleCp = 0;
78 if (s_uConsoleCp == 0)
79 s_uConsoleCp = GetConsoleCP();
80
81 cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1));
82 if (cwcToWrite > 0)
83 {
84 /* Let the CRT do the rest. At least the Visual C++ 2010 CRT
85 sources indicates _cputws will do the right thing we want. */
86 pawcTmp[cwcToWrite] = '\0';
87 if (_cputws(pawcTmp) >= 0)
88 return cbToWrite;
89 return -1;
90 }
91 }
92 }
93 }
94#endif
95
96 /*
97 * Semi regular write handling.
98 */
99 cbWritten = write(fd, pvBuf, (to_write_t)cbToWrite);
100 if (cbWritten == cbToWrite)
101 { /* likely */ }
102 else if (cbWritten >= 0 || errno == EINTR)
103 {
104 if (cbWritten < 0)
105 cbWritten = 0;
106 while (cbWritten < (ssize_t)cbToWrite)
107 {
108 ssize_t cbThis = write(fd, (char *)pvBuf + cbWritten, (to_write_t)(cbToWrite - cbWritten));
109 if (cbThis >= 0)
110 cbWritten += cbThis;
111 else if (errno != EINTR)
112 return -1;
113 }
114 }
115 return cbWritten;
116}
Note: See TracBrowser for help on using the repository browser.

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