4 // Martin Fuchs, 30.11.2003
6 // Jan Roeloffzen, 26.1.2010
7 // Pipe client, based on msdn example
10 #define WIN32_LEAN_AND_MEAN
15 #define PIPEREAD_VERSION "0.3"
16 #define PIPEREAD_NOPIPE (-101)
18 // This definition currently missing in MinGW.
19 #ifndef FILE_FLAG_FIRST_PIPE_INSTANCE
20 #define FILE_FLAG_FIRST_PIPE_INSTANCE 0x00080000
26 static void print_error(DWORD win32_error
)
28 fprintf(stderr
, "WIN32 error %lu\n", win32_error
);
31 static int pipeServer(char *path
)
33 HANDLE hPipe
= CreateNamedPipe(path
, PIPE_ACCESS_DUPLEX
|FILE_FLAG_FIRST_PIPE_INSTANCE
, PIPE_WAIT
|PIPE_TYPE_BYTE
, 1, 4096, 4096, 30000, NULL
);
35 if (hPipe
== INVALID_HANDLE_VALUE
) {
36 print_error(GetLastError());
44 if (!ReadFile(hPipe
, buffer
, sizeof(buffer
), &read
, NULL
)) {
45 DWORD error
= GetLastError();
47 if (error
== ERROR_PIPE_LISTENING
) {
49 } else if (error
== ERROR_BROKEN_PIPE
) {
52 hPipe
= CreateNamedPipe(path
, PIPE_ACCESS_DUPLEX
|FILE_FLAG_FIRST_PIPE_INSTANCE
, PIPE_WAIT
|PIPE_TYPE_BYTE
, 1, 4096, 4096, 30000, NULL
);
54 if (hPipe
== INVALID_HANDLE_VALUE
) {
55 fprintf(stderr
,"INVALID_HANDLE_VALUE\n");
56 print_error(GetLastError());
60 fprintf(stderr
,"error %lu\n",error
);
67 fwrite(buffer
, read
, 1, stdout
);
70 if (!CloseHandle(hPipe
))
71 print_error(GetLastError());
77 static int pipeClient(char *path
)
79 HANDLE hPipe
=INVALID_HANDLE_VALUE
;
81 BOOL fSuccess
= FALSE
;
86 setvbuf(stdout
, NULL
, _IONBF
, 0);
88 hPipe
= CreateFile(path
, // pipe name
91 NULL
, // default security attributes
92 OPEN_EXISTING
, // opens existing pipe
93 0, // default attributes
94 NULL
); // no template file
96 // Break if the pipe handle is valid.
97 if (hPipe
!= INVALID_HANDLE_VALUE
)
100 // Exit if an error other than ERROR_PIPE_BUSY occurs.
101 Err
= GetLastError();
102 if (Err
!= ERROR_PIPE_BUSY
) {
103 if (ERROR_FILE_NOT_FOUND
== Err
)
105 res
= PIPEREAD_NOPIPE
;
110 fprintf(stderr
,"Could not open pipe %s. Error=%lu\n", path
, Err
);
116 // All pipe instances are busy, so wait for 20 seconds.
117 if ( ! WaitNamedPipe(path
, 20000)) {
118 fprintf(stderr
,"Could not open pipe: 20 second wait timed out.");
125 fSuccess
= ReadFile(hPipe
, // pipe handle
126 chBuf
, // buffer to receive reply
127 BUFSIZE
, // size of buffer
128 &cbRead
, // number of bytes read
129 NULL
); // not overlapped
132 Err
= GetLastError();
133 if ( Err
== ERROR_MORE_DATA
) {
136 fprintf(stderr
, "ReadFile: Error %lu \n", Err
);
142 fwrite(chBuf
,1,cbRead
,stdout
);
146 fprintf(stderr
, "ReadFile from pipe failed. Error=%lu\n", GetLastError() );
149 if (hPipe
!= INVALID_HANDLE_VALUE
)
156 static int fileClient(const char *path
)
162 setvbuf(stdout
, NULL
, _IONBF
, 0);
163 if (!(fin
= fopen(path
, "r"))) {
164 fprintf(stderr
,"Could not fopen %s (%s)\n", path
, strerror(errno
) );
168 while ((c
= fgetc(fin
)) != EOF
) {
178 fprintf(stderr
, "piperead " PIPEREAD_VERSION
"\n\n");
179 fprintf(stderr
, "Usage: piperead [-c] <named pipe>\n");
180 fprintf(stderr
, "-c means Client mode\n");
181 fprintf(stderr
, "Example: piperead -c \\\\.\\pipe\\kdbg | log2lines -c\n\n");
184 int main(int argc
, char** argv
)
187 const char* pipe_name
;
188 const char* clientMode
;
195 clientMode
= *++argv
;
196 if (strcmp(clientMode
,"-c") != 0) {
197 fprintf(stderr
,"Invalid option: %s\n", clientMode
);
204 if (strcmp(pipe_name
,"-h") == 0) {
217 if ( pipe_name
[0] == '\\' ) {
218 //assume caller specified full path
219 sprintf(path
, "%s", pipe_name
);
221 sprintf(path
, "\\\\.\\pipe\\%s", pipe_name
);
225 res
= pipeClient(path
);
226 if (res
== PIPEREAD_NOPIPE
) {
227 res
= fileClient(pipe_name
);
230 res
= pipeServer(path
);