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
36 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
46 gchar
*erroutput
= NULL
;
47 int pipedown
[2], pipeup
[2];
52 if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
60 g_warning ("no error for sync spawn of nonexistent application");
65 if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
72 g_warning ("no error for async spawn of nonexistent application");
78 if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
82 fprintf (stderr
, "Error: %s\n", err
->message
);
88 g_assert (output
!= NULL
);
90 if (strcmp (output
, "hello\n") != 0)
92 printf ("output was '%s', should have been 'hello'\n",
102 printf ("Running netstat synchronously, collecting its output\n");
104 if (!g_spawn_command_line_sync ("netstat -n",
105 &output
, &erroutput
, NULL
,
108 fprintf (stderr
, "Error: %s\n", err
->message
);
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",
124 if (erroutput
[0] != '\0')
126 printf ("error output was '%s', should have been empty\n",
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
);
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
,
153 fprintf (stderr
, "Error: %s\n", err
->message
);
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
));
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
));
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
);
189 if (!g_spawn_async (NULL
, argv
, NULL
,
190 G_SPAWN_FILE_AND_ARGV_ZERO
,
194 fprintf (stderr
, "Error: %s\n", err
->message
);
199 printf ("Now talking to it through pipes.\n");
201 if (pipe (pipedown
) < 0 ||
204 fprintf (stderr
, "Could not create pipes\n");
208 if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
209 pipedown
[0], pipeup
[1]),
213 fprintf (stderr
, "Error parsing command line? %s\n", err
->message
);
218 if (!g_spawn_async (NULL
, argv
, NULL
,
219 G_SPAWN_LEAVE_DESCRIPTORS_OPEN
|
220 G_SPAWN_DO_NOT_REAP_CHILD
,
224 fprintf (stderr
, "Error: %s\n", err
->message
);
233 if ((k
= read (pipeup
[0], &n
, sizeof (n
))) != sizeof (n
))
237 fprintf (stderr
, "Read error: %s\n", g_strerror (errsv
));
239 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",
244 if ((k
= read (pipeup
[0], buf
, n
)) != n
)
248 fprintf (stderr
, "Read error: %s\n", g_strerror (errsv
));
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)
260 fprintf (stderr
, "Write error: %s\n", g_strerror (errsv
));
264 if ((k
= read (pipeup
[0], &n
, sizeof (n
))) != sizeof (n
))
268 fprintf (stderr
, "Read error: %s\n", g_strerror (errsv
));
270 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",
275 if ((k
= read (pipeup
[0], buf
, n
)) != n
)
279 fprintf (stderr
, "Read error: %s\n", g_strerror (errsv
));
281 fprintf (stderr
, "Wanted to read %d bytes, got %d\n",