glib/gspawn-win32.c Implement G_SPAWN_FILE_AND_ARGV_ZERO. (#136792, Bruce
[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   char buf[100];
14
15   if (__argc >= 2 && strcmp (__argv[1], "nop") == 0)
16     {
17       sprintf (buf, "spawn-test-win32-gui: argv[0]=\"%s\"", __argv[0]);
18       MessageBox (NULL, buf, lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
19     }
20   else if (__argc <= 2)
21     {
22       MessageBox (NULL, "spawn-test-win32-gui: Will write to stdout",
23                   lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
24       
25       printf ("This is stdout\n");
26       fflush (stdout);
27       
28       MessageBox (NULL, "spawn-test-win32-gui: Will write to stderr",
29                   lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
30       
31       fprintf (stderr, "This is stderr\n");
32       fflush (stderr);
33     }
34   else if (__argc == 4 && strcmp (__argv[1], "pipes") == 0)
35     {
36       int infd = atoi (__argv[2]);
37       int outfd = atoi (__argv[3]);
38       int k, n;
39
40       if (infd < 0 || outfd < 0)
41         {
42           MessageBox (NULL, "spawn-test-win32-gui: illegal fds on command line",
43                       lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
44           exit (1);
45         }
46
47       MessageBox (NULL, "spawn-test-win32-gui: Will write to parent",
48                   lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
49
50       n = strlen ("Hello there");
51       if (write (outfd, &n, sizeof (n)) == -1 ||
52           write (outfd, "Hello there", n) == -1)
53         {
54           sprintf (buf, "spawn-test-win32-gui: Write: %s", strerror (errno));
55           MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
56           exit (1);
57         }
58
59       MessageBox (NULL, "spawn-test-win32-gui: Will read from parent",
60                   lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
61
62       if ((k = read (infd, &n, sizeof (n))) != sizeof (n))
63         {
64           sprintf (buf, "spawn-test-win32-gui: Got only %d bytes, wanted %d",
65                    k, sizeof (n));
66           MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
67           exit (1);
68         }
69
70       sprintf (buf, "spawn-test-win32-gui: Parent says %d bytes to read", n);
71       MessageBox (NULL, buf, lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
72
73       if ((k = read (infd, buf, n)) != n)
74         {
75           if (k == -1)
76             sprintf (buf, "spawn-test-win32-gui: Read: %s", strerror (errno));
77           else
78             sprintf (buf, "spawn-test-win32-gui: Got only %d bytes", k);
79           MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
80           exit (1);
81         }
82
83       MessageBox (NULL, "spawn-test-win32-gui: Will write more to parent",
84                   lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
85
86       n = strlen ("See ya");
87       if (write (outfd, &n, sizeof (n)) == -1 ||
88           write (outfd, "See ya", n) == -1)
89         {
90           sprintf (buf, "spawn-test-win32-gui: Write: %s", strerror (errno));
91           MessageBox (NULL, buf, lpszCmdLine, MB_ICONERROR|MB_SYSTEMMODAL);
92           exit (1);
93         }
94     }
95
96   Sleep (2000);
97   
98   MessageBox (NULL, "spawn-test-win32-gui: Done, exiting.",
99               lpszCmdLine, MB_ICONINFORMATION|MB_SYSTEMMODAL);
100
101   return 0;
102 }