From 99e3d051392536e0e971e6ab11cf6d63e00f4cc7 Mon Sep 17 00:00:00 2001 From: Cameron Ross Date: Sun, 15 Dec 2013 23:49:35 -0400 Subject: [PATCH] Exit gracefully when stdout becomes invalid --- src/nyancat.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/nyancat.c b/src/nyancat.c index 4f4d887..f35f1c6 100644 --- a/src/nyancat.c +++ b/src/nyancat.c @@ -185,6 +185,14 @@ void SIGALRM_handler(int sig) { /* Unreachable */ } +/* + * Handle the loss of stdout, as would be the case when + * in telnet mode and the client disconnects + */ +void SIGPIPE_handler(int sig) { + finish(); +} + /* * Telnet requires us to send a specific sequence * for a line break (\r\000\n), so let's make it happy. @@ -563,6 +571,9 @@ int main(int argc, char ** argv) { int always_escape = 0; /* Used for text mode */ /* Accept ^C -> restore cursor */ signal(SIGINT, SIGINT_handler); + + /* Handle loss of stdout */ + signal(SIGPIPE, SIGPIPE_handler); switch (ttype) { case 1: colors[','] = "\033[48;5;17m"; /* Blue background */