1 | /* $Id: maybe_con_printf.c 2903 2016-09-09 17:38:50Z 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 | #include <stdio.h>
|
---|
40 | #ifdef _MSC_VER
|
---|
41 | # include <io.h>
|
---|
42 | # include <conio.h>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Drop-in printf replacement for optimizing console output on windows.
|
---|
49 | *
|
---|
50 | *
|
---|
51 | * @returns Number of bytes written.
|
---|
52 | * @param pszFormat The format string.
|
---|
53 | * @param ... The format arguments.
|
---|
54 | */
|
---|
55 | int maybe_con_printf(const char *pszFormat, ...)
|
---|
56 | {
|
---|
57 | va_list va;
|
---|
58 | int rc;
|
---|
59 |
|
---|
60 | #ifdef KBUILD_OS_WINDOWS
|
---|
61 | /*
|
---|
62 | * Just try format it into a stack buffer.
|
---|
63 | */
|
---|
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)
|
---|
70 | {
|
---|
71 | size_t cchRet = maybe_con_fwrite(szTmp, 1, rc, stdout);
|
---|
72 | return cchRet > 0 ? (int)cchRet : -1;
|
---|
73 | }
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | /*
|
---|
77 | * call the 'v' function.
|
---|
78 | */
|
---|
79 | va_start(va, pszFormat);
|
---|
80 | rc = vfprintf(stdout, pszFormat, va);
|
---|
81 | va_end(va);
|
---|
82 | return rc;
|
---|
83 | }
|
---|
84 |
|
---|