Subject: bin/6556: [PATCH] Adventure EOF handling
To: None <gnats-bugs@gnats.netbsd.org>
From: Joseph Myers <jsm28@cam.ac.uk>
List: netbsd-bugs
Date: 12/09/1998 17:42:57
>Number:         6556
>Category:       bin
>Synopsis:       [PATCH] Adventure EOF handling
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Dec  9 10:20:01 1998
>Last-Modified:
>Originator:     Joseph S. Myers
>Organization:
Trinity College, University of Cambridge, UK
>Release:        NetBSD-current of 1998-12-07
>Environment:
[
System: Linux decomino 2.0.36 #1 Mon Nov 16 14:25:34 UTC 1998 i686 unknown
Architecture: i686
]
>Description:

The game adventure(6) handles EOF on standard input rather
ungracefully.  The patch below, derived from OpenBSD, improves this
handling.

>How-To-Repeat:

>Fix:

diff -ruN adventure/hdr.h adventure+/hdr.h
--- adventure/hdr.h	Sun Aug 30 11:05:03 1998
+++ adventure+/hdr.h	Sun Aug 30 20:48:02 1998
@@ -64,7 +64,7 @@
 
 #define TAB     011
 #define LF      012
-#define FLUSHLINE while (getchar()!='\n')
+#define FLUSHLINE do { int flushline_ch; while ((flushline_ch = getchar()) != EOF && flushline_ch != '\n'); } while (0)
 #define FLUSHLF   while (next()!=LF)
 
 int     loc, newloc, oldloc, oldlc2, wzdark, gaveup, kq, k, k2;
diff -ruN adventure/io.c adventure+/io.c
--- adventure/io.c	Mon Sep 14 11:04:45 1998
+++ adventure+/io.c	Tue Sep 15 11:03:38 1998
@@ -89,6 +89,9 @@
 				*s = 0;
 				return;
 			}
+		case EOF:
+			printf("user closed input stream, quitting...\n");
+			exit(0);
 		default:
 			if (++numch >= MAXSTR) {	/* string too long */
 				printf("Give me a break!!\n");
@@ -106,14 +109,17 @@
 	int     x, y, z;
 {
 	int     result = TRUE;	/* pacify gcc */
-	char    ch;
+	int    ch;
 	for (;;) {
 		rspeak(x);	/* tell him what we want */
 		if ((ch = getchar()) == 'y')
 			result = TRUE;
-		else
-			if (ch == 'n')
-				result = FALSE;
+		else if (ch == 'n')
+			result = FALSE;
+		else if (ch == EOF) {
+			printf("user closed input stream, quitting...\n");
+			exit(0);
+		}
 		FLUSHLINE;
 		if (ch == 'y' || ch == 'n')
 			break;
@@ -131,14 +137,17 @@
 	int     x, y, z;
 {
 	int     result = TRUE;	/* pacify gcc */
-	char    ch;
+	int    ch;
 	for (;;) {
 		mspeak(x);	/* tell him what we want */
 		if ((ch = getchar()) == 'y')
 			result = TRUE;
-		else
-			if (ch == 'n')
-				result = FALSE;
+		else if (ch == 'n')
+			result = FALSE;
+		else if (ch == EOF) {
+			printf("user closed input stream, quitting...\n");
+			exit(0);
+		}
 		FLUSHLINE;
 		if (ch == 'y' || ch == 'n')
 			break;
diff -ruN adventure/wizard.c adventure+/wizard.c
--- adventure/wizard.c	Tue Aug 25 11:05:17 1998
+++ adventure+/wizard.c	Tue Dec  8 14:16:17 1998
@@ -136,9 +136,14 @@
 	char    fname[80];
 
 	printf("What would you like to call the saved version?\n");
-	for (c = fname;; c++)
-		if ((*c = getchar()) == '\n')
+	/* XXX - should use fgetln to avoid arbitrary limit */
+	for (c = fname; c < fname + sizeof fname - 1; c++) {
+		int ch;
+		ch = getchar();
+		if (ch == '\n' || ch == EOF)
 			break;
+		*c = ch;
+	}
 	*c = 0;
 	if (save(fname) != 0)
 		return;		/* Save failed */
>Audit-Trail:
>Unformatted: