-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcatwin.c
More file actions
108 lines (87 loc) · 2.79 KB
/
catwin.c
File metadata and controls
108 lines (87 loc) · 2.79 KB
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
#include <stdio.h>
#include <Windows.h>
#include <tchar.h>
#include "arg.inl"
#define FILE_ERR ((HANDLE) -1)
void Process(HANDLE hInput, HANDLE hOutput)
{
BYTE buffer[1024];
while (TRUE)
{
DWORD dwBytesRead = 0;
BOOL fSuccess = ReadFile(hInput, buffer, (DWORD) ARRAYSIZE(buffer) * sizeof(BYTE), &dwBytesRead, NULL);
if (!fSuccess || dwBytesRead == 0)
break;
DWORD dwBytesWritten = 0;
fSuccess = WriteFile(hOutput, buffer, dwBytesRead, &dwBytesWritten, NULL);
if (!fSuccess)
break;
}
}
HANDLE argfile(int i, const TCHAR* descvalue, const TCHAR* desc)
{
const TCHAR* name = argnum(i, NULL, descvalue, desc);
if (name == NULL)
return NULL;
else if (_tcscmp(name, _T("-")) == 0)
return GetStdHandle(STD_INPUT_HANDLE);
else
{
HANDLE hPipe = NULL;
while (1)
{
hPipe = CreateFile(
name, // pipe name
GENERIC_READ,
0, // no sharing
NULL, // default security attributes
OPEN_EXISTING, // opens existing pipe
0, // default attributes
NULL); // no template file
// Break if the pipe handle is valid.
if (hPipe != INVALID_HANDLE_VALUE)
break;
// Exit if an error other than ERROR_PIPE_BUSY occurs.
if (GetLastError() != ERROR_PIPE_BUSY)
{
_ftprintf(stderr, TEXT("Error(%d): opening file\n"), GetLastError());
hPipe = FILE_ERR;
break;
}
// All pipe instances are busy, so wait for 20 seconds.
if (!WaitNamedPipe(name, 20000))
{
_ftprintf(stderr, TEXT("Error waiting for pipe: 20 second wait timed out."));
hPipe = FILE_ERR;
break;
}
}
return hPipe;
}
}
int _tmain(int argc, const TCHAR* const argv[])
{
arginit(argc, argv, _T("Read and output a file to stdout using windows file functions"));
HANDLE i = argfile(1, _T("file"), _T("The file to read ('-' for stdin)"));
HANDLE o = GetStdHandle(STD_OUTPUT_HANDLE);
if (!argcleanup())
return EXIT_FAILURE;
if (argusage(i == NULL))
return EXIT_SUCCESS;
if (i == FILE_ERR)
return EXIT_FAILURE;
int ret = EXIT_SUCCESS;
Process(i, o);
if (GetLastError() == ERROR_BROKEN_PIPE)
{
_fputts(TEXT("Client disconnected.\n"), stderr);
}
else
{
_ftprintf(stderr, TEXT("Process failed %d\n"), GetLastError());
ret = EXIT_FAILURE;
}
if (i != GetStdHandle(STD_INPUT_HANDLE))
CloseHandle(i);
return EXIT_SUCCESS;
}