2 W32 version of 'cat' program
5 cat is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 cat is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with cat; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
20 SPDX-License-Identifier: AGPL3.0-or-later
29 parent_control_thread(LPVOID lpParameter)
31 HANDLE h = (HANDLE)lpParameter;
38 b = ReadFile(h, &c, 1, &dw, NULL);
48 install_parent_control_handler()
55 env_buf = getenv("GNUNET_OS_CONTROL_PIPE");
56 if ((NULL == env_buf) || (strlen(env_buf) <= 0))
59 pipe_fd = strtoull(env_buf, &env_buf_end, 16);
60 if ((0 != errno) || (env_buf == env_buf_end))
62 /* Gcc will issue a warning here. What to do with it? */
63 pipe_handle = (HANDLE)(uintptr_t)pipe_fd;
64 CreateThread(NULL, 0, parent_control_thread, (LPVOID)pipe_handle, 0, NULL);
68 main(int argc, char **argv)
72 wchar_t *commandlinew, **argvw;
76 stdo = GetStdHandle(STD_OUTPUT_HANDLE);
77 if (stdo == INVALID_HANDLE_VALUE || stdo == NULL)
80 commandlinew = GetCommandLineW();
81 argvw = CommandLineToArgvW(commandlinew, &argcw);
85 install_parent_control_handler();
87 for (i = 1; i < argcw || argcw == 1; i++)
90 int is_dash = wcscmp(NULL == argvw[i] ? L"-" : argvw[i], L"-") == 0;
91 if (argcw == 1 || is_dash)
93 stdi = GetStdHandle(STD_INPUT_HANDLE);
94 if (stdi == INVALID_HANDLE_VALUE)
96 fprintf(stderr, "cat: Failed to obtain stdin handle.\n");
101 fprintf(stderr, "cat: Have no stdin.\n");
107 stdi = CreateFileW(argvw[i], GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
108 if (stdi == INVALID_HANDLE_VALUE)
111 DWORD le = GetLastError();
112 if (0 < FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *)&msgbuf, 0, NULL))
114 fprintf(stderr, "cat: Failed to open file `%S'. Error %lu.\n", argvw[i], le);
117 fprintf(stderr, "cat: Failed to open file `%S'. Error %lu: %S\n", argvw[i], le, msgbuf);
126 b = ReadFile(stdi, &c, 1, &r, NULL);
129 b = WriteFile(stdo, &c, 1, &w, NULL);
133 DWORD le = GetLastError();
134 if (0 < FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *)&msgbuf, 0, NULL))
136 fprintf(stderr, "cat: Failed to write into stdout. Error %lu.\n", le);
139 fprintf(stderr, "cat: Failed to write into stdout. Error %lu: %S\n", le, msgbuf);