Merge branch '896-variant-type-docs' into 'master'
[glib.git] / tests / spawn-test-win32-gui.c
blob2798ce6da09fea91b7abe4adb3513d10e5f5e6aa
1 #include <windows.h>
2 #include <errno.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <fcntl.h>
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
16 int _stdcall
17 WinMain (struct HINSTANCE__ *hInstance,
18 struct HINSTANCE__ *hPrevInstance,
19 char *lpszCmdLine,
20 int nCmdShow)
22 char buf[100];
24 if (__argc >= 2 && strcmp (__argv[1], "nop") == 0)
26 sprintf (buf, "spawn-test-win32-gui: argv[0]=\"%s\"", __argv[0]);
27 MessageBox (NULL, buf, lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
29 else if (__argc <= 2)
31 MessageBox (NULL, "spawn-test-win32-gui: Will write to stdout",
32 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
34 printf ("This is stdout\n");
35 fflush (stdout);
37 MessageBox (NULL, "spawn-test-win32-gui: Will write to stderr",
38 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
40 fprintf (stderr, "This is stderr\n");
41 fflush (stderr);
43 else if (__argc == 4 && strcmp (__argv[1], "pipes") == 0)
45 int infd = atoi (__argv[2]);
46 int outfd = atoi (__argv[3]);
47 int k, n;
49 if (infd < 0 || outfd < 0)
51 MessageBox (NULL, "spawn-test-win32-gui: illegal fds on command line",
52 lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
53 exit (1);
56 MessageBox (NULL, "spawn-test-win32-gui: Will write to parent",
57 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
59 n = strlen ("Hello there");
60 if (write (outfd, &n, sizeof (n)) == -1 ||
61 write (outfd, "Hello there", n) == -1)
63 int errsv = errno;
64 sprintf (buf, "spawn-test-win32-gui: Write: %s", strerror (errsv));
65 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
66 exit (1);
69 MessageBox (NULL, "spawn-test-win32-gui: Will read from parent",
70 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
72 if ((k = read (infd, &n, sizeof (n))) != sizeof (n))
74 sprintf (buf, "spawn-test-win32-gui: Got only %d bytes, wanted %d",
75 k, sizeof (n));
76 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
77 exit (1);
80 sprintf (buf, "spawn-test-win32-gui: Parent says %d bytes to read", n);
81 MessageBox (NULL, buf, lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
83 if ((k = read (infd, buf, n)) != n)
85 int errsv = errno;
86 if (k == -1)
87 sprintf (buf, "spawn-test-win32-gui: Read: %s", strerror (errsv));
88 else
89 sprintf (buf, "spawn-test-win32-gui: Got only %d bytes", k);
90 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
91 exit (1);
94 MessageBox (NULL, "spawn-test-win32-gui: Will write more to parent",
95 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
97 n = strlen ("See ya");
98 if (write (outfd, &n, sizeof (n)) == -1 ||
99 write (outfd, "See ya", n) == -1)
101 int errsv = errno;
102 sprintf (buf, "spawn-test-win32-gui: Write: %s", strerror (errsv));
103 MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
104 exit (1);
108 Sleep (2000);
110 MessageBox (NULL, "spawn-test-win32-gui: Done, exiting.",
111 lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
113 return 0;