GSubprocess examples in C.
Quick start:
// Build command: gcc -o gsubprocess gsubprocess.c `pkgconf --cflags --libs gio-2.0`
#include <gio/gio.h>
int
main(int argc, char** argv)
{
GSubprocess* sub = g_subprocess_new(
G_SUBPROCESS_FLAGS_NONE,
NULL,
"echo", "Hello world!", NULL
);
gboolean success = g_subprocess_wait(sub, NULL, NULL);
g_object_unref(sub);
return 0;
}Read output:
#include <gio/gio.h>
int
main(int argc, char** argv)
{
GSubprocess* sub = g_subprocess_new(
G_SUBPROCESS_FLAGS_STDOUT_PIPE,
NULL,
"echo", "Hello world!", NULL
);
gboolean success = g_subprocess_wait(sub, NULL, NULL);
if (success) {
GInputStream* stdout_pipe = g_subprocess_get_stdout_pipe(sub);
GDataInputStream* data = g_data_input_stream_new(stdout_pipe);
gchar* line = g_data_input_stream_read_line(data, NULL, NULL, NULL);
while (line != NULL) {
g_print("%s\n", line);
line = g_data_input_stream_read_line(data, NULL, NULL, NULL);
}
g_object_unref(data);
g_free(line);
data = NULL;
stdout_pipe = NULL;
}
g_object_unref(sub);
return 0;
}