From b641a8cf4859a2eb468bc77f6ce98a4b7eeeb759 Mon Sep 17 00:00:00 2001 From: Charles Longeau Date: Tue, 25 Sep 2007 09:51:49 +0000 Subject: check fgets return value use sizeof input intead of BUFSIZ remove unneeded feof handle empty strings returned by fgets properly remove newline and white space at the end of buffer with help and ok ray@ --- sbin/restore/interactive.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'sbin') diff --git a/sbin/restore/interactive.c b/sbin/restore/interactive.c index 860de17dab2..3eb1eb99511 100644 --- a/sbin/restore/interactive.c +++ b/sbin/restore/interactive.c @@ -1,4 +1,4 @@ -/* $OpenBSD: interactive.c,v 1.24 2007/09/02 15:19:25 deraadt Exp $ */ +/* $OpenBSD: interactive.c,v 1.25 2007/09/25 09:51:48 chl Exp $ */ /* $NetBSD: interactive.c,v 1.10 1997/03/19 08:42:52 lukem Exp $ */ /* @@ -34,7 +34,7 @@ #if 0 static char sccsid[] = "@(#)interactive.c 8.3 (Berkeley) 9/13/94"; #else -static const char rcsid[] = "$OpenBSD: interactive.c,v 1.24 2007/09/02 15:19:25 deraadt Exp $"; +static const char rcsid[] = "$OpenBSD: interactive.c,v 1.25 2007/09/25 09:51:48 chl Exp $"; #endif #endif /* not lint */ @@ -327,13 +327,13 @@ getcmd(char *curdir, char *cmd, size_t cmdlen, char *name, size_t namelen, do { (void)fprintf(stderr, "%s > ", __progname); (void)fflush(stderr); - (void)fgets(input, BUFSIZ, terminal); - } while (!feof(terminal) && input[0] == '\n'); - if (feof(terminal)) { - (void)strlcpy(cmd, "quit", cmdlen); - return; - } - for (cp = &input[strlen(input) - 2]; *cp == ' ' || *cp == '\t'; cp--) + if (fgets(input, sizeof input, terminal) == NULL) { + (void)strlcpy(cmd, "quit", cmdlen); + return; + } + } while (input[0] == '\n' || input[0] == '\0'); + for (cp = &input[strlen(input) - 1]; + cp >= input && (*cp == ' ' || *cp == '\t' || *cp == '\n'); cp--) /* trim off trailing white space and newline */; *++cp = '\0'; /* -- cgit v1.2.3