8 #include <gio/gunixinputstream.h>
9 #include <gio/gunixoutputstream.h>
14 static GOptionEntry options
[] = {
25 gssize bytes_written
= write (fd
, buf
, len
);
27 if (bytes_written
< 0)
28 g_error ("Failed to write to fd %d: %s",
29 fd
, g_strerror (errsv
));
41 for (i
= 2; i
< argc
; i
++)
43 write_all (1, (guint8
*)argv
[i
], strlen (argv
[i
]));
44 write_all (1, (guint8
*)"\n", 1);
51 echo_stdout_and_stderr_mode (int argc
,
56 for (i
= 2; i
< argc
; i
++)
58 write_all (1, (guint8
*)argv
[i
], strlen (argv
[i
]));
59 write_all (1, (guint8
*)"\n", 1);
60 write_all (2, (guint8
*)argv
[i
], strlen (argv
[i
]));
61 write_all (2, (guint8
*)"\n", 1);
71 GIOChannel
*chan_stdin
;
72 GIOChannel
*chan_stdout
;
75 gsize bytes_read
, bytes_written
;
76 GError
*local_error
= NULL
;
77 GError
**error
= &local_error
;
79 chan_stdin
= g_io_channel_unix_new (0);
80 g_io_channel_set_encoding (chan_stdin
, NULL
, error
);
81 g_assert_no_error (local_error
);
82 chan_stdout
= g_io_channel_unix_new (1);
83 g_io_channel_set_encoding (chan_stdout
, NULL
, error
);
84 g_assert_no_error (local_error
);
89 status
= g_io_channel_read_chars (chan_stdin
, buf
, sizeof (buf
),
91 while (status
== G_IO_STATUS_AGAIN
);
93 if (status
== G_IO_STATUS_EOF
|| status
== G_IO_STATUS_ERROR
)
97 status
= g_io_channel_write_chars (chan_stdout
, buf
, bytes_read
,
98 &bytes_written
, error
);
99 while (status
== G_IO_STATUS_AGAIN
);
101 if (status
== G_IO_STATUS_EOF
|| status
== G_IO_STATUS_ERROR
)
105 g_io_channel_unref (chan_stdin
);
106 g_io_channel_unref (chan_stdout
);
110 g_printerr ("I/O error: %s\n", local_error
->message
);
111 g_clear_error (&local_error
);
118 sleep_forever_mode (int argc
,
123 loop
= g_main_loop_new (NULL
, TRUE
);
124 g_main_loop_run (loop
);
130 write_to_fds (int argc
, char **argv
)
134 for (i
= 2; i
< argc
; i
++)
136 int fd
= atoi (argv
[i
]);
137 FILE *f
= fdopen (fd
, "w");
138 const char buf
[] = "hello world\n";
139 size_t bytes_written
;
141 g_assert (f
!= NULL
);
143 bytes_written
= fwrite (buf
, 1, sizeof (buf
), f
);
144 g_assert (bytes_written
== sizeof (buf
));
146 if (fclose (f
) == -1)
147 g_assert_not_reached ();
154 env_mode (int argc
, char **argv
)
159 env
= g_get_environ ();
161 for (i
= 0; env
[i
]; i
++)
162 g_print ("%s\n", env
[i
]);
170 cwd_mode (int argc
, char **argv
)
174 cwd
= g_get_current_dir ();
175 g_print ("%s\n", cwd
);
182 printenv_mode (int argc
, char **argv
)
186 for (i
= 2; i
< argc
; i
++)
188 const gchar
*value
= g_getenv (argv
[i
]);
191 g_print ("%s=%s\n", argv
[i
], value
);
198 main (int argc
, char **argv
)
200 GOptionContext
*context
;
201 GError
*error
= NULL
;
204 context
= g_option_context_new ("MODE - Test GSubprocess stuff");
205 g_option_context_add_main_entries (context
, options
, NULL
);
206 if (!g_option_context_parse (context
, &argc
, &argv
, &error
))
208 g_printerr ("%s: %s\n", argv
[0], error
->message
);
214 g_printerr ("MODE argument required\n");
219 if (strcmp (mode
, "noop") == 0)
221 else if (strcmp (mode
, "exit1") == 0)
223 else if (strcmp (mode
, "assert-argv0") == 0)
225 if (strcmp (argv
[0], "moocow") == 0)
227 g_printerr ("argv0=%s != moocow\n", argv
[0]);
230 else if (strcmp (mode
, "echo") == 0)
231 return echo_mode (argc
, argv
);
232 else if (strcmp (mode
, "echo-stdout-and-stderr") == 0)
233 return echo_stdout_and_stderr_mode (argc
, argv
);
234 else if (strcmp (mode
, "cat") == 0)
235 return cat_mode (argc
, argv
);
236 else if (strcmp (mode
, "sleep-forever") == 0)
237 return sleep_forever_mode (argc
, argv
);
238 else if (strcmp (mode
, "write-to-fds") == 0)
239 return write_to_fds (argc
, argv
);
240 else if (strcmp (mode
, "env") == 0)
241 return env_mode (argc
, argv
);
242 else if (strcmp (mode
, "cwd") == 0)
243 return cwd_mode (argc
, argv
);
244 else if (strcmp (mode
, "printenv") == 0)
245 return printenv_mode (argc
, argv
);
248 g_printerr ("Unknown MODE %s\n", argv
[1]);