8 #include <gio/gunixinputstream.h>
9 #include <gio/gunixoutputstream.h>
12 static GOptionEntry options[] = {
23 gssize bytes_written = write (fd, buf, len);
24 if (bytes_written < 0)
25 g_error ("Failed to write to fd %d: %s",
26 fd, strerror (errno));
38 for (i = 2; i < argc; i++)
40 write_all (1, (guint8*)argv[i], strlen (argv[i]));
41 write_all (1, (guint8*)"\n", 1);
48 echo_stdout_and_stderr_mode (int argc,
53 for (i = 2; i < argc; i++)
55 write_all (1, (guint8*)argv[i], strlen (argv[i]));
56 write_all (1, (guint8*)"\n", 1);
57 write_all (2, (guint8*)argv[i], strlen (argv[i]));
58 write_all (2, (guint8*)"\n", 1);
68 GIOChannel *chan_stdin;
69 GIOChannel *chan_stdout;
72 gsize bytes_read, bytes_written;
73 GError *local_error = NULL;
74 GError **error = &local_error;
76 chan_stdin = g_io_channel_unix_new (0);
77 g_io_channel_set_encoding (chan_stdin, NULL, error);
78 g_assert_no_error (local_error);
79 chan_stdout = g_io_channel_unix_new (1);
80 g_io_channel_set_encoding (chan_stdout, NULL, error);
81 g_assert_no_error (local_error);
86 status = g_io_channel_read_chars (chan_stdin, buf, sizeof (buf),
88 while (status == G_IO_STATUS_AGAIN);
90 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
94 status = g_io_channel_write_chars (chan_stdout, buf, bytes_read,
95 &bytes_written, error);
96 while (status == G_IO_STATUS_AGAIN);
98 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
102 g_io_channel_unref (chan_stdin);
103 g_io_channel_unref (chan_stdout);
107 g_printerr ("I/O error: %s\n", local_error->message);
108 g_clear_error (&local_error);
115 sleep_forever_mode (int argc,
120 loop = g_main_loop_new (NULL, TRUE);
121 g_main_loop_run (loop);
127 write_to_fds (int argc, char **argv)
131 for (i = 2; i < argc; i++)
133 int fd = atoi (argv[i]);
134 FILE *f = fdopen (fd, "w");
135 const char buf[] = "hello world\n";
136 size_t bytes_written;
138 g_assert (f != NULL);
140 bytes_written = fwrite (buf, 1, sizeof (buf), f);
141 g_assert (bytes_written == sizeof (buf));
143 if (fclose (f) == -1)
144 g_assert_not_reached ();
151 env_mode (int argc, char **argv)
156 env = g_get_environ ();
158 for (i = 0; env[i]; i++)
159 g_print ("%s\n", env[i]);
167 main (int argc, char **argv)
169 GOptionContext *context;
170 GError *error = NULL;
173 context = g_option_context_new ("MODE - Test GSubprocess stuff");
174 g_option_context_add_main_entries (context, options, NULL);
175 if (!g_option_context_parse (context, &argc, &argv, &error))
177 g_printerr ("%s: %s\n", argv[0], error->message);
183 g_printerr ("MODE argument required\n");
188 if (strcmp (mode, "noop") == 0)
190 else if (strcmp (mode, "exit1") == 0)
192 else if (strcmp (mode, "assert-argv0") == 0)
194 if (strcmp (argv[0], "moocow") == 0)
196 g_printerr ("argv0=%s != moocow\n", argv[0]);
199 else if (strcmp (mode, "echo") == 0)
200 return echo_mode (argc, argv);
201 else if (strcmp (mode, "echo-stdout-and-stderr") == 0)
202 return echo_stdout_and_stderr_mode (argc, argv);
203 else if (strcmp (mode, "cat") == 0)
204 return cat_mode (argc, argv);
205 else if (strcmp (mode, "sleep-forever") == 0)
206 return sleep_forever_mode (argc, argv);
207 else if (strcmp (mode, "write-to-fds") == 0)
208 return write_to_fds (argc, argv);
209 else if (strcmp (mode, "env") == 0)
210 return env_mode (argc, argv);
213 g_printerr ("Unknown MODE %s\n", argv[1]);