22 |
#include <assert.h> |
#include <assert.h> |
23 |
#include <stdio.h> |
#include <stdio.h> |
24 |
#include <stdarg.h> |
#include <stdarg.h> |
25 |
|
#include <errno.h> |
26 |
|
|
27 |
#include <windows.h> |
#include <windows.h> |
28 |
#include <wtsapi32.h> |
#include <wtsapi32.h> |
93 |
} |
} |
94 |
|
|
95 |
int |
int |
96 |
vchannel_read(char *buffer, size_t length) |
vchannel_read(char *line, size_t length) |
97 |
{ |
{ |
98 |
return -1; |
static BOOL overflow_mode = FALSE; |
99 |
|
static char buffer[VCHANNEL_MAX_LINE]; |
100 |
|
static size_t size = 0; |
101 |
|
|
102 |
|
char *newline; |
103 |
|
int line_size; |
104 |
|
|
105 |
|
BOOL result; |
106 |
|
ULONG bytes_read; |
107 |
|
|
108 |
|
result = WTSVirtualChannelRead(g_vchannel, 0, buffer + size, |
109 |
|
sizeof(buffer) - size, &bytes_read); |
110 |
|
|
111 |
|
if (!result) { |
112 |
|
errno = EIO; |
113 |
|
return -1; |
114 |
|
} |
115 |
|
|
116 |
|
if (overflow_mode) { |
117 |
|
newline = strchr(buffer, '\n'); |
118 |
|
if (newline && (newline - buffer) < bytes_read) { |
119 |
|
size = bytes_read - (newline - buffer) - 1; |
120 |
|
memmove(buffer, newline + 1, size); |
121 |
|
overflow_mode = FALSE; |
122 |
|
} |
123 |
|
} |
124 |
|
else |
125 |
|
size += bytes_read; |
126 |
|
|
127 |
|
if (overflow_mode) { |
128 |
|
errno = -EAGAIN; |
129 |
|
return -1; |
130 |
|
} |
131 |
|
|
132 |
|
newline = strchr(buffer, '\n'); |
133 |
|
if (!newline || (newline - buffer) >= size) { |
134 |
|
if (size == sizeof(buffer)) { |
135 |
|
overflow_mode = TRUE; |
136 |
|
size = 0; |
137 |
|
} |
138 |
|
errno = -EAGAIN; |
139 |
|
return -1; |
140 |
|
} |
141 |
|
|
142 |
|
if ((newline - buffer) >= length) { |
143 |
|
errno = ENOMEM; |
144 |
|
return -1; |
145 |
|
} |
146 |
|
|
147 |
|
*newline = '\0'; |
148 |
|
|
149 |
|
strcpy(line, buffer); |
150 |
|
line_size = newline - buffer; |
151 |
|
|
152 |
|
size -= newline - buffer + 1; |
153 |
|
memmove(buffer, newline + 1, size); |
154 |
|
|
155 |
|
return 0; |
156 |
} |
} |
157 |
|
|
158 |
int |
int |
160 |
{ |
{ |
161 |
BOOL result; |
BOOL result; |
162 |
va_list argp; |
va_list argp; |
163 |
char buf[1024]; |
char buf[VCHANNEL_MAX_LINE]; |
164 |
int size; |
int size; |
165 |
ULONG bytes_written; |
ULONG bytes_written; |
166 |
|
|