1 #include <windows.h> 2 #include <errno.h> 3 #include <stdio.h> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <fcntl.h> 7 8 #ifdef __CYGWIN__ 9 /* For read() and write() */ 10 #include <unistd.h> 11 /* Cygwin does not prototype __argc and __argv in stdlib.h */ 12 extern int __argc; 13 extern char** __argv; 14 #endif 15 16 int _stdcall 17 WinMain (struct HINSTANCE__ *hInstance, 18 struct HINSTANCE__ *hPrevInstance, 19 char *lpszCmdLine, 20 int nCmdShow) 21 { 22 char buf[100]; 23 24 if (__argc >= 2 && strcmp (__argv[1], "nop") == 0) 25 { 26 sprintf (buf, "spawn-test-win32-gui: argv[0]=\"%s\"", __argv[0]); 27 MessageBox (NULL, buf, lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 28 } 29 else if (__argc <= 2) 30 { 31 MessageBox (NULL, "spawn-test-win32-gui: Will write to stdout", 32 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 33 34 printf ("This is stdout\n"); 35 fflush (stdout); 36 37 MessageBox (NULL, "spawn-test-win32-gui: Will write to stderr", 38 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 39 40 fprintf (stderr, "This is stderr\n"); 41 fflush (stderr); 42 } 43 else if (__argc == 4 && strcmp (__argv[1], "pipes") == 0) 44 { 45 int infd = atoi (__argv[2]); 46 int outfd = atoi (__argv[3]); 47 int k, n; 48 49 if (infd < 0 || outfd < 0) 50 { 51 MessageBox (NULL, "spawn-test-win32-gui: illegal fds on command line", 52 lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL); 53 exit (1); 54 } 55 56 MessageBox (NULL, "spawn-test-win32-gui: Will write to parent", 57 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 58 59 n = strlen ("Hello there"); 60 if (write (outfd, &n, sizeof (n)) == -1 || 61 write (outfd, "Hello there", n) == -1) 62 { 63 sprintf (buf, "spawn-test-win32-gui: Write: %s", strerror (errno)); 64 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL); 65 exit (1); 66 } 67 68 MessageBox (NULL, "spawn-test-win32-gui: Will read from parent", 69 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 70 71 if ((k = read (infd, &n, sizeof (n))) != sizeof (n)) 72 { 73 sprintf (buf, "spawn-test-win32-gui: Got only %d bytes, wanted %d", 74 k, sizeof (n)); 75 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL); 76 exit (1); 77 } 78 79 sprintf (buf, "spawn-test-win32-gui: Parent says %d bytes to read", n); 80 MessageBox (NULL, buf, lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 81 82 if ((k = read (infd, buf, n)) != n) 83 { 84 if (k == -1) 85 sprintf (buf, "spawn-test-win32-gui: Read: %s", strerror (errno)); 86 else 87 sprintf (buf, "spawn-test-win32-gui: Got only %d bytes", k); 88 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL); 89 exit (1); 90 } 91 92 MessageBox (NULL, "spawn-test-win32-gui: Will write more to parent", 93 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 94 95 n = strlen ("See ya"); 96 if (write (outfd, &n, sizeof (n)) == -1 || 97 write (outfd, "See ya", n) == -1) 98 { 99 sprintf (buf, "spawn-test-win32-gui: Write: %s", strerror (errno)); 100 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL); 101 exit (1); 102 } 103 } 104 105 Sleep (2000); 106 107 MessageBox (NULL, "spawn-test-win32-gui: Done, exiting.", 108 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL); 109 110 return 0; 111 } 112