Skip to content

Commit

Permalink
chafa: Add error handling for stdout writes
Browse files Browse the repository at this point in the history
Fixes Github #44.
  • Loading branch information
hpjansson committed Aug 6, 2021
1 parent 8a96b70 commit 5d65bb8
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions tools/chafa/chafa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,12 @@ interruptible_usleep (gint us)
}
}

static gboolean
write_to_stdout (gpointer buf, gsize len)
{
return fwrite (buf, 1, len, stdout) == len ? TRUE : FALSE;
}

/* FIXME: This function is ripe for refactoring, probably to something with
* a heap-allocated context. */
static gboolean
Expand Down Expand Up @@ -1471,14 +1477,20 @@ run_magickwand (const gchar *filename, gboolean is_first_file, gboolean is_first
if (is_first_frame && !options.clear && !is_first_file)
*(p0++) = '\n';

fwrite (buf, sizeof (gchar), p0 - buf, stdout);
fwrite (frame->gs->str, sizeof (gchar), frame->gs->len, stdout);
if (!write_to_stdout (buf, p0 - buf))
goto out;
if (!write_to_stdout (frame->gs->str, frame->gs->len))
goto out;

/* No linefeed after frame in sixel mode */
if (options.pixel_mode == CHAFA_PIXEL_MODE_SYMBOLS)
fputc ('\n', stdout);
{
if (!write_to_stdout ("\n", 1))
goto out;
}

fflush (stdout);
if (fflush (stdout) != 0)
goto out;

if (is_animation)
{
Expand Down Expand Up @@ -1623,14 +1635,20 @@ run_gif (const gchar *filename, gboolean is_first_file, gboolean is_first_frame,
if (is_first_frame && !options.clear && !is_first_file)
*(p0++) = '\n';

fwrite (buf, sizeof (gchar), p0 - buf, stdout);
fwrite (frame->gs->str, sizeof (gchar), frame->gs->len, stdout);
if (!write_to_stdout (buf, p0 - buf))
goto out;
if (!write_to_stdout (frame->gs->str, frame->gs->len))
goto out;

/* No linefeed after frame in sixel mode */
if (options.pixel_mode == CHAFA_PIXEL_MODE_SYMBOLS)
fputc ('\n', stdout);
{
if (!write_to_stdout ("\n", 1))
goto out;
}

fflush (stdout);
if (fflush (stdout) != 0)
goto out;

if (is_animation)
{
Expand Down

0 comments on commit 5d65bb8

Please sign in to comment.