7 #include <gio/gunixinputstream.h>
8 #include <gio/gunixoutputstream.h>
11 static GOptionEntry options[] = {
22 ssize_t bytes_written = write (fd, buf, len);
23 if (bytes_written < 0)
24 g_error ("Failed to write to fd %d: %s",
25 fd, strerror (errno));
37 for (i = 2; i < argc; i++)
39 write_all (1, (guint8*)argv[i], strlen (argv[i]));
40 write_all (1, (guint8*)"\n", 1);
47 echo_stdout_and_stderr_mode (int argc,
52 for (i = 2; i < argc; i++)
54 write_all (1, (guint8*)argv[i], strlen (argv[i]));
55 write_all (1, (guint8*)"\n", 1);
56 write_all (2, (guint8*)argv[i], strlen (argv[i]));
57 write_all (2, (guint8*)"\n", 1);
67 GIOChannel *chan_stdin;
68 GIOChannel *chan_stdout;
71 gsize bytes_read, bytes_written;
72 GError *local_error = NULL;
73 GError **error = &local_error;
75 chan_stdin = g_io_channel_unix_new (0);
76 g_io_channel_set_encoding (chan_stdin, NULL, error);
77 g_assert_no_error (local_error);
78 chan_stdout = g_io_channel_unix_new (1);
79 g_io_channel_set_encoding (chan_stdout, NULL, error);
80 g_assert_no_error (local_error);
85 status = g_io_channel_read_chars (chan_stdin, buf, sizeof (buf),
87 while (status == G_IO_STATUS_AGAIN);
89 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
93 status = g_io_channel_write_chars (chan_stdout, buf, bytes_read,
94 &bytes_written, error);
95 while (status == G_IO_STATUS_AGAIN);
97 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
101 g_io_channel_unref (chan_stdin);
102 g_io_channel_unref (chan_stdout);
106 g_printerr ("I/O error: %s\n", local_error->message);
107 g_clear_error (&local_error);
114 sleep_forever_mode (int argc,
119 loop = g_main_loop_new (NULL, TRUE);
120 g_main_loop_run (loop);
126 write_to_fds (int argc, char **argv)
130 for (i = 2; i < argc; i++)
132 int fd = atoi (argv[i]);
133 FILE *f = fdopen (fd, "w");
134 const char buf[] = "hello world\n";
135 size_t bytes_written;
137 g_assert (f != NULL);
139 bytes_written = fwrite (buf, 1, sizeof (buf), f);
140 g_assert (bytes_written == sizeof (buf));
142 if (fclose (f) == -1)
143 g_assert_not_reached ();
150 main (int argc, char **argv)
152 GOptionContext *context;
153 GError *error = NULL;
156 context = g_option_context_new ("MODE - Test GSubprocess stuff");
157 g_option_context_add_main_entries (context, options, NULL);
158 if (!g_option_context_parse (context, &argc, &argv, &error))
160 g_printerr ("%s: %s\n", argv[0], error->message);
166 g_printerr ("MODE argument required\n");
171 if (strcmp (mode, "noop") == 0)
173 else if (strcmp (mode, "exit1") == 0)
175 else if (strcmp (mode, "assert-argv0") == 0)
177 if (strcmp (argv[0], "moocow") == 0)
179 g_printerr ("argv0=%s != moocow\n", argv[0]);
182 else if (strcmp (mode, "echo") == 0)
183 return echo_mode (argc, argv);
184 else if (strcmp (mode, "echo-stdout-and-stderr") == 0)
185 return echo_stdout_and_stderr_mode (argc, argv);
186 else if (strcmp (mode, "cat") == 0)
187 return cat_mode (argc, argv);
188 else if (strcmp (mode, "sleep-forever") == 0)
189 return sleep_forever_mode (argc, argv);
190 else if (strcmp (mode, "write-to-fds") == 0)
191 return write_to_fds (argc, argv);
194 g_printerr ("Unknown MODE %s\n", argv[1]);