Update German translation
[glib.git] / tests / spawn-test.c
blob806b93eb8fd5eafe819e725b713f5f25d2035b9c
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 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 if (k == -1)
236 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
237 else
238 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
239 sizeof (n), k);
240 exit (1);
243 if ((k = read (pipeup[0], buf, n)) != n)
245 if (k == -1)
246 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
247 else
248 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
249 n, k);
250 exit (1);
253 n = strlen ("Bye then");
254 if (write (pipedown[1], &n, sizeof (n)) == -1 ||
255 write (pipedown[1], "Bye then", n) == -1)
257 fprintf (stderr, "Write error: %s\n", g_strerror (errno));
258 exit (1);
261 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
263 if (k == -1)
264 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
265 else
266 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
267 sizeof (n), k);
268 exit (1);
271 if ((k = read (pipeup[0], buf, n)) != n)
273 if (k == -1)
274 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
275 else
276 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
277 n, k);
278 exit (1);
281 #endif
282 #endif
286 main (int argc,
287 char *argv[])
289 run_tests ();
291 return 0;