-LRN: Wincat:
authorChristian Grothoff <christian@grothoff.org>
Sat, 23 Jun 2012 22:33:52 +0000 (22:33 +0000)
committerChristian Grothoff <christian@grothoff.org>
Sat, 23 Jun 2012 22:33:52 +0000 (22:33 +0000)
  I've been experimenting with an alternative non-MSYS buildsystem for
a while, and GNUNET_os_start_process() test kept failing due to lack
of cat. Wincat is a minimal cat implementation in pure WinAPI. It's
not named "cat.exe" to avoid clashing with MSYS cat (which can lead to
some very weird issues).

src/util/Makefile.am
src/util/test_os_start_process.c
src/util/w32cat.c [new file with mode: 0644]

index 5426953fcd607f5363a3456c875f9e1f4c93e99e..3be3062d0abf3df103b828be8bb7f487ab522d83 100644 (file)
@@ -22,6 +22,7 @@ libgnunetutilwin_la_LIBADD = \
   -lshell32 -liconv -lstdc++ \
   -lcomdlg32 -lgdi32 -liphlpapi
 WINLIB = libgnunetutilwin.la
+W32CAT = w32cat
 endif
 
 if !MINGW
@@ -33,8 +34,11 @@ if USE_COVERAGE
   XLIB = -lgcov
 endif
 
+cat_SOURCES = w32cat.c
+
 noinst_PROGRAMS = \
  gnunet-config-diff \
+ $(W32CAT) \
  test_common_logging_dummy
 
 
@@ -224,6 +228,8 @@ test_os_start_process_SOURCES = \
  test_os_start_process.c
 test_os_start_process_LDADD = \
  $(top_builddir)/src/util/libgnunetutil.la
+test_os_start_process_DEPENDENCIES = \
+ $(WINCAT)
 
 test_client_SOURCES = \
  test_client.c
index 5b9973250ac93eb89b7d0fa33c006aaa8a4cebe4..3c30c75f3ed86f8d80c4dddb316bed471e540d24 100644 (file)
@@ -113,7 +113,11 @@ run_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   const struct GNUNET_DISK_FileHandle *stdout_read_handle;
   const struct GNUNET_DISK_FileHandle *wh;
 
+#if !WINDOWS
   GNUNET_asprintf (&fn, "cat");
+#else
+  GNUNET_asprintf (&fn, "w32cat");
+#endif
 
   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
@@ -183,6 +187,12 @@ check_run ()
 static int
 check_kill ()
 {
+  char *fn;
+#if !WINDOWS
+  GNUNET_asprintf (&fn, "cat");
+#else
+  GNUNET_asprintf (&fn, "w32cat");
+#endif
   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
@@ -190,7 +200,7 @@ check_kill ()
     return 1;
   }
   proc =
-    GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, "cat",
+    GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, fn,
                             "gnunet-service-resolver", "-", NULL); 
   sleep (1); /* give process time to start, so we actually use the pipe-kill mechanism! */
   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
@@ -210,6 +220,12 @@ check_kill ()
 static int
 check_instant_kill ()
 {
+  char *fn;
+#if !WINDOWS
+  GNUNET_asprintf (&fn, "cat");
+#else
+  GNUNET_asprintf (&fn, "w32cat");
+#endif
   hello_pipe_stdin = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_YES, GNUNET_NO);
   hello_pipe_stdout = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
   if ((hello_pipe_stdout == NULL) || (hello_pipe_stdin == NULL))
@@ -217,7 +233,7 @@ check_instant_kill ()
     return 1;
   }
   proc =
-    GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, "cat",
+    GNUNET_OS_start_process (GNUNET_YES, hello_pipe_stdin, hello_pipe_stdout, fn,
                             "gnunet-service-resolver", "-", NULL); 
   if (0 != GNUNET_OS_process_kill (proc, SIGTERM))
   {
diff --git a/src/util/w32cat.c b/src/util/w32cat.c
new file mode 100644 (file)
index 0000000..0c5091c
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+     W32 version of 'cat' program
+     (C) 2012 LRN
+
+     cat is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 3, or (at your
+     option) any later version.
+
+     cat is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with cat; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+\r
+#include <stdio.h>\r
+#include <windows.h>\r
+\r
+int\r
+main (int argc, char **argv)\r
+{\r
+  HANDLE stdi, stdo;\r
+  BOOL b;\r
+  wchar_t *commandlinew, **argvw;\r
+  int argcw;\r
+  int i;\r
+\r
+  stdo = GetStdHandle (STD_OUTPUT_HANDLE);\r
+  if (stdo == INVALID_HANDLE_VALUE || stdo == NULL)\r
+    return 1;\r
+\r
+  commandlinew = GetCommandLineW ();\r
+  argvw = CommandLineToArgvW (commandlinew, &argcw);\r
+  if (argvw == NULL)\r
+    return 1;\r
+\r
+  for (i = 1; i < argcw || argcw == 1; i++)\r
+  {\r
+    DWORD r, w;\r
+    int is_dash = wcscmp (argvw[i], L"-") == 0;\r
+    if (argcw == 1 || is_dash)\r
+    {\r
+      stdi = GetStdHandle (STD_INPUT_HANDLE);\r
+      if (stdi == INVALID_HANDLE_VALUE)\r
+      {\r
+        fprintf (stderr, "cat: Failed to obtain stdin handle.\n");\r
+        return 4;\r
+      }\r
+      if (stdi == NULL)\r
+      {\r
+        fprintf (stderr, "cat: Have no stdin.\n");\r
+        return 5;\r
+      }\r
+    }\r
+    else\r
+    {\r
+      stdi = CreateFileW (argvw[i], GENERIC_READ, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);\r
+      if (stdi == INVALID_HANDLE_VALUE)\r
+      {\r
+        wchar_t *msgbuf;\r
+        DWORD le = GetLastError ();\r
+        if (0 < FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *) &msgbuf, 0, NULL))\r
+        {\r
+          fprintf (stderr, "cat: Failed to open file `%S'. Error %lu.\n", argvw[i], le);\r
+          return 3;\r
+        }\r
+        fprintf (stderr, "cat: Failed to open file `%S'. Error %lu: %S\n", argvw[i], le, msgbuf);\r
+        if (msgbuf != NULL)\r
+          LocalFree (msgbuf);\r
+        return 2;\r
+      }\r
+    }\r
+    do\r
+    {\r
+      unsigned char c;\r
+      b = ReadFile (stdi, &c, 1, &r, NULL);\r
+      if (r > 0)\r
+      {\r
+        b = WriteFile (stdo, &c, 1, &w, NULL);\r
+        if (b == 0)\r
+        {\r
+          wchar_t *msgbuf;\r
+          DWORD le = GetLastError ();\r
+          if (0 < FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, 0, le, 0, (wchar_t *) &msgbuf, 0, NULL))\r
+          {\r
+            fprintf (stderr, "cat: Failed to write into stdout. Error %lu.\n", le);\r
+            return 3;\r
+          }\r
+          fprintf (stderr, "cat: Failed to write into stdout. Error %lu: %S\n", le, msgbuf);\r
+          if (msgbuf != NULL)\r
+            LocalFree (msgbuf);\r
+          return 6;\r
+        }\r
+      }\r
+    } while (b && r > 0);\r
+    if (argcw == 1)\r
+      break;\r
+    if (!is_dash)\r
+      CloseHandle (stdi);\r
+  }\r
+  LocalFree (argvw);\r
+  return 0;\r
+}\r