fully switch quiz(6) to getline; diff from Ben Fuller (ben [at] bvnf
authorop <op@openbsd.org>
Mon, 8 Aug 2022 17:54:08 +0000 (17:54 +0000)
committerop <op@openbsd.org>
Mon, 8 Aug 2022 17:54:08 +0000 (17:54 +0000)
space), ok millert@

games/quiz/quiz.c

index d4fd060..38e629b 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: quiz.c,v 1.31 2021/03/11 21:18:25 naddy Exp $ */
+/*     $OpenBSD: quiz.c,v 1.32 2022/08/08 17:54:08 op Exp $    */
 /*     $NetBSD: quiz.c,v 1.9 1995/04/22 10:16:58 cgd Exp $     */
 
 /*-
@@ -224,12 +224,16 @@ quiz(void)
 {
        QE *qp;
        int i;
-       size_t len;
+       size_t size;
+       ssize_t len;
        u_int guesses, rights, wrongs;
        int next;
        char *answer, *t, question[LINE_SZ];
        const char *s;
 
+       size = 0;
+       answer = NULL;
+
        guesses = rights = wrongs = 0;
        for (;;) {
                if (qsize == 0)
@@ -278,7 +282,7 @@ quiz(void)
                qp->q_asked = TRUE;
                (void)printf("%s?\n", question);
                for (;; ++guesses) {
-                       if ((answer = fgetln(stdin, &len)) == NULL ||
+                       if ((len = getline(&answer, &size, stdin)) == -1 ||
                            answer[len - 1] != '\n') {
                                score(rights, wrongs, guesses);
                                exit(0);
@@ -302,6 +306,7 @@ quiz(void)
                }
        }
        score(rights, wrongs, guesses);
+       free(answer);
 }
 
 const char *