From 474fdebb7abf08aecbcf00a92b7d53cd9528649c Mon Sep 17 00:00:00 2001
From: Nicholas Marriott <nicm@openbsd.org>
Date: Tue, 7 Jul 2009 17:24:32 +0000
Subject: [PATCH] Handle empty or unset TERM correctly; also fix a fatal()
 message while here.

---
 server-msg.c | 4 +++-
 tty.c        | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/server-msg.c b/server-msg.c
index c52fe800..9f33fad3 100644
--- a/server-msg.c
+++ b/server-msg.c
@@ -211,7 +211,9 @@ server_msg_fn_identify(struct hdr *hdr, struct client *c)
 		c->tty.term_flags |= TERM_88COLOURS;
 	if (data.flags & IDENTIFY_HASDEFAULTS)
 		c->tty.term_flags |= TERM_HASDEFAULTS;
-	xfree(term);
+
+	if (term != NULL)
+		xfree(term);
 
 	c->flags |= CLIENT_TERMINAL;
 
diff --git a/tty.c b/tty.c
index 75986ef0..1ce8b226 100644
--- a/tty.c
+++ b/tty.c
@@ -76,7 +76,7 @@ void
 tty_init(struct tty *tty, char *path, char *term)
 {
 	tty->path = xstrdup(path);
-	if (term == NULL)
+	if (term == NULL || *term == '\0')
 		tty->termname = xstrdup("unknown");
 	else
 		tty->termname = xstrdup(term);
@@ -98,7 +98,7 @@ tty_open(struct tty *tty, char **cause)
 	if ((mode = fcntl(tty->fd, F_GETFL)) == -1)
 		fatal("fcntl failed");
 	if (fcntl(tty->fd, F_SETFL, mode|O_NONBLOCK) == -1)
-		fatal("fcntl failedo");
+		fatal("fcntl failed");
 	if (fcntl(tty->fd, F_SETFD, FD_CLOEXEC) == -1)
 		fatal("fcntl failed");