Fix PR#2 in a simplistic way. Handle EOF on all getchar()s
authorniklas <niklas@openbsd.org>
Sat, 11 Jan 1997 10:01:27 +0000 (10:01 +0000)
committerniklas <niklas@openbsd.org>
Sat, 11 Jan 1997 10:01:27 +0000 (10:01 +0000)
games/adventure/hdr.h
games/adventure/io.c
games/adventure/wizard.c

index e56a8ee..0143e56 100644 (file)
@@ -55,6 +55,7 @@
 
 /* hdr.h: included by c advent files */
 #include <sys/types.h>
+#include <stdio.h>
 
 int datfd;                              /* message file descriptor      */
 int delhit;
@@ -63,7 +64,7 @@ extern char data_file[];                /* Virtual data file            */
 
 #define TAB     011
 #define LF      012
-#define FLUSHLINE while (getchar()!='\n')
+#define FLUSHLINE do { int c; while ((c = getchar()) != EOF && c != '\n'); } while (0)
 #define FLUSHLF   while (next()!=LF)
 
 int loc,newloc,oldloc,oldlc2,wzdark,gaveup,kq,k,k2;
index 86f0c85..fb7686f 100644 (file)
@@ -83,6 +83,9 @@ char **wrd1,**wrd2;                     /* no prompt, usually           */
                                *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");
@@ -99,10 +102,14 @@ char **wrd1,**wrd2;                     /* no prompt, usually           */
 confirm(mesg)                           /* confirm irreversible action  */
 char *mesg;
 {       register int result;
+       register int c;
        printf("%s",mesg);              /* tell him what he did         */
-       if (getchar()=='y')             /* was his first letter a 'y'?  */
+       if ((c = getchar())=='y')             /* was his first letter a 'y'?  */
                result=1;
-       else    result=0;
+       else if (c == EOF) {
+               printf("user closed input stream, quitting...\n");
+               exit(0);
+       } else result=0;
        FLUSHLINE;
        return(result);
 }
@@ -110,12 +117,16 @@ char *mesg;
 yes(x,y,z)                              /* confirm with rspeak          */
 int x,y,z;
 {       register int result;
-       register char ch;
+       register 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 == EOF) {
+                       printf("user closed input stream, quitting...\n");
+                       exit(0);
+               }
                FLUSHLINE;
                if (ch=='y'|| ch=='n') break;
                printf("Please answer the question.\n");
@@ -134,6 +145,10 @@ int x,y,z;
                if ((ch=getchar())=='y')
                        result=TRUE;
                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;
                printf("Please answer the question.\n");
index 2ff3f5f..4722391 100644 (file)
@@ -120,7 +120,7 @@ char *cmdfile;
 
        printf("What would you like to call the saved version?\n");
        for (c=fname;; c++)
-               if ((*c=getchar())=='\n') break;
+               if ((*c=getchar())=='\n' || *c == EOF) break;
        *c=0;
        if (save(fname) != 0) return;           /* Save failed */
        printf("To resume, say \"adventure %s\".\n", fname);