comments/docs: Fix couple of typos
[glib.git] / tests / spawn-test.c
blob1bc360a4280247be565ec6ebd90bdd1189099a77
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
28 #undef G_LOG_DOMAIN
30 #include <glib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
35 #ifdef G_OS_WIN32
36 #include <fcntl.h>
37 #include <io.h>
38 #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
39 #endif
42 static void
43 run_tests (void)
45 GError *err;
46 gchar *output = NULL;
47 #ifdef G_OS_WIN32
48 gchar *erroutput = NULL;
49 int pipedown[2], pipeup[2];
50 gchar **argv = 0;
51 #endif
53 err = NULL;
54 if (!g_spawn_command_line_sync ("nonexistent_application foo 'bar baz' blah blah",
55 NULL, NULL, NULL,
56 &err))
58 g_error_free (err);
60 else
62 g_warning ("no error for sync spawn of nonexistent application");
63 exit (1);
66 err = NULL;
67 if (!g_spawn_command_line_async ("nonexistent_application foo bar baz \"blah blah\"",
68 &err))
70 g_error_free (err);
72 else
74 g_warning ("no error for async spawn of nonexistent application");
75 exit (1);
78 err = NULL;
79 #ifdef G_OS_UNIX
80 if (!g_spawn_command_line_sync ("/bin/sh -c 'echo hello'",
81 &output, NULL, NULL,
82 &err))
84 fprintf (stderr, "Error: %s\n", err->message);
85 g_error_free (err);
86 exit (1);
88 else
90 g_assert (output != NULL);
92 if (strcmp (output, "hello\n") != 0)
94 printf ("output was '%s', should have been 'hello'\n",
95 output);
97 exit (1);
100 g_free (output);
102 #else
103 #ifdef G_OS_WIN32
104 printf ("Running netstat synchronously, collecting its output\n");
106 if (!g_spawn_command_line_sync ("netstat -n",
107 &output, &erroutput, NULL,
108 &err))
110 fprintf (stderr, "Error: %s\n", err->message);
111 g_error_free (err);
112 exit (1);
114 else
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",
122 output);
124 exit (1);
126 if (erroutput[0] != '\0')
128 printf ("error output was '%s', should have been empty\n",
129 erroutput);
130 exit (1);
133 g_free (output);
134 output = NULL;
135 g_free (erroutput);
136 erroutput = NULL;
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);
146 g_error_free (err);
147 exit (1);
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,
153 &err))
155 fprintf (stderr, "Error: %s\n", err->message);
156 g_error_free (err);
157 exit (1);
159 else
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));
169 exit (1);
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));
175 exit (1);
178 g_free (output);
179 g_free (erroutput);
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);
187 g_error_free (err);
188 exit (1);
191 if (!g_spawn_async (NULL, argv, NULL,
192 G_SPAWN_FILE_AND_ARGV_ZERO,
193 NULL, NULL, NULL,
194 &err))
196 fprintf (stderr, "Error: %s\n", err->message);
197 g_error_free (err);
198 exit (1);
201 printf ("Now talking to it through pipes.\n");
203 if (pipe (pipedown) < 0 ||
204 pipe (pipeup) < 0)
206 fprintf (stderr, "Could not create pipes\n");
207 exit (1);
210 if (!g_shell_parse_argv (g_strdup_printf ("'.\\spawn-test-win32-gui.exe' pipes %d %d",
211 pipedown[0], pipeup[1]),
212 NULL, &argv,
213 &err))
215 fprintf (stderr, "Error parsing command line? %s\n", err->message);
216 g_error_free (err);
217 exit (1);
220 if (!g_spawn_async (NULL, argv, NULL,
221 G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
222 G_SPAWN_DO_NOT_REAP_CHILD,
223 NULL, NULL, NULL,
224 &err))
226 fprintf (stderr, "Error: %s\n", err->message);
227 g_error_free (err);
228 exit (1);
230 else
232 int k, n;
233 char buf[100];
235 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
237 if (k == -1)
238 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
239 else
240 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
241 sizeof (n), k);
242 exit (1);
245 if ((k = read (pipeup[0], buf, n)) != n)
247 if (k == -1)
248 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
249 else
250 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
251 n, k);
252 exit (1);
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));
260 exit (1);
263 if ((k = read (pipeup[0], &n, sizeof (n))) != sizeof (n))
265 if (k == -1)
266 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
267 else
268 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
269 sizeof (n), k);
270 exit (1);
273 if ((k = read (pipeup[0], buf, n)) != n)
275 if (k == -1)
276 fprintf (stderr, "Read error: %s\n", g_strerror (errno));
277 else
278 fprintf (stderr, "Wanted to read %d bytes, got %d\n",
279 n, k);
280 exit (1);
283 #endif
284 #endif
288 main (int argc,
289 char *argv[])
291 run_tests ();
293 return 0;