summaryrefslogtreecommitdiff
path: root/gio/tests/gsubprocess-testprog.c
blob: eee759dcd7de8c09028679b92e5d2b3b921bcb0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <gio/gio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifdef G_OS_UNIX
#include <unistd.h>
#else
#include <io.h>
#endif

static GOptionEntry options[] = {
  G_OPTION_ENTRY_NULL
};

static void
write_all (int           fd,
	   const guint8* buf,
	   gsize         len)
{
  while (len > 0)
    {
      gssize bytes_written = write (fd, buf, len);
      int errsv = errno;
      if (bytes_written < 0)
	g_error ("Failed to write to fd %d: %s",
		 fd, g_strerror (errsv));
      buf += bytes_written;
      len -= bytes_written;
    }
}

static int
echo_mode (int argc,
	   char **argv)
{
  int i;

  for (i = 2; i < argc; i++)
    {
      write_all (1, (guint8*)argv[i], strlen (argv[i]));
      write_all (1, (guint8*)"\n", 1);
    }

  return 0;
}

static int
echo_stdout_and_stderr_mode (int argc,
			     char **argv)
{
  int i;

  for (i = 2; i < argc; i++)
    {
      write_all (1, (guint8*)argv[i], strlen (argv[i]));
      write_all (1, (guint8*)"\n", 1);
      write_all (2, (guint8*)argv[i], strlen (argv[i]));
      write_all (2, (guint8*)"\n", 1);
    }

  return 0;
}

static int
cat_mode (int argc,
	  char **argv)
{
  GIOChannel *chan_stdin;
  GIOChannel *chan_stdout;
  GIOStatus status;
  char buf[1024];
  gsize bytes_read, bytes_written;
  GError *local_error = NULL;
  GError **error = &local_error;

  chan_stdin = g_io_channel_unix_new (0);
  g_io_channel_set_encoding (chan_stdin, NULL, error);
  g_assert_no_error (local_error);
  chan_stdout = g_io_channel_unix_new (1);
  g_io_channel_set_encoding (chan_stdout, NULL, error);
  g_assert_no_error (local_error);

  while (TRUE)
    {
      do
	status = g_io_channel_read_chars (chan_stdin, buf, sizeof (buf),
					  &bytes_read, error);
      while (status == G_IO_STATUS_AGAIN);

      if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
	break;

      do
	status = g_io_channel_write_chars (chan_stdout, buf, bytes_read,
					   &bytes_written, error);
      while (status == G_IO_STATUS_AGAIN);

      if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
	break;
    }

  g_io_channel_unref (chan_stdin);
  g_io_channel_unref (chan_stdout);

  if (local_error)
    {
      g_printerr ("I/O error: %s\n", local_error->message);
      g_clear_error (&local_error);
      return 1;
    }
  return 0;
}

static gint
sleep_forever_mode (int argc,
		    char **argv)
{
  GMainLoop *loop;
  
  loop = g_main_loop_new (NULL, TRUE);
  g_main_loop_run (loop);

  return 0;
}

static int
write_to_fds (int argc, char **argv)
{
  int i;

  for (i = 2; i < argc; i++)
    {
      int fd = atoi (argv[i]);
      FILE *f = fdopen (fd, "w");
      const char buf[] = "hello world\n";
      size_t bytes_written;
      
      g_assert (f != NULL);
      
      bytes_written = fwrite (buf, 1, sizeof (buf), f);
      g_assert (bytes_written == sizeof (buf));
      
      if (fclose (f) == -1)
        g_assert_not_reached ();
    }

  return 0;
}

static int
read_from_fd (int argc, char **argv)
{
  int fd;
  const char expected_result[] = "Yay success!";
  guint8 buf[sizeof (expected_result) + 1];
  gsize bytes_read;
  FILE *f;

  if (argc != 3)
    {
      g_print ("Usage: %s read-from-fd FD\n", argv[0]);
      return 1;
    }

  fd = atoi (argv[2]);
  if (fd == 0)
    {
      g_warning ("Argument \"%s\" does not look like a valid nonzero file descriptor", argv[2]);
      return 1;
    }

  f = fdopen (fd, "r");
  if (f == NULL)
    {
      g_warning ("Failed to open fd %d: %s", fd, g_strerror (errno));
      return 1;
    }

  bytes_read = fread (buf, 1, sizeof (buf), f);
  if (bytes_read != sizeof (expected_result))
    {
      g_warning ("Read %zu bytes, but expected %zu", bytes_read, sizeof (expected_result));
      return 1;
    }

  if (memcmp (expected_result, buf, sizeof (expected_result)) != 0)
    {
      buf[sizeof (expected_result)] = '\0';
      g_warning ("Expected \"%s\" but read  \"%s\"", expected_result, (char *)buf);
      return 1;
    }

  if (fclose (f) == -1)
    g_assert_not_reached ();

  return 0;
}

static int
env_mode (int argc, char **argv)
{
  char **env;
  int i;

  env = g_get_environ ();

  for (i = 0; env[i]; i++)
    g_print ("%s\n", env[i]);

  g_strfreev (env);

  return 0;
}

static int
cwd_mode (int argc, char **argv)
{
  char *cwd;

  cwd = g_get_current_dir ();
  g_print ("%s\n", cwd);
  g_free (cwd);

  return 0;
}

static int
printenv_mode (int argc, char **argv)
{
  gint i;

  for (i = 2; i < argc; i++)
    {
      const gchar *value = g_getenv (argv[i]);

      if (value != NULL)
        g_print ("%s=%s\n", argv[i], value);
    }

  return 0;
}

int
main (int argc, char **argv)
{
  GOptionContext *context;
  GError *error = NULL;
  const char *mode;
  gboolean ret;

  context = g_option_context_new ("MODE - Test GSubprocess stuff");
  g_option_context_add_main_entries (context, options, NULL);
  ret = g_option_context_parse (context, &argc, &argv, &error);
  g_option_context_free (context);

  if (!ret)
    {
      g_printerr ("%s: %s\n", argv[0], error->message);
      g_error_free (error);
      return 1;
    }

  if (argc < 2)
    {
      g_printerr ("MODE argument required\n");
      return 1;
    }

  mode = argv[1];
  if (strcmp (mode, "noop") == 0)
    return 0;
  else if (strcmp (mode, "exit1") == 0)
    return 1;
  else if (strcmp (mode, "assert-argv0") == 0)
    {
      if (strcmp (argv[0], "moocow") == 0)
	return 0;
      g_printerr ("argv0=%s != moocow\n", argv[0]);
      return 1;
    }
  else if (strcmp (mode, "echo") == 0)
    return echo_mode (argc, argv);
  else if (strcmp (mode, "echo-stdout-and-stderr") == 0)
    return echo_stdout_and_stderr_mode (argc, argv);
  else if (strcmp (mode, "cat") == 0)
    return cat_mode (argc, argv);
  else if (strcmp (mode, "sleep-forever") == 0)
    return sleep_forever_mode (argc, argv);
  else if (strcmp (mode, "write-to-fds") == 0)
    return write_to_fds (argc, argv);
  else if (strcmp (mode, "read-from-fd") == 0)
    return read_from_fd (argc, argv);
  else if (strcmp (mode, "env") == 0)
    return env_mode (argc, argv);
  else if (strcmp (mode, "cwd") == 0)
    return cwd_mode (argc, argv);
  else if (strcmp (mode, "printenv") == 0)
    return printenv_mode (argc, argv);
  else
    {
      g_printerr ("Unknown MODE %s\n", argv[1]);
      return 1;
    }

  return TRUE;
}