8 #include <gio/gunixinputstream.h>
9 #include <gio/gunixoutputstream.h>
14 static GOptionEntry options
[] = {
25 gssize bytes_written
= write (fd
, buf
, len
);
26 if (bytes_written
< 0)
27 g_error ("Failed to write to fd %d: %s",
28 fd
, strerror (errno
));
40 for (i
= 2; i
< argc
; i
++)
42 write_all (1, (guint8
*)argv
[i
], strlen (argv
[i
]));
43 write_all (1, (guint8
*)"\n", 1);
50 echo_stdout_and_stderr_mode (int argc
,
55 for (i
= 2; i
< argc
; i
++)
57 write_all (1, (guint8
*)argv
[i
], strlen (argv
[i
]));
58 write_all (1, (guint8
*)"\n", 1);
59 write_all (2, (guint8
*)argv
[i
], strlen (argv
[i
]));
60 write_all (2, (guint8
*)"\n", 1);
70 GIOChannel
*chan_stdin
;
71 GIOChannel
*chan_stdout
;
74 gsize bytes_read
, bytes_written
;
75 GError
*local_error
= NULL
;
76 GError
**error
= &local_error
;
78 chan_stdin
= g_io_channel_unix_new (0);
79 g_io_channel_set_encoding (chan_stdin
, NULL
, error
);
80 g_assert_no_error (local_error
);
81 chan_stdout
= g_io_channel_unix_new (1);
82 g_io_channel_set_encoding (chan_stdout
, NULL
, error
);
83 g_assert_no_error (local_error
);
88 status
= g_io_channel_read_chars (chan_stdin
, buf
, sizeof (buf
),
90 while (status
== G_IO_STATUS_AGAIN
);
92 if (status
== G_IO_STATUS_EOF
|| status
== G_IO_STATUS_ERROR
)
96 status
= g_io_channel_write_chars (chan_stdout
, buf
, bytes_read
,
97 &bytes_written
, error
);
98 while (status
== G_IO_STATUS_AGAIN
);
100 if (status
== G_IO_STATUS_EOF
|| status
== G_IO_STATUS_ERROR
)
104 g_io_channel_unref (chan_stdin
);
105 g_io_channel_unref (chan_stdout
);
109 g_printerr ("I/O error: %s\n", local_error
->message
);
110 g_clear_error (&local_error
);
117 sleep_forever_mode (int argc
,
122 loop
= g_main_loop_new (NULL
, TRUE
);
123 g_main_loop_run (loop
);
129 write_to_fds (int argc
, char **argv
)
133 for (i
= 2; i
< argc
; i
++)
135 int fd
= atoi (argv
[i
]);
136 FILE *f
= fdopen (fd
, "w");
137 const char buf
[] = "hello world\n";
138 size_t bytes_written
;
140 g_assert (f
!= NULL
);
142 bytes_written
= fwrite (buf
, 1, sizeof (buf
), f
);
143 g_assert (bytes_written
== sizeof (buf
));
145 if (fclose (f
) == -1)
146 g_assert_not_reached ();
153 env_mode (int argc
, char **argv
)
158 env
= g_get_environ ();
160 for (i
= 0; env
[i
]; i
++)
161 g_print ("%s\n", env
[i
]);
169 cwd_mode (int argc
, char **argv
)
173 cwd
= g_get_current_dir ();
174 g_print ("%s\n", cwd
);
181 printenv_mode (int argc
, char **argv
)
185 for (i
= 2; i
< argc
; i
++)
187 const gchar
*value
= g_getenv (argv
[i
]);
190 g_print ("%s=%s\n", argv
[i
], value
);
197 main (int argc
, char **argv
)
199 GOptionContext
*context
;
200 GError
*error
= NULL
;
203 context
= g_option_context_new ("MODE - Test GSubprocess stuff");
204 g_option_context_add_main_entries (context
, options
, NULL
);
205 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
207 g_printerr ("%s: %s\n", argv
[0], error
->message
);
213 g_printerr ("MODE argument required\n");
218 if (strcmp (mode
, "noop") == 0)
220 else if (strcmp (mode
, "exit1") == 0)
222 else if (strcmp (mode
, "assert-argv0") == 0)
224 if (strcmp (argv
[0], "moocow") == 0)
226 g_printerr ("argv0=%s != moocow\n", argv
[0]);
229 else if (strcmp (mode
, "echo") == 0)
230 return echo_mode (argc
, argv
);
231 else if (strcmp (mode
, "echo-stdout-and-stderr") == 0)
232 return echo_stdout_and_stderr_mode (argc
, argv
);
233 else if (strcmp (mode
, "cat") == 0)
234 return cat_mode (argc
, argv
);
235 else if (strcmp (mode
, "sleep-forever") == 0)
236 return sleep_forever_mode (argc
, argv
);
237 else if (strcmp (mode
, "write-to-fds") == 0)
238 return write_to_fds (argc
, argv
);
239 else if (strcmp (mode
, "env") == 0)
240 return env_mode (argc
, argv
);
241 else if (strcmp (mode
, "cwd") == 0)
242 return cwd_mode (argc
, argv
);
243 else if (strcmp (mode
, "printenv") == 0)
244 return printenv_mode (argc
, argv
);
247 g_printerr ("Unknown MODE %s\n", argv
[1]);