merging configs
[oweals/gnunet.git] / src / util / w32cat.c
1 /*
2      W32 version of 'cat' program
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 \r
21 #include <stdio.h>\r
22 #include <windows.h>\r
23 \r
24 int\r
25 main (int argc, char **argv)\r
26 {\r
27   HANDLE stdi, stdo;\r
28   BOOL b;\r
29   wchar_t *commandlinew, **argvw;\r
30   int argcw;\r
31   int i;\r
32 \r
33   stdo = GetStdHandle (STD_OUTPUT_HANDLE);\r
34   if (stdo == INVALID_HANDLE_VALUE || stdo == NULL)\r
35     return 1;\r
36 \r
37   commandlinew = GetCommandLineW ();\r
38   argvw = CommandLineToArgvW (commandlinew, &argcw);\r
39   if (argvw == NULL)\r
40     return 1;\r
41 \r
42   for (i = 1; i < argcw || argcw == 1; i++)\r
43   {\r
44     DWORD r, w;\r
45     int is_dash = wcscmp (argvw[i], L"-") == 0;\r
46     if (argcw == 1 || is_dash)\r
47     {\r
48       stdi = GetStdHandle (STD_INPUT_HANDLE);\r
49       if (stdi == INVALID_HANDLE_VALUE)\r
50       {\r
51         fprintf (stderr, "cat: Failed to obtain stdin handle.\n");\r
52         return 4;\r
53       }\r
54       if (stdi == NULL)\r
55       {\r
56         fprintf (stderr, "cat: Have no stdin.\n");\r
57         return 5;\r
58       }\r
59     }\r
60     else\r
61     {\r
62       stdi = CreateFileW (argvw[i], GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);\r
63       if (stdi == INVALID_HANDLE_VALUE)\r
64       {\r
65         wchar_t *msgbuf;\r
66         DWORD le = GetLastError ();\r
67         if (0 < FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *) &msgbuf, 0, NULL))\r
68         {\r
69           fprintf (stderr, "cat: Failed to open file `%S'. Error %lu.\n", argvw[i], le);\r
70           return 3;\r
71         }\r
72         fprintf (stderr, "cat: Failed to open file `%S'. Error %lu: %S\n", argvw[i], le, msgbuf);\r
73         if (msgbuf != NULL)\r
74           LocalFree (msgbuf);\r
75         return 2;\r
76       }\r
77     }\r
78     do\r
79     {\r
80       unsigned char c;\r
81       b = ReadFile (stdi, &c, 1, &r, NULL);\r
82       if (b && r > 0)\r
83       {\r
84         b = WriteFile (stdo, &c, 1, &w, NULL);\r
85         if (b == 0)\r
86         {\r
87           wchar_t *msgbuf;\r
88           DWORD le = GetLastError ();\r
89           if (0 < FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *) &msgbuf, 0, NULL))\r
90           {\r
91             fprintf (stderr, "cat: Failed to write into stdout. Error %lu.\n", le);\r
92             return 3;\r
93           }\r
94           fprintf (stderr, "cat: Failed to write into stdout. Error %lu: %S\n", le, msgbuf);\r
95           if (msgbuf != NULL)\r
96             LocalFree (msgbuf);\r
97           return 6;\r
98         }\r
99       }\r
100     } while (b && r > 0);\r
101     if (argcw == 1)\r
102       break;\r
103     if (!is_dash)\r
104       CloseHandle (stdi);\r
105   }\r
106   LocalFree (argvw);\r
107   return 0;\r
108 }\r