Merge branch '896-variant-type-docs' into 'master'
[glib.git] / tests / spawn-test.c
blob05cce17f852ccd5d74afa569bf38f3e13d339b2a
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #undef G_DISABLE_ASSERT
26 #undef G_LOG_DOMAIN
28 #include <glib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
33 #ifdef G_OS_WIN32
34 #include <fcntl.h>
35 #include <io.h>
36 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
37 #endif
40 static void
41 run_tests (void)
43 GError *err;
44 gchar *output = NULL;
45 #ifdef G_OS_WIN32
46 gchar *erroutput = NULL;
47 int pipedown[2], pipeup[2];
48 gchar **argv = 0;
49 #endif
51 err = NULL;
52 if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
53 NULL, NULL, NULL,
54 &err))
56 g_error_free (err);
58 else
60 g_warning ("no error for sync spawn of nonexistent application");
61 exit (1);
64 err = NULL;
65 if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
66 &err))
68 g_error_free (err);
70 else
72 g_warning ("no error for async spawn of nonexistent application");
73 exit (1);
76 err = NULL;
77 #ifdef G_OS_UNIX
78 if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
79 &output, NULL, NULL,
80 &err))
82 fprintf (stderr, "Error: %s\n", err->message);
83 g_error_free (err);
84 exit (1);
86 else
88 g_assert (output != NULL);
90 if (strcmp (output, "hello\n") != 0)
92 printf ("output was '%s', should have been 'hello'\n",
93 output);
95 exit (1);
98 g_free (output);
100 #else
101 #ifdef G_OS_WIN32
102 printf ("Running netstat synchronously, collecting its output\n");
104 if (!g_spawn_command_line_sync ("netstat -n",
105 &output, &erroutput, NULL,
106 &err))
108 fprintf (stderr, "Error: %s\n", err->message);
109 g_error_free (err);
110 exit (1);
112 else
114 g_assert (output != NULL);
115 g_assert (erroutput != NULL);
117 if (strstr (output, "Active Connections") == 0)
119 printf ("output was '%s', should have contained 'Active Connections'\n",
120 output);
122 exit (1);
124 if (erroutput[0] != '\0')
126 printf ("error output was '%s', should have been empty\n",
127 erroutput);
128 exit (1);
131 g_free (output);
132 output = NULL;
133 g_free (erroutput);
134 erroutput = NULL;
137 printf ("Running spawn-test-win32-gui in various ways. Click on the OK buttons.\n");
139 printf ("First asynchronously (without wait).\n");
141 if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
143 fprintf (stderr, "Error: %s\n", err->message);
144 g_error_free (err);
145 exit (1);
148 printf ("Now synchronously, collecting its output.\n");
149 if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
150 &output, &erroutput, NULL,
151 &err))
153 fprintf (stderr, "Error: %s\n", err->message);
154 g_error_free (err);
155 exit (1);
157 else
159 g_assert (output != NULL);
160 g_assert (erroutput != NULL);
162 if (strcmp (output, "This is stdout\r\n") != 0)
164 printf ("output was '%s', should have been 'This is stdout'\n",
165 g_strescape (output, NULL));
167 exit (1);
169 if (strcmp (erroutput, "This is stderr\r\n") != 0)
171 printf ("error output was '%s', should have been 'This is stderr'\n",
172 g_strescape (erroutput, NULL));
173 exit (1);
176 g_free (output);
177 g_free (erroutput);
180 printf ("Now with G_SPAWN_FILE_AND_ARGV_ZERO.\n");
182 if (!g_shell_parse_argv ("'.\\spawn-test-win32-gui.exe' this-should-be-argv-zero nop", NULL, &argv, &err))
184 fprintf (stderr, "Error parsing command line? %s\n", err->message);
185 g_error_free (err);
186 exit (1);
189 if (!g_spawn_async (NULL, argv, NULL,
190 G_SPAWN_FILE_AND_ARGV_ZERO,
191 NULL, NULL, NULL,
192 &err))
194 fprintf (stderr, "Error: %s\n", err->message);
195 g_error_free (err);
196 exit (1);
199 printf ("Now talking to it through pipes.\n");
201 if (pipe (pipedown) < 0 ||
202 pipe (pipeup) < 0)
204 fprintf (stderr, "Could not create pipes\n");
205 exit (1);
208 if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
209 pipedown[0], pipeup[1]),
210 NULL, &argv,
211 &err))
213 fprintf (stderr, "Error parsing command line? %s\n", err->message);
214 g_error_free (err);
215 exit (1);
218 if (!g_spawn_async (NULL, argv, NULL,
219 G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
220 G_SPAWN_DO_NOT_REAP_CHILD,
221 NULL, NULL, NULL,
222 &err))
224 fprintf (stderr, "Error: %s\n", err->message);
225 g_error_free (err);
226 exit (1);
228 else
230 int k, n;
231 char buf[100];
233 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
235 int errsv = errno;
236 if (k == -1)
237 fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
238 else
239 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
240 sizeof (n), k);
241 exit (1);
244 if ((k = read (pipeup[0], buf, n)) != n)
246 int errsv = errno;
247 if (k == -1)
248 fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
249 else
250 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
251 n, k);
252 exit (1);
255 n = strlen ("Bye then");
256 if (write (pipedown[1], &n, sizeof (n)) == -1 ||
257 write (pipedown[1], "Bye then", n) == -1)
259 int errsv = errno;
260 fprintf (stderr, "Write error: %s\n", g_strerror (errsv));
261 exit (1);
264 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
266 int errsv = errno;
267 if (k == -1)
268 fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
269 else
270 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
271 sizeof (n), k);
272 exit (1);
275 if ((k = read (pipeup[0], buf, n)) != n)
277 int errsv = errno;
278 if (k == -1)
279 fprintf (stderr, "Read error: %s\n", g_strerror (errsv));
280 else
281 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
282 n, k);
283 exit (1);
286 #endif
287 #endif
291 main (int argc,
292 char *argv[])
294 run_tests ();
296 return 0;