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