Merge branch 'master' of ctdb into 'master' of samba
[samba.git] / testprogs / win32 / npecho / npecho_client2.c
1 /*
2  * Simple Named Pipe Client
3  * (C) 2005 Jelmer Vernooij <jelmer@samba.org>
4  * (C) 2009 Stefan Metzmacher <metze@samba.org>
5  * Published to the public domain
6  */
7
8 #include <windows.h>
9 #include <stdio.h>
10
11 #define ECHODATA "Black Dog"
12
13 int main(int argc, char *argv[])
14 {
15         HANDLE h;
16         DWORD numread = 0;
17         char *outbuffer = malloc(sizeof(ECHODATA)*2);
18         BOOL small_reads = FALSE;
19         DWORD state = 0;
20         DWORD flags = 0;
21
22         if (argc == 1) {
23                 goto usage;
24         } else if (argc >= 3) {
25                 if (strcmp(argv[2], "large") == 0) {
26                         small_reads = FALSE;
27                 } else if (strcmp(argv[2], "small") == 0) {
28                         small_reads = TRUE;
29                 } else {
30                         goto usage;
31                 }
32         }
33
34         h = CreateFile(argv[1], GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
35         if (h == INVALID_HANDLE_VALUE) {
36                 printf("Error opening: %d\n", GetLastError());
37                 return -1;
38         }
39
40         GetNamedPipeHandleState(h, &state, NULL, NULL, NULL, NULL, 0);
41
42         if (state & PIPE_READMODE_MESSAGE) {
43                 printf("message read mode\n");
44         } else {
45                 printf("byte read mode\n");
46         }
47
48         Sleep(5000);
49
50         if (small_reads) {
51                 DWORD ofs, size, nread;
52                 const char *more = "";
53                 printf("small reads\n");
54                 numread = 0;
55                 ofs = 0;
56                 size = sizeof(ECHODATA)/2;
57                 if (ReadFile(h, outbuffer+ofs, size, &nread, NULL)) {
58                         if (state & PIPE_READMODE_MESSAGE) {
59                                 printf("Error message mode small read succeeded\n");
60                                 return -1;
61                         }
62                 } else if (GetLastError() == ERROR_MORE_DATA) {
63                         if (!(state & PIPE_READMODE_MESSAGE)) {
64                                 printf("Error byte mode small read returned ERROR_MORE_DATA\n");
65                                 return -1;
66                         }
67                         more = " more data";
68                 } else {
69                         printf("Error reading: %d\n", GetLastError());
70                         return -1;
71                 }
72                 printf("Small Read: %d%s\n", nread, more);
73                 numread += nread;
74                 ofs = size;
75                 size = sizeof(ECHODATA) - ofs;
76                 if (!ReadFile(h, outbuffer+ofs, size, &nread, NULL)) {
77                         printf("Error reading: %d\n", GetLastError());
78                         return -1;
79                 }
80                 printf("Small Read: %d\n", nread);
81                 numread += nread;
82         } else {
83                 printf("large read\n");
84                 if (!ReadFile(h, outbuffer, sizeof(ECHODATA)*2, &numread, NULL)) {
85                         printf("Error reading: %d\n", GetLastError());
86                         return -1;
87                 }
88         }
89         printf("Read: %s %d\n", outbuffer, numread);
90         if (state & PIPE_READMODE_MESSAGE) {
91                 if (numread != sizeof(ECHODATA)) {
92                         printf("message mode returned %d bytes should be %s\n",
93                                numread, sizeof(ECHODATA));
94                         return -1;
95                 }
96         } else {
97                 if (numread != (sizeof(ECHODATA)*2)) {
98                         printf("message mode returned %d bytes should be %s\n",
99                                numread, (sizeof(ECHODATA)*2));
100                         return -1;
101                 }
102         }
103
104         if (!ReadFile(h, outbuffer, sizeof(ECHODATA)*2, &numread, NULL)) {
105                 printf("Error reading: %d\n", GetLastError());
106                 return -1;
107         }
108
109         printf("Read: %s %d\n", outbuffer, numread);
110
111         return 0;
112 usage:
113         printf("Usage: %s pipename [read]\n", argv[0]);
114         printf("  Where pipename is something like \\\\servername\\NPECHO\n");
115         printf("  Where read is something 'large' or 'small'\n");
116         return -1;
117 }