glib/gtester.c:Read the output of the child process
[platform/upstream/glib.git] / glib / gtester.c
1 /* This file is part of gtester
2  *
3  * AUTHORS
4  *     Sven Herzberg  <herzi@gnome-de.org>
5  *
6  * Copyright (C) 2007  Sven Herzberg
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License as
10  * published by the Free Software Foundation; either version 2.1 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  */
23
24 #include <glib.h>
25
26 static void
27 child_watch_cb (GPid     pid,
28                 gint     status,
29                 gpointer data)
30 {
31   GMainLoop* loop = data;
32
33   g_main_loop_quit (loop);
34 }
35
36 static gboolean
37 child_out_cb (GIOChannel  * source,
38               GIOCondition  condition,
39               gpointer      data)
40 {
41   GError* error = NULL;
42   gsize length = 0;
43   gchar buffer[4096];
44   GIOStatus status;
45
46   status = g_io_channel_read_chars (source, buffer, sizeof(buffer), &length, &error);
47   if (status == G_IO_STATUS_NORMAL)
48     {
49       g_print ("%d\n", length);
50       return TRUE;
51     }
52   else
53     {
54       g_print ("Output done: %d\n", status);
55       return FALSE;
56     }
57 }
58
59 int
60 main (int   argc,
61       char**argv)
62 {
63   GMainLoop* loop;
64   GError   * error = NULL;
65   GPid       pid = 0;
66   gchar    * working_folder;
67   gchar    * child_argv[] = {
68     "cat",
69     "/proc/cpuinfo",
70     NULL
71   };
72   gint        child_out;
73   GIOChannel* out;
74
75   working_folder = g_get_current_dir ();
76   g_spawn_async_with_pipes (working_folder,
77                  child_argv, NULL /* envp */,
78                  G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
79                  NULL, NULL,
80                  &pid,
81                  NULL,
82                  &child_out,
83                  NULL,
84                  &error);
85   g_free (working_folder);
86
87   if (error)
88     {
89       g_error ("Couldn't execute child: %s", error->message);
90       /* doesn't return */
91     }
92
93   loop = g_main_loop_new (NULL, FALSE);
94
95   g_child_watch_add (pid,
96                      child_watch_cb,
97                      loop);
98
99   out = g_io_channel_unix_new (child_out);
100   g_io_add_watch (out, G_IO_IN,
101                   child_out_cb, NULL);
102
103   g_main_loop_run (loop);
104   g_main_loop_unref (loop);
105   return 0;
106 }
107