From 5bca13d3536be2816d3c9452b0ca2e4081c3bb55 Mon Sep 17 00:00:00 2001 From: dholland Date: Sat, 23 Feb 2008 20:12:15 +0000 Subject: Sanity fixes for input buffer handling. From OpenBSD --- monop/getinp.c | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'monop') diff --git a/monop/getinp.c b/monop/getinp.c index 4e1c2881..92546ac6 100644 --- a/monop/getinp.c +++ b/monop/getinp.c @@ -1,4 +1,4 @@ -/* $NetBSD: getinp.c,v 1.16 2008/02/19 09:45:02 dholland Exp $ */ +/* $NetBSD: getinp.c,v 1.17 2008/02/23 20:12:15 dholland Exp $ */ /* * Copyright (c) 1980, 1993 @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)getinp.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: getinp.c,v 1.16 2008/02/19 09:45:02 dholland Exp $"); +__RCSID("$NetBSD: getinp.c,v 1.17 2008/02/23 20:12:15 dholland Exp $"); #endif #endif /* not lint */ @@ -55,18 +55,13 @@ getinp(prompt, lst) { int i, n_match, match = 0; char *sp; - int c; for (;;) { printf("%s", prompt); - for (sp = buf; (c = getchar()) != '\n'; ) { - if (c == -1) - return 0; - *sp = c; - if (sp != buf || *sp != ' ') - sp++; + fgets(buf, sizeof(buf), stdin); + if (feof(stdin)) { + return 0; } - *sp = c; if (buf[0] == '?' && buf[1] == '\n') { printf("Valid inputs are: "); for (i = 0, match = 18; lst[i]; i++) { @@ -88,7 +83,8 @@ getinp(prompt, lst) } continue; } - *sp = '\0'; + if ((sp = strchr(buf, '\n')) != NULL) + *sp = '\0'; for (sp = buf; *sp; sp++) *sp = tolower((unsigned char)*sp); for (i = n_match = 0; lst[i]; i++) -- cgit v1.2.3-56-ge451