aboutsummaryrefslogtreecommitdiff
path: root/src/util/w32cat.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/w32cat.c')
-rw-r--r--src/util/w32cat.c166
1 files changed, 84 insertions, 82 deletions
diff --git a/src/util/w32cat.c b/src/util/w32cat.c
index 8a48638aa..fa7637d38 100644
--- a/src/util/w32cat.c
+++ b/src/util/w32cat.c
@@ -18,7 +18,7 @@
18 Boston, MA 02110-1301, USA. 18 Boston, MA 02110-1301, USA.
19 19
20 SPDX-License-Identifier: AGPL3.0-or-later 20 SPDX-License-Identifier: AGPL3.0-or-later
21*/ 21 */
22 22
23#include <stdio.h> 23#include <stdio.h>
24#include <windows.h> 24#include <windows.h>
@@ -26,45 +26,46 @@
26#include <signal.h> 26#include <signal.h>
27 27
28DWORD WINAPI 28DWORD WINAPI
29parent_control_thread (LPVOID lpParameter) 29parent_control_thread(LPVOID lpParameter)
30{ 30{
31 HANDLE h = (HANDLE) lpParameter; 31 HANDLE h = (HANDLE)lpParameter;
32
32 while (TRUE) 33 while (TRUE)
33 {
34 DWORD dw;
35 BOOL b;
36 unsigned char c;
37 b = ReadFile (h, &c, 1, &dw, NULL);
38 if (!b)
39 { 34 {
40 ExitProcess (0); 35 DWORD dw;
36 BOOL b;
37 unsigned char c;
38 b = ReadFile(h, &c, 1, &dw, NULL);
39 if (!b)
40 {
41 ExitProcess(0);
42 }
43 raise((int)c);
41 } 44 }
42 raise ((int) c);
43 }
44} 45}
45 46
46void 47void
47install_parent_control_handler () 48install_parent_control_handler()
48{ 49{
49 const char *env_buf; 50 const char *env_buf;
50 char *env_buf_end; 51 char *env_buf_end;
51 uint64_t pipe_fd; 52 uint64_t pipe_fd;
52 HANDLE pipe_handle; 53 HANDLE pipe_handle;
53 54
54 env_buf = getenv ("GNUNET_OS_CONTROL_PIPE"); 55 env_buf = getenv("GNUNET_OS_CONTROL_PIPE");
55 if ( (NULL == env_buf) || (strlen (env_buf) <= 0) ) 56 if ((NULL == env_buf) || (strlen(env_buf) <= 0))
56 return; 57 return;
57 errno = 0; 58 errno = 0;
58 pipe_fd = strtoull (env_buf, &env_buf_end, 16); 59 pipe_fd = strtoull(env_buf, &env_buf_end, 16);
59 if ((0 != errno) || (env_buf == env_buf_end)) 60 if ((0 != errno) || (env_buf == env_buf_end))
60 return; 61 return;
61 /* Gcc will issue a warning here. What to do with it? */ 62 /* Gcc will issue a warning here. What to do with it? */
62 pipe_handle = (HANDLE) (uintptr_t) pipe_fd; 63 pipe_handle = (HANDLE)(uintptr_t)pipe_fd;
63 CreateThread (NULL, 0, parent_control_thread, (LPVOID) pipe_handle, 0, NULL); 64 CreateThread(NULL, 0, parent_control_thread, (LPVOID)pipe_handle, 0, NULL);
64} 65}
65 66
66int 67int
67main (int argc, char **argv) 68main(int argc, char **argv)
68{ 69{
69 HANDLE stdi, stdo; 70 HANDLE stdi, stdo;
70 BOOL b; 71 BOOL b;
@@ -72,81 +73,82 @@ main (int argc, char **argv)
72 int argcw; 73 int argcw;
73 int i; 74 int i;
74 75
75 stdo = GetStdHandle (STD_OUTPUT_HANDLE); 76 stdo = GetStdHandle(STD_OUTPUT_HANDLE);
76 if (stdo == INVALID_HANDLE_VALUE || stdo == NULL) 77 if (stdo == INVALID_HANDLE_VALUE || stdo == NULL)
77 return 1; 78 return 1;
78 79
79 commandlinew = GetCommandLineW (); 80 commandlinew = GetCommandLineW();
80 argvw = CommandLineToArgvW (commandlinew, &argcw); 81 argvw = CommandLineToArgvW(commandlinew, &argcw);
81 if (argvw == NULL) 82 if (argvw == NULL)
82 return 1; 83 return 1;
83 84
84 install_parent_control_handler (); 85 install_parent_control_handler();
85 86
86 for (i = 1; i < argcw || argcw == 1; i++) 87 for (i = 1; i < argcw || argcw == 1; i++)
87 {
88 DWORD r, w;
89 int is_dash = wcscmp (NULL == argvw[i] ? L"-" : argvw[i], L"-") == 0;
90 if (argcw == 1 || is_dash)
91 {
92 stdi = GetStdHandle (STD_INPUT_HANDLE);
93 if (stdi == INVALID_HANDLE_VALUE)
94 {
95 fprintf (stderr, "cat: Failed to obtain stdin handle.\n");
96 return 4;
97 }
98 if (stdi == NULL)
99 {
100 fprintf (stderr, "cat: Have no stdin.\n");
101 return 5;
102 }
103 }
104 else
105 { 88 {
106 stdi = CreateFileW (argvw[i], GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); 89 DWORD r, w;
107 if (stdi == INVALID_HANDLE_VALUE) 90 int is_dash = wcscmp(NULL == argvw[i] ? L"-" : argvw[i], L"-") == 0;
108 { 91 if (argcw == 1 || is_dash)
109 wchar_t *msgbuf;
110 DWORD le = GetLastError ();
111 if (0 < FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *) &msgbuf, 0, NULL))
112 { 92 {
113 fprintf (stderr, "cat: Failed to open file `%S'. Error %lu.\n", argvw[i], le); 93 stdi = GetStdHandle(STD_INPUT_HANDLE);
114 return 3; 94 if (stdi == INVALID_HANDLE_VALUE)
95 {
96 fprintf(stderr, "cat: Failed to obtain stdin handle.\n");
97 return 4;
98 }
99 if (stdi == NULL)
100 {
101 fprintf(stderr, "cat: Have no stdin.\n");
102 return 5;
103 }
115 } 104 }
116 fprintf (stderr, "cat: Failed to open file `%S'. Error %lu: %S\n", argvw[i], le, msgbuf); 105 else
117 if (msgbuf != NULL)
118 LocalFree (msgbuf);
119 return 2;
120 }
121 }
122 do
123 {
124 unsigned char c;
125 b = ReadFile (stdi, &c, 1, &r, NULL);
126 if (b && r > 0)
127 {
128 b = WriteFile (stdo, &c, 1, &w, NULL);
129 if (b == 0)
130 { 106 {
131 wchar_t *msgbuf; 107 stdi = CreateFileW(argvw[i], GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
132 DWORD le = GetLastError (); 108 if (stdi == INVALID_HANDLE_VALUE)
133 if (0 < FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *) &msgbuf, 0, NULL)) 109 {
134 { 110 wchar_t *msgbuf;
135 fprintf (stderr, "cat: Failed to write into stdout. Error %lu.\n", le); 111 DWORD le = GetLastError();
136 return 3; 112 if (0 < FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *)&msgbuf, 0, NULL))
137 } 113 {
138 fprintf (stderr, "cat: Failed to write into stdout. Error %lu: %S\n", le, msgbuf); 114 fprintf(stderr, "cat: Failed to open file `%S'. Error %lu.\n", argvw[i], le);
139 if (msgbuf != NULL) 115 return 3;
140 LocalFree (msgbuf); 116 }
141 return 6; 117 fprintf(stderr, "cat: Failed to open file `%S'. Error %lu: %S\n", argvw[i], le, msgbuf);
118 if (msgbuf != NULL)
119 LocalFree(msgbuf);
120 return 2;
121 }
142 } 122 }
143 } 123 do
144 } while (b && r > 0); 124 {
145 if (argcw == 1) 125 unsigned char c;
146 break; 126 b = ReadFile(stdi, &c, 1, &r, NULL);
147 if (!is_dash) 127 if (b && r > 0)
148 CloseHandle (stdi); 128 {
149 } 129 b = WriteFile(stdo, &c, 1, &w, NULL);
150 LocalFree (argvw); 130 if (b == 0)
131 {
132 wchar_t *msgbuf;
133 DWORD le = GetLastError();
134 if (0 < FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *)&msgbuf, 0, NULL))
135 {
136 fprintf(stderr, "cat: Failed to write into stdout. Error %lu.\n", le);
137 return 3;
138 }
139 fprintf(stderr, "cat: Failed to write into stdout. Error %lu: %S\n", le, msgbuf);
140 if (msgbuf != NULL)
141 LocalFree(msgbuf);
142 return 6;
143 }
144 }
145 }
146 while (b && r > 0);
147 if (argcw == 1)
148 break;
149 if (!is_dash)
150 CloseHandle(stdi);
151 }
152 LocalFree(argvw);
151 return 0; 153 return 0;
152} 154}