spawn-test-win32-gui.c 1.96 KB
Newer Older
1
#include <windows.h>
2
#include <io.h>
3
#include <errno.h>
4 5 6 7 8
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

9 10 11 12 13 14 15 16
#ifdef __CYGWIN__
/* For read() and write() */
#include <unistd.h>
/* Cygwin does not prototype __argc and __argv in stdlib.h */
extern int __argc;
extern char** __argv;
#endif

17 18 19 20 21 22
int _stdcall
WinMain (struct HINSTANCE__ *hInstance,
	struct HINSTANCE__ *hPrevInstance,
	char *lpszCmdLine,
	int   nCmdShow)
{
23
  if (__argc >= 2 && strcmp (__argv[1], "print_argv0") == 0)
24
    {
25
      printf ("%s", __argv[0]);
26 27
    }
  else if (__argc <= 2)
28 29 30 31 32 33 34 35 36 37 38 39
    {
      printf ("This is stdout\n");
      fflush (stdout);
      
      fprintf (stderr, "This is stderr\n");
      fflush (stderr);
    }
  else if (__argc == 4 && strcmp (__argv[1], "pipes") == 0)
    {
      int infd = atoi (__argv[2]);
      int outfd = atoi (__argv[3]);
      int k, n;
40
      char buf[100] = {0};
41 42 43

      if (infd < 0 || outfd < 0)
	{
44 45
	  printf ("spawn-test-win32-gui: illegal fds on command line %s",
		      lpszCmdLine);
46 47 48 49 50 51 52
	  exit (1);
	}

      n = strlen ("Hello there");
      if (write (outfd, &n, sizeof (n)) == -1 ||
	  write (outfd, "Hello there", n) == -1)
	{
53
	  int errsv = errno;
54
	  printf ("spawn-test-win32-gui: Write error: %s", strerror (errsv));
55 56 57 58 59
	  exit (1);
	}

      if ((k = read (infd, &n, sizeof (n))) != sizeof (n))
	{
60 61
	  printf ("spawn-test-win32-gui: Got only %d bytes, wanted %d",
		   k, (int)sizeof (n));
62 63 64
	  exit (1);
	}

65
      printf ("spawn-test-win32-gui: Parent says %d bytes to read", n);
66 67 68

      if ((k = read (infd, buf, n)) != n)
	{
69
	  int errsv = errno;
70
	  if (k == -1)
71
	    printf ("spawn-test-win32-gui: Read error: %s", strerror (errsv));
72
	  else
73
	    printf ("spawn-test-win32-gui: Got only %d bytes", k);
74 75 76 77 78 79 80
	  exit (1);
	}

      n = strlen ("See ya");
      if (write (outfd, &n, sizeof (n)) == -1 ||
	  write (outfd, "See ya", n) == -1)
	{
81
	  int errsv = errno;
82
	  printf ("spawn-test-win32-gui: Write error: %s", strerror (errsv));
83 84 85 86 87 88
	  exit (1);
	}
    }

  return 0;
}