Debian Codegraph @

src/win32_cgreen_pipe.c

raw symbols SCIP
1 #ifdef WIN32
2
3 #include <stdio.h>
4 #include <winsock2.h>
5 #include "cgreen/internal/windows_headers/wincompat.h"
6 #include "cgreen/internal/cgreen_pipe.h"
7
8 #include "win32_cgreen.h"
9
10
11 ssize_t cgreen_pipe_read(int p, void *buf, size_t count)
12 {
13 DWORD bytesRead;
14
15 ReadFile((HANDLE)p, buf, (DWORD)count, &bytesRead, NULL);
16
17 return bytesRead;
18 }
19
20 ssize_t cgreen_pipe_write(int p, const void *buf, size_t count)
21 {
22 DWORD bytesWritten;
23
24 WriteFile((HANDLE)p, buf, (DWORD)count, &bytesWritten, NULL);
25
26 if (bytesWritten != count)
27 {
28 printf("failed to write %d bytes - error = %d\n",count,GetLastError());
29 }
30
31 return bytesWritten;
32 }
33
34 int cgreen_pipe_open(int pipes[2])
35 {
36 SECURITY_ATTRIBUTES saAttr = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE};
37 char handleString[256];
38 DWORD result;
39
40 // Figure out if we are a child process, and if so, use the handle provided by our parent
41 result = GetEnvironmentVariableA(CGREEN_READ_HANDLE,handleString,sizeof(handleString));
42
43 if (result) //we are a child process
44 {
45 pipes[0] = (int)atoi(handleString);
46 result = GetEnvironmentVariableA(CGREEN_WRITE_HANDLE,handleString,sizeof(handleString));
47 pipes[1] = (int)atoi(handleString);
48
49 //now that we have retrieved these handles, clear the environment variables so
50 //that subsequent calls to this function will create new pipes
51 //(This is needed for unit tests "will_report_beginning_and_successful_finishing_of_test"
52 //and "will_report_failing_of_test_only_once")
53 SetEnvironmentVariableA(CGREEN_READ_HANDLE,NULL);
54 SetEnvironmentVariableA(CGREEN_WRITE_HANDLE,NULL);
55 }
56 else
57 {
58 HANDLE readpipe,writepipe;
59 DWORD mode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
60 const DWORD DEFAULT_BUFFER_SIZE = 0;
61
62 if (!CreatePipe(&readpipe, &writepipe, &saAttr, DEFAULT_BUFFER_SIZE))
63 return -1;
64
65 //turn on NOWAIT
66 if (!SetNamedPipeHandleState(readpipe,&mode,NULL,NULL))
67 {
68 fprintf(stderr, "could not set file status flag on read pipe\n");
69 return -1;
70 }
71
72 pipes[0] = (int)readpipe;
73 pipes[1] = (int)writepipe;
74 }
75
76 return 0;
77 }
78
79 void cgreen_pipe_close(int p)
80 {
81 CloseHandle((HANDLE)p);
82 }
83
84 #endif
85 /* vim: set ts=4 sw=4 et cindent: */