VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/win/nocrt-streams-win.cpp@ 95084

Last change on this file since 95084 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
1/* $Id: nocrt-streams-win.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - No-CRT - minimal stream implementation
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32#include <iprt/stream.h>
33
34#include <iprt/nt/nt-and-windows.h>
35
36#include <iprt/ctype.h>
37#include <iprt/string.h>
38
39
40
41/*********************************************************************************************************************************
42* Structures and Typedefs *
43*********************************************************************************************************************************/
44typedef struct PRINTFBUF
45{
46 HANDLE hHandle;
47 size_t offBuf;
48 char szBuf[128];
49} PRINTFBUF;
50
51struct RTSTREAM
52{
53 int iStream;
54 HANDLE hHandle;
55};
56
57
58/*********************************************************************************************************************************
59* Defined Constants And Macros *
60*********************************************************************************************************************************/
61RTSTREAM g_aStdStreams[3] =
62{
63 { 0, NULL },
64 { 1, NULL },
65 { 2, NULL },
66};
67
68RTSTREAM *g_pStdIn = &g_aStdStreams[0];
69RTSTREAM *g_pStdOut = &g_aStdStreams[1];
70RTSTREAM *g_pStdErr = &g_aStdStreams[2];
71
72
73
74DECLHIDDEN(void) InitStdHandles(PRTL_USER_PROCESS_PARAMETERS pParams)
75{
76 if (pParams)
77 {
78 g_pStdIn->hHandle = pParams->StandardInput;
79 g_pStdOut->hHandle = pParams->StandardOutput;
80 g_pStdErr->hHandle = pParams->StandardError;
81 }
82}
83
84
85static void FlushPrintfBuffer(PRINTFBUF *pBuf)
86{
87 if (pBuf->offBuf)
88 {
89 DWORD cbWritten = 0;
90 WriteFile(pBuf->hHandle, pBuf->szBuf, (DWORD)pBuf->offBuf, &cbWritten, NULL);
91 pBuf->offBuf = 0;
92 pBuf->szBuf[0] = '\0';
93 }
94}
95
96
97/** @callback_method_impl{FNRTSTROUTPUT} */
98static DECLCALLBACK(size_t) MyPrintfOutputter(void *pvArg, const char *pachChars, size_t cbChars)
99{
100 PRINTFBUF *pBuf = (PRINTFBUF *)pvArg;
101 if (cbChars != 0)
102 {
103 size_t offSrc = 0;
104 while (offSrc < cbChars)
105 {
106 size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
107 if (cbLeft > 0)
108 {
109 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
110 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
111 pBuf->offBuf += cbToCopy;
112 pBuf->szBuf[pBuf->offBuf] = '\0';
113 if (cbLeft > cbToCopy)
114 break;
115 offSrc += cbToCopy;
116 }
117 FlushPrintfBuffer(pBuf);
118 }
119 }
120 else /* Special zero byte write at the end of the formatting. */
121 FlushPrintfBuffer(pBuf);
122 return cbChars;
123}
124
125
126RTR3DECL(int) RTStrmPrintfV(PRTSTREAM pStream, const char *pszFormat, va_list args)
127{
128 PRINTFBUF Buf;
129 Buf.hHandle = pStream->hHandle;
130 Buf.offBuf = 0;
131 Buf.szBuf[0] = '\0';
132
133 return (int)RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, args);
134}
135
136
137RTR3DECL(int) RTStrmPrintf(PRTSTREAM pStream, const char *pszFormat, ...)
138{
139 va_list args;
140 va_start(args, pszFormat);
141 int rc = RTStrmPrintfV(pStream, pszFormat, args);
142 va_end(args);
143 return rc;
144}
145
146
147RTR3DECL(int) RTPrintfV(const char *pszFormat, va_list va)
148{
149 PRINTFBUF Buf;
150 Buf.hHandle = g_pStdOut->hHandle;
151 Buf.offBuf = 0;
152 Buf.szBuf[0] = '\0';
153
154 return (int)RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, va);
155}
156
157
158RTR3DECL(int) RTPrintf(const char *pszFormat, ...)
159{
160 va_list va;
161 va_start(va, pszFormat);
162 int rc = RTPrintfV(pszFormat, va);
163 va_end(va);
164 return rc;
165}
166
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