Add Friulian translation
[glib.git] / gio / tests / gsubprocess-testprog.c
blob7457246729339b988393a84b0410b217ffc9e117
1 #include <gio/gio.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <errno.h>
6 #ifdef G_OS_UNIX
7 #include <unistd.h>
8 #include <gio/gunixinputstream.h>
9 #include <gio/gunixoutputstream.h>
10 #else
11 #include <io.h>
12 #endif
14 static GOptionEntry options[] = {
15 {NULL}
18 static void
19 write_all (int fd,
20 const guint8* buf,
21 gsize len)
23 while (len > 0)
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));
29 buf += bytes_written;
30 len -= bytes_written;
34 static int
35 echo_mode (int argc,
36 char **argv)
38 int i;
40 for (i = 2; i < argc; i++)
42 write_all (1, (guint8*)argv[i], strlen (argv[i]));
43 write_all (1, (guint8*)"\n", 1);
46 return 0;
49 static int
50 echo_stdout_and_stderr_mode (int argc,
51 char **argv)
53 int i;
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);
63 return 0;
66 static int
67 cat_mode (int argc,
68 char **argv)
70 GIOChannel *chan_stdin;
71 GIOChannel *chan_stdout;
72 GIOStatus status;
73 char buf[1024];
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);
85 while (TRUE)
88 status = g_io_channel_read_chars (chan_stdin, buf, sizeof (buf),
89 &bytes_read, error);
90 while (status == G_IO_STATUS_AGAIN);
92 if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
93 break;
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)
101 break;
104 g_io_channel_unref (chan_stdin);
105 g_io_channel_unref (chan_stdout);
107 if (local_error)
109 g_printerr ("I/O error: %s\n", local_error->message);
110 g_clear_error (&local_error);
111 return 1;
113 return 0;
116 static gint
117 sleep_forever_mode (int argc,
118 char **argv)
120 GMainLoop *loop;
122 loop = g_main_loop_new (NULL, TRUE);
123 g_main_loop_run (loop);
125 return 0;
128 static int
129 write_to_fds (int argc, char **argv)
131 int i;
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 ();
149 return 0;
152 static int
153 env_mode (int argc, char **argv)
155 char **env;
156 int i;
158 env = g_get_environ ();
160 for (i = 0; env[i]; i++)
161 g_print ("%s\n", env[i]);
163 g_strfreev (env);
165 return 0;
168 static int
169 cwd_mode (int argc, char **argv)
171 char *cwd;
173 cwd = g_get_current_dir ();
174 g_print ("%s\n", cwd);
175 g_free (cwd);
177 return 0;
180 static int
181 printenv_mode (int argc, char **argv)
183 gint i;
185 for (i = 2; i < argc; i++)
187 const gchar *value = g_getenv (argv[i]);
189 if (value != NULL)
190 g_print ("%s=%s\n", argv[i], value);
193 return 0;
197 main (int argc, char **argv)
199 GOptionContext *context;
200 GError *error = NULL;
201 const char *mode;
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);
208 return 1;
211 if (argc < 2)
213 g_printerr ("MODE argument required\n");
214 return 1;
217 mode = argv[1];
218 if (strcmp (mode, "noop") == 0)
219 return 0;
220 else if (strcmp (mode, "exit1") == 0)
221 return 1;
222 else if (strcmp (mode, "assert-argv0") == 0)
224 if (strcmp (argv[0], "moocow") == 0)
225 return 0;
226 g_printerr ("argv0=%s != moocow\n", argv[0]);
227 return 1;
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);
245 else
247 g_printerr ("Unknown MODE %s\n", argv[1]);
248 return 1;
251 return TRUE;