• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <windows.h>
2 #include <io.h>
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 
9 #ifdef __CYGWIN__
10 /* For read() and write() */
11 #include <unistd.h>
12 /* Cygwin does not prototype __argc and __argv in stdlib.h */
13 extern int __argc;
14 extern char** __argv;
15 #endif
16 
17 int _stdcall
WinMain(struct HINSTANCE__ * hInstance,struct HINSTANCE__ * hPrevInstance,char * lpszCmdLine,int nCmdShow)18 WinMain (struct HINSTANCE__ *hInstance,
19 	struct HINSTANCE__ *hPrevInstance,
20 	char *lpszCmdLine,
21 	int   nCmdShow)
22 {
23   if (__argc >= 2 && strcmp (__argv[1], "print_argv0") == 0)
24     {
25       printf ("%s", __argv[0]);
26     }
27   else if (__argc <= 2)
28     {
29       printf ("This is stdout\n");
30       fflush (stdout);
31 
32       fprintf (stderr, "This is stderr\n");
33       fflush (stderr);
34     }
35   else if (__argc == 4 && strcmp (__argv[1], "pipes") == 0)
36     {
37       int infd = atoi (__argv[2]);
38       int outfd = atoi (__argv[3]);
39       int k, n;
40       char buf[100] = {0};
41 
42       if (infd < 0 || outfd < 0)
43 	{
44 	  printf ("spawn-test-win32-gui: illegal fds on command line %s",
45 		      lpszCmdLine);
46 	  exit (1);
47 	}
48 
49       n = strlen ("Hello there");
50       if (write (outfd, &n, sizeof (n)) == -1 ||
51 	  write (outfd, "Hello there", n) == -1)
52 	{
53 	  int errsv = errno;
54 	  printf ("spawn-test-win32-gui: Write error: %s", strerror (errsv));
55 	  exit (1);
56 	}
57 
58       if ((k = read (infd, &n, sizeof (n))) != sizeof (n))
59 	{
60 	  printf ("spawn-test-win32-gui: Got only %d bytes, wanted %d",
61 		   k, (int)sizeof (n));
62 	  exit (1);
63 	}
64 
65       printf ("spawn-test-win32-gui: Parent says %d bytes to read", n);
66 
67       if ((k = read (infd, buf, n)) != n)
68 	{
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 	  exit (1);
75 	}
76 
77       n = strlen ("See ya");
78       if (write (outfd, &n, sizeof (n)) == -1 ||
79 	  write (outfd, "See ya", n) == -1)
80 	{
81 	  int errsv = errno;
82 	  printf ("spawn-test-win32-gui: Write error: %s", strerror (errsv));
83 	  exit (1);
84 	}
85     }
86 
87   return 0;
88 }
89