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
38 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
48 gchar
*erroutput
= NULL
;
49 int pipedown
[2], pipeup
[2];
54 if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
62 g_warning ("no error for sync spawn of nonexistent application");
67 if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
74 g_warning ("no error for async spawn of nonexistent application");
80 if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
84 fprintf (stderr
, "Error: %s\n", err
->message
);
90 g_assert (output
!= NULL
);
92 if (strcmp (output
, "hello\n") != 0)
94 printf ("output was '%s', should have been 'hello'\n",
104 printf ("Running netstat synchronously, collecting its output\n");
106 if (!g_spawn_command_line_sync ("netstat -n",
107 &output
, &erroutput
, NULL
,
110 fprintf (stderr
, "Error: %s\n", err
->message
);
116 g_assert (output
!= NULL
);
117 g_assert (erroutput
!= NULL
);
119 if (strstr (output
, "Active Connections") == 0)
121 printf ("output was '%s', should have contained 'Active Connections'\n",
126 if (erroutput
[0] != '\0')
128 printf ("error output was '%s', should have been empty\n",
139 printf ("Running spawn-test-win32-gui in various ways. Click on the OK buttons.\n");
141 printf ("First asynchronously (without wait).\n");
143 if (!g_spawn_command_line_async ("'.\\spawn-test-win32-gui.exe' 1", &err
))
145 fprintf (stderr
, "Error: %s\n", err
->message
);
150 printf ("Now synchronously, collecting its output.\n");
151 if (!g_spawn_command_line_sync ("'.\\spawn-test-win32-gui.exe' 2",
152 &output
, &erroutput
, NULL
,
155 fprintf (stderr
, "Error: %s\n", err
->message
);
161 g_assert (output
!= NULL
);
162 g_assert (erroutput
!= NULL
);
164 if (strcmp (output
, "This is stdout\r\n") != 0)
166 printf ("output was '%s', should have been 'This is stdout'\n",
167 g_strescape (output
, NULL
));
171 if (strcmp (erroutput
, "This is stderr\r\n") != 0)
173 printf ("error output was '%s', should have been 'This is stderr'\n",
174 g_strescape (erroutput
, NULL
));
182 printf ("Now with G_SPAWN_FILE_AND_ARGV_ZERO.\n");
184 if (!g_shell_parse_argv ("'.\\spawn-test-win32-gui.exe' this-should-be-argv-zero nop", NULL
, &argv
, &err
))
186 fprintf (stderr
, "Error parsing command line? %s\n", err
->message
);
191 if (!g_spawn_async (NULL
, argv
, NULL
,
192 G_SPAWN_FILE_AND_ARGV_ZERO
,
196 fprintf (stderr
, "Error: %s\n", err
->message
);
201 printf ("Now talking to it through pipes.\n");
203 if (pipe (pipedown
) < 0 ||
206 fprintf (stderr
, "Could not create pipes\n");
210 if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
211 pipedown
[0], pipeup
[1]),
215 fprintf (stderr
, "Error parsing command line? %s\n", err
->message
);
220 if (!g_spawn_async (NULL
, argv
, NULL
,
221 G_SPAWN_LEAVE_DESCRIPTORS_OPEN
|
222 G_SPAWN_DO_NOT_REAP_CHILD
,
226 fprintf (stderr
, "Error: %s\n", err
->message
);
235 if ((k
= read (pipeup
[0], &n
, sizeof (n
))) != sizeof (n
))
238 fprintf (stderr
, "Read error: %s\n", g_strerror (errno
));
240 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",
245 if ((k
= read (pipeup
[0], buf
, n
)) != n
)
248 fprintf (stderr
, "Read error: %s\n", g_strerror (errno
));
250 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",
255 n
= strlen ("Bye then");
256 if (write (pipedown
[1], &n
, sizeof (n
)) == -1 ||
257 write (pipedown
[1], "Bye then", n
) == -1)
259 fprintf (stderr
, "Write error: %s\n", g_strerror (errno
));
263 if ((k
= read (pipeup
[0], &n
, sizeof (n
))) != sizeof (n
))
266 fprintf (stderr
, "Read error: %s\n", g_strerror (errno
));
268 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",
273 if ((k
= read (pipeup
[0], buf
, n
)) != n
)
276 fprintf (stderr
, "Read error: %s\n", g_strerror (errno
));
278 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",