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
|
#include <gio/gio.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#ifdef G_OS_UNIX
#include <gio/gunixinputstream.h>
#include <gio/gunixoutputstream.h>
#endif
static GOptionEntry options[] = {
{NULL}
};
static void
write_all (int fd,
const guint8* buf,
gsize len)
{
while (len > 0)
{
ssize_t bytes_written = write (fd, buf, len);
if (bytes_written < 0)
g_error ("Failed to write to fd %d: %s",
fd, strerror (errno));
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;
}
int
main (int argc, char **argv)
{
GOptionContext *context;
GError *error = NULL;
const char *mode;
context = g_option_context_new ("MODE - Test GSubprocess stuff");
g_option_context_add_main_entries (context, options, NULL);
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_printerr ("%s: %s\n", argv[0], error->message);
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
{
g_printerr ("Unknown MODE %s\n", argv[1]);
return 1;
}
return TRUE;
}
|