[gc] Fix crash when doing WaitForPendingFinalizers
[mono-project.git] / eglib / test / spawn.c
blobec30fc8641e0ae08a3b1ee73b5e194b96264788c
1 #include <config.h>
2 #include <glib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #ifdef HAVE_UNISTD_H
6 #include <unistd.h>
7 #endif
8 #include "test.h"
10 #ifdef G_OS_WIN32
11 #include <io.h>
12 #define read _read
13 #define close _close
14 #endif
16 RESULT
17 test_spawn_sync ()
19 gchar *out;
20 gchar *err;
21 gint status = -1;
22 GError *error = NULL;
24 if (!g_spawn_command_line_sync ("ls", &out, &err, &status, &error))
25 return FAILED ("Error executing 'ls'");
27 if (status != 0)
28 return FAILED ("Status is %d", status);
30 if (out == NULL || strlen (out) == 0)
31 return FAILED ("Didn't get any output from ls!?");
33 g_free (out);
34 g_free (err);
35 return OK;
38 RESULT
39 test_spawn_async ()
42 gboolean
43 g_spawn_async_with_pipes (const gchar *working_directory,
44 gchar **argv,
45 gchar **envp,
46 GSpawnFlags flags,
47 GSpawnChildSetupFunc child_setup,
48 gpointer user_data,
49 GPid *child_pid,
50 gint *standard_input,
51 gint *standard_output,
52 gint *standard_error,
53 GError **error) */
54 char *argv [15];
55 int stdout_fd = -1;
56 char buffer [512];
57 pid_t child_pid = 0;
59 memset (argv, 0, 15 * sizeof (char *));
60 argv [0] = "ls";
61 if (!g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &child_pid, NULL, &stdout_fd, NULL, NULL))
62 return FAILED ("1 Failed to run ls");
63 if (child_pid == 0)
64 return FAILED ("2 child pid not returned");
65 if (stdout_fd == -1)
66 return FAILED ("3 out fd is -1");
68 while (read (stdout_fd, buffer, 512) > 0);
69 close (stdout_fd);
71 return OK;
74 static Test spawn_tests [] = {
75 {"g_shell_spawn_sync", test_spawn_sync},
76 {"g_shell_spawn_async_with_pipes", test_spawn_async},
77 {NULL, NULL}
80 DEFINE_TEST_GROUP_INIT(spawn_tests_init, spawn_tests)