gtestutils: Use stdio rather than write() to be Windows-friendly

Windows doesn't define STDOUT_FILENO and STDERR_FILENO, and they're
not even guaranteed to be 1 and 2. So just use stdio instead. Also fix
a counting error. Pointed out on gtk-devel-list.
This commit is contained in:
Dan Winship 2013-05-22 14:20:08 -03:00
parent c9cc0beb3a
commit 9115dd0a7c

View File

@ -2252,7 +2252,7 @@ child_read (GIOChannel *io, GIOCondition cond, gpointer user_data)
GIOStatus status; GIOStatus status;
gsize nread, nwrote, total; gsize nread, nwrote, total;
gchar buf[4096]; gchar buf[4096];
int echo_fd = -1; FILE *echo_file = NULL;
status = g_io_channel_read_chars (io, buf, sizeof (buf), &nread, NULL); status = g_io_channel_read_chars (io, buf, sizeof (buf), &nread, NULL);
if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF) if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF)
@ -2273,25 +2273,22 @@ child_read (GIOChannel *io, GIOCondition cond, gpointer user_data)
{ {
g_string_append_len (data->stdout_str, buf, nread); g_string_append_len (data->stdout_str, buf, nread);
if (data->echo_stdout) if (data->echo_stdout)
echo_fd = STDOUT_FILENO; echo_file = stdout;
} }
else else
{ {
g_string_append_len (data->stderr_str, buf, nread); g_string_append_len (data->stderr_str, buf, nread);
if (data->echo_stderr) if (data->echo_stderr)
echo_fd = STDERR_FILENO; echo_file = stderr;
} }
if (echo_fd != -1) if (echo_file)
{ {
for (total = 0; total < nread; total += nwrote) for (total = 0; total < nread; total += nwrote)
{ {
do nwrote = fwrite (buf + total, 1, nread - total, echo_file);
nwrote = write (echo_fd, buf + total, nread - total); if (nwrote == 0)
while (nwrote == -1 && errno == EINTR);
if (nwrote == -1)
g_error ("write failed: %s", g_strerror (errno)); g_error ("write failed: %s", g_strerror (errno));
total += nwrote;
} }
} }