-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathso_stdio 17NOV.cpp
510 lines (422 loc) · 11.1 KB
/
so_stdio 17NOV.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "so_stdio.h"
#include<stdio.h>
#define FILE_NULL -1
#define READ_OPERATION 100
#define WRITE_OPERATION 101
#define BUFSIZE 4096
#pragma warning (disable:4996)
//static int CheckFileValability(SO_FILE* stream) {
// if (stream == NULL)
// return FILE_NULL;
//}
HANDLE g_hChildStd_IN_Rd = NULL;
HANDLE g_hChildStd_IN_Wr = NULL;
HANDLE g_hChildStd_OUT_Rd = NULL;
HANDLE g_hChildStd_OUT_Wr = NULL;
HANDLE g_hInputFile = NULL;
static int Last_Operation = 0;
static int ReturnModeAcces(const char* mode) {
DWORD rMode = ERROR;
if (strncmp("r", mode, sizeof(mode)) == 0)
rMode = GENERIC_READ;
if (strncmp("r+", mode, sizeof(mode)) == 0)
rMode = (GENERIC_READ | GENERIC_WRITE);
if (strncmp("w", mode, sizeof(mode)) == 0)
rMode = GENERIC_WRITE;
if (strncmp("w+", mode, sizeof(mode)) == 0)
rMode = (GENERIC_READ | GENERIC_WRITE);
if (strncmp("a", mode, sizeof(mode)) == 0)
rMode = (GENERIC_READ | GENERIC_WRITE);
if (strncmp("a+", mode, sizeof(mode)) == 0)
rMode = (GENERIC_READ | GENERIC_WRITE);
return rMode;
}
static int soCreationDisposition(const char* mode) {
DWORD rAccesMode = ERROR;
if (strncmp("r", mode, sizeof(mode)) == 0)
rAccesMode = OPEN_EXISTING;
if (strncmp("r+", mode, sizeof(mode)) == 0)
rAccesMode = OPEN_EXISTING;
if (strncmp("w", mode, sizeof(mode)) == 0)
rAccesMode = OPEN_ALWAYS;
if (strncmp("w+", mode, sizeof(mode)) == 0)
rAccesMode = OPEN_ALWAYS;
if (strncmp("a", mode, sizeof(mode)) == 0)
rAccesMode = OPEN_ALWAYS;
if (strncmp("a+", mode, sizeof(mode)) == 0)
rAccesMode = OPEN_ALWAYS;
return rAccesMode;
}
static void SeekPosition(SO_FILE* stream, const char* mode) {
BOOL bRet;
if (strncmp("a", mode, sizeof(mode)) == 0 ||
strncmp("a+", mode, sizeof(mode)) == 0)
bRet = SetFilePointer(stream->so_handle,
0,
NULL,
FILE_END
);
}
static void CheckLastOperation(SO_FILE* stream, int CurentOperation) {
DWORD bytesWriten;
BOOL bRet;
if (CurentOperation != READ_OPERATION
&& Last_Operation == WRITE_OPERATION) {
bRet = WriteFile(stream->so_handle,
stream->buffer,
stream->bufSize,
&bytesWriten,
NULL);
if (bRet > 0) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
}
if (CurentOperation != WRITE_OPERATION
&& Last_Operation == READ_OPERATION)
{
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
}
static void CheckLastOperationV2(SO_FILE* stream) {
BOOL bRet;
DWORD bytesWriten;
if (Last_Operation == READ_OPERATION) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
if (Last_Operation == WRITE_OPERATION) {
bRet = WriteFile(stream->so_handle,
stream->buffer,
stream->bufSize,
&bytesWriten,
NULL);
if (bRet > 0) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
}
}
static void CheckLastOperationV3(SO_FILE* stream, int CurentOperation) {
DWORD bytesWriten;
BOOL bRet;
if (Last_Operation == READ_OPERATION &&
CurentOperation == WRITE_OPERATION) {
memset(stream->buffer, 0, stream->bufSize);
stream->bufSize = 0;
}
if (Last_Operation == WRITE_OPERATION &&
CurentOperation == READ_OPERATION) {
bRet = WriteFile(stream->so_handle,
stream->buffer,
stream->bufSize,
&bytesWriten,
NULL);
if (bRet > 0) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
}
Last_Operation = CurentOperation;
}
static char* ReadElement(SO_FILE* stream, size_t nmemb) {
char* rElement = (char*)malloc(nmemb * sizeof(char) + 1);
if (rElement != NULL) {
memset(rElement, 0, nmemb * sizeof(char) + 1);
if (stream->bufSize >= nmemb)
stream->bufSize -= nmemb;
else nmemb = stream->bufSize;
strncpy(rElement, stream->buffer, nmemb);
strcpy(stream->buffer, stream->buffer + nmemb);
}
rElement[strlen(rElement)] = '\0';
return rElement;
}
static VOID RedirectHandle(STARTUPINFO* psi, HANDLE hFile, INT opt)
{
if (hFile == INVALID_HANDLE_VALUE)
return;
/* TODO 1 - set handles from psi to
* current STDIN,STDOUT, STDERR handles
*/
psi->hStdInput = GetStdHandle(STD_INPUT_HANDLE);
psi->hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
psi->hStdError = GetStdHandle(STD_ERROR_HANDLE);
/* TODO 1 - Redirect one of STDIN, STDOUT, STDERR to hFile */
switch (opt) {
case STD_INPUT_HANDLE:
/* TODO 1 */
psi->hStdInput = hFile;
break;
case STD_OUTPUT_HANDLE:
/* TODO 1 */
psi->hStdOutput = hFile;
break;
case STD_ERROR_HANDLE:
/* TODO 1 */
psi->hStdError = hFile;
break;
}
}
static HANDLE MyOpenFile(PCSTR filename)
{
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa, sizeof(sa));
sa.bInheritHandle = TRUE;
return CreateFile(
filename,
GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
&sa,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
}
static VOID RunSimpleCommand(PCHAR command)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD dwRet;
BOOL bRet;
HANDLE hFile=INVALID_HANDLE_VALUE;
const char stdoutFilename[] = "pipe.txt";
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
/* Call my redirect function */
if (stdoutFilename != NULL) {
hFile = MyOpenFile(stdoutFilename);
RedirectHandle(&si, hFile, STD_OUTPUT_HANDLE);
}
si.dwFlags = STARTF_USESTDHANDLES;
bRet = CreateProcess(
NULL, /* No module name */
command, /* Command line */
NULL, /* Process handle not inheritable */
NULL, /* Thread handle not inheritable */
TRUE, /* Set handle inheritance */
0, /* No creation flags */
NULL, /* Use parent's environment block */
NULL, /* Use parent's starting directory */
&si, /* Pointer to STARTUPINFO structure */
&pi); /* Pointer to PROCESS_INFORMATION */
//if(bRet == FALSE)
dwRet = WaitForSingleObject(pi.hProcess, INFINITE);
if(dwRet == WAIT_FAILED)
printf( "WaitForSingleObject");
CloseHandle(pi.hProcess);
if (stdoutFilename != NULL) {
bRet = CloseHandle(hFile);
//DIE(bRet == FALSE, "CloseHandle");
}
}
static void WriteToPipe(void)
// Read from a file and write its contents to the pipe for the child's STDIN.
// Stop when there is no more data.
{
DWORD dwRead, dwWritten;
CHAR chBuf[BUFSIZE];
BOOL bSuccess = FALSE;
/*for (;;)
{
dwRead = 4096;
bSuccess = WriteFile(g_hChildStd_IN_Wr, chBuf, dwRead, &dwWritten, NULL);
if (!bSuccess) break;
}*/
// Close the pipe handle so the child process stops reading.
if (!CloseHandle(g_hChildStd_IN_Wr))
printf(("StdInWr CloseHandle"));
}
SO_FILE* so_fopen(const char* pathname, const char* mode) {
SO_FILE* retFile = (SO_FILE*)malloc(sizeof(SO_FILE));
if (retFile != NULL) {
retFile->so_handle = CreateFile(pathname,
ReturnModeAcces(mode),
0,
NULL,
soCreationDisposition(mode),
FILE_ATTRIBUTE_NORMAL,
NULL
);
memset(retFile->buffer, 0, sizeof(retFile->buffer));
retFile->bufSize = 0;
//retFile->buffer = (char*)malloc(SIZE_BUFFER * sizeof(char));
SeekPosition(retFile, mode);
}
return retFile;
}
int so_fclose(SO_FILE* stream) {
BOOL bRet;
DWORD bytesWriten;
if (Last_Operation == WRITE_OPERATION && stream->bufSize > 0) {
bRet = WriteFile(stream->so_handle,
stream->buffer,
stream->bufSize,
&bytesWriten,
NULL);
if (bRet > 0) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
}
if (stream != NULL) {
//
free(stream);
return 0;
}
return SO_EOF;
}
HANDLE so_fileno(SO_FILE* stream) {
if (stream != NULL)
return stream->so_handle;
else
return INVALID_HANDLE_VALUE;
}
int so_fflush(SO_FILE* stream)
{
BOOL bRet;
DWORD bytesWriten;
int rCode = 0;
if (Last_Operation == WRITE_OPERATION) {
bRet = WriteFile(stream->so_handle,
stream->buffer,
stream->bufSize,
&bytesWriten,
NULL);
if (bRet > 0) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
else rCode = SO_EOF;
}
return rCode;
}
int so_fseek(SO_FILE* stream, long offset, int whence)
{
CheckLastOperationV2(stream);
BOOL bRet = SetFilePointer(stream->so_handle,
offset,
NULL,
whence);
return bRet;
}
long so_ftell(SO_FILE* stream)
{
BOOL bRet = SetFilePointer(stream->so_handle,
0,
NULL,
SEEK_CUR);
return bRet;
}
size_t so_fread(void* ptr, size_t size, size_t nmemb, SO_FILE* stream)
{
Last_Operation = READ_OPERATION;
DWORD bytesRead;
BOOL bRet = 0;
size_t count = 0;
char* my_ptr = (char*)ptr;
for (int i = 0; i < nmemb; i++) {
int offset = i * size;
if (stream->bufSize == 0) {
bRet = ReadFile(stream->so_handle,
stream->buffer,
BUFFER_SIZE,
&bytesRead,
NULL);
stream->bufSize += bytesRead;
}
if (bRet < 0)
return 0;
char* element = ReadElement(stream, size);
if (strlen(element) >= size)
count++;
memcpy(my_ptr + offset, element, size);
if (strlen(element) == 0)
i = nmemb;
if (element != NULL)
free(element);
element = NULL;
}
//my_ptr[size] = '\0';
return count;
}
size_t so_fwrite(const void* ptr, size_t size, size_t nmemb, SO_FILE* stream)
{
char* my_ptr = (char*)ptr;
size_t count = 0;
for (int i = 0; i < nmemb * size; i++) {
so_fputc(my_ptr[i], stream);
if (count <= nmemb)
count++;
/*if(i % size == 0 && size != 1)
so_fputc(' ', stream);*/
}
return count;
}
int so_fgetc(SO_FILE* stream) {
CheckLastOperation(stream, READ_OPERATION);
char rChar = SO_EOF;
DWORD bytesRead;
BOOL bRet;
if (stream->bufSize == 0) {
bRet = ReadFile(stream->so_handle,
stream->buffer,
1, //1 bytes to read
&bytesRead,
NULL);
stream->bufSize += bytesRead;
}
if (stream->bufSize > 0) {
rChar = stream->buffer[0];
Last_Operation = READ_OPERATION;
strcpy_s(stream->buffer, sizeof(stream->buffer + 1), stream->buffer + 1);
stream->bufSize--;
}
else
rChar = SO_EOF;
return rChar;
}
int so_fputc(int c, SO_FILE* stream)
{
CheckLastOperationV3(stream, WRITE_OPERATION);
DWORD bytesWriten;
char rChar = SO_EOF;
BOOL bRet = 0;
if (stream->bufSize == BUFFER_SIZE - 1) {
bRet = WriteFile(stream->so_handle,
stream->buffer,
stream->bufSize,
&bytesWriten,
NULL);
}
else {
stream->buffer[stream->bufSize] = c;
stream->bufSize++;
Last_Operation = WRITE_OPERATION;
}
if (bRet > 0) {
stream->bufSize = 0;
memset(stream->buffer, 0, sizeof(stream->buffer));
}
return rChar;
}
int so_feof(SO_FILE* stream) {
int curetPosition = so_ftell(stream);
if (curetPosition == so_fseek(stream, 0, SEEK_END)) {
so_fseek(stream, curetPosition, SEEK_SET);
return 0;
}
return 1;
}
int so_ferror(SO_FILE* stream)
{
if (stream->buffer == INVALID_HANDLE_VALUE)
return 1;
return 0;
}
SO_FILE* so_popen(const char* command, const char* type)
{
RunSimpleCommand(PCHAR("cmd.exe /K dir"));
return nullptr;
}