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];
52 printf ("The following errors are supposed to occur:\n");
55 if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
59 fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
64 if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
67 fprintf (stderr, "Error (normal, supposed to happen): %s\n", err->message);
71 printf ("Errors after this are not supposed to happen:\n");
75 if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
79 fprintf (stderr, "Error: %s\n", err->message);
85 g_assert (output != NULL);
87 if (strcmp (output, "hello\n") != 0)
89 printf ("output was '%s', should have been 'hello'\n",
99 printf ("Running netstat synchronously, collecting its output\n");
101 if (!g_spawn_command_line_sync ("netstat -n",
102 &output, &erroutput, NULL,
105 fprintf (stderr, "Error: %s\n", err->message);
111 g_assert (output != NULL);
112 g_assert (erroutput != NULL);
114 if (strstr (output, "Active Connections") == 0)
116 printf ("output was '%s', should have contained 'Active Connections'\n",
121 if (erroutput[0] != '\0')
123 printf ("error output was '%s', should have been empty\n",
134 printf ("Running spawn-test-win32-gui in various ways. Click on the OK buttons.\n");
136 printf ("First asynchronously (without wait).\n");
138 if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err))
140 fprintf (stderr, "Error: %s\n", err->message);
145 printf ("Now synchronously, collecting its output.\n");
146 if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
147 &output, &erroutput, NULL,
150 fprintf (stderr, "Error: %s\n", err->message);
156 g_assert (output != NULL);
157 g_assert (erroutput != NULL);
159 if (strcmp (output, "This is stdout\r\n") != 0)
161 printf ("output was '%s', should have been 'This is stdout'\n",
162 g_strescape (output, NULL));
166 if (strcmp (erroutput, "This is stderr\r\n") != 0)
168 printf ("error output was '%s', should have been 'This is stderr'\n",
169 g_strescape (erroutput, NULL));
177 printf ("Now with G_SPAWN_FILE_AND_ARGV_ZERO.\n");
179 if (!g_shell_parse_argv ("'.\\spawn-test-win32-gui.exe' this-should-be-argv-zero nop", NULL, &argv, &err))
181 fprintf (stderr, "Error parsing command line? %s\n", err->message);
186 if (!g_spawn_async (NULL, argv, NULL,
187 G_SPAWN_FILE_AND_ARGV_ZERO,
191 fprintf (stderr, "Error: %s\n", err->message);
196 printf ("Now talking to it through pipes.\n");
198 if (pipe (pipedown) < 0 ||
201 fprintf (stderr, "Could not create pipes\n");
205 if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
206 pipedown[0], pipeup[1]),
210 fprintf (stderr, "Error parsing command line? %s\n", err->message);
215 if (!g_spawn_async (NULL, argv, NULL,
216 G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
217 G_SPAWN_DO_NOT_REAP_CHILD,
221 fprintf (stderr, "Error: %s\n", err->message);
230 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
233 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
235 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
240 if ((k = read (pipeup[0], buf, n)) != n)
243 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
245 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
250 n = strlen ("Bye then");
251 if (write (pipedown[1], &n, sizeof (n)) == -1 ||
252 write (pipedown[1], "Bye then", n) == -1)
254 fprintf (stderr, "Write error: %s\n", g_strerror (errno));
258 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
261 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
263 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
268 if ((k = read (pipeup[0], buf, n)) != n)
271 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
273 fprintf (stderr, "Wanted to read %d bytes, got %d\n",