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