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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #undef G_DISABLE_ASSERT
47 gchar *erroutput = NULL;
48 int pipedown[2], pipeup[2];
53 if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
61 g_warning ("no error for sync spawn of nonexistent application");
66 if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
73 g_warning ("no error for async spawn of nonexistent application");
79 if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
83 fprintf (stderr, "Error: %s\n", err->message);
89 g_assert (output != NULL);
91 if (strcmp (output, "hello\n") != 0)
93 printf ("output was '%s', should have been 'hello'\n",
103 printf ("Running netstat synchronously, collecting its output\n");
105 if (!g_spawn_command_line_sync ("netstat -n",
106 &output, &erroutput, NULL,
109 fprintf (stderr, "Error: %s\n", err->message);
115 g_assert (output != NULL);
116 g_assert (erroutput != NULL);
118 if (strstr (output, "Active Connections") == 0)
120 printf ("output was '%s', should have contained 'Active Connections'\n",
125 if (erroutput[0] != '\0')
127 printf ("error output was '%s', should have been empty\n",
138 printf ("Running spawn-test-win32-gui in various ways. Click on the OK buttons.\n");
140 printf ("First asynchronously (without wait).\n");
142 if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
144 fprintf (stderr, "Error: %s\n", err->message);
149 printf ("Now synchronously, collecting its output.\n");
150 if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
151 &output, &erroutput, NULL,
154 fprintf (stderr, "Error: %s\n", err->message);
160 g_assert (output != NULL);
161 g_assert (erroutput != NULL);
163 if (strcmp (output, "This is stdout\r\n") != 0)
165 printf ("output was '%s', should have been 'This is stdout'\n",
166 g_strescape (output, NULL));
170 if (strcmp (erroutput, "This is stderr\r\n") != 0)
172 printf ("error output was '%s', should have been 'This is stderr'\n",
173 g_strescape (erroutput, NULL));
181 printf ("Now with G_SPAWN_FILE_AND_ARGV_ZERO.\n");
183 if (!g_shell_parse_argv ("'.\\spawn-test-win32-gui.exe' this-should-be-argv-zero nop", NULL, &argv, &err))
185 fprintf (stderr, "Error parsing command line? %s\n", err->message);
190 if (!g_spawn_async (NULL, argv, NULL,
191 G_SPAWN_FILE_AND_ARGV_ZERO,
195 fprintf (stderr, "Error: %s\n", err->message);
200 printf ("Now talking to it through pipes.\n");
202 if (pipe (pipedown) < 0 ||
205 fprintf (stderr, "Could not create pipes\n");
209 if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
210 pipedown[0], pipeup[1]),
214 fprintf (stderr, "Error parsing command line? %s\n", err->message);
219 if (!g_spawn_async (NULL, argv, NULL,
220 G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
221 G_SPAWN_DO_NOT_REAP_CHILD,
225 fprintf (stderr, "Error: %s\n", err->message);
234 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
237 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
239 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
244 if ((k = read (pipeup[0], buf, n)) != n)
247 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
249 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
254 n = strlen ("Bye then");
255 if (write (pipedown[1], &n, sizeof (n)) == -1 ||
256 write (pipedown[1], "Bye then", n) == -1)
258 fprintf (stderr, "Write error: %s\n", g_strerror (errno));
262 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
265 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
267 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
272 if ((k = read (pipeup[0], buf, n)) != n)
275 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
277 fprintf (stderr, "Wanted to read %d bytes, got %d\n",