Module Name:    src
Committed By:   dholland
Date:           Fri Mar  4 03:02:52 UTC 2016

Modified Files:
        src/usr.bin/hexdump: display.c

Log Message:
Don't try to use stdin after clobbering it with a failed freopen().
Prevents an extra "Bad file descriptor" message when trying to hexdump
a single nonexistent file.

The intended behavior seems to have been to read from stdin if there
was one filename given and it wasn't valid. But this seems like a bad
idea, so prevent that case instead of hacking it up so it works.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/usr.bin/hexdump/display.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/hexdump/display.c
diff -u src/usr.bin/hexdump/display.c:1.24 src/usr.bin/hexdump/display.c:1.25
--- src/usr.bin/hexdump/display.c:1.24	Fri Mar  4 02:54:38 2016
+++ src/usr.bin/hexdump/display.c	Fri Mar  4 03:02:52 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: display.c,v 1.24 2016/03/04 02:54:38 dholland Exp $	*/
+/*	$NetBSD: display.c,v 1.25 2016/03/04 03:02:52 dholland Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)display.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: display.c,v 1.24 2016/03/04 02:54:38 dholland Exp $");
+__RCSID("$NetBSD: display.c,v 1.25 2016/03/04 03:02:52 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -297,12 +297,32 @@ get(void)
 	}
 }
 
+/*
+ * Save argv for later retrieval.
+ */
 void
 stashargv(char **argv)
 {
 	_argv = argv;
 }
 
+/*
+ * Get the next file. The idea with the twisty logic seems to be to
+ * either read N filenames from argv and then exit, or if there aren't
+ * any, to use stdin and then exit. It should probably be simplified.
+ * The "done" flag doesn't mean "we are done", it means "we are done
+ * once we run out of filenames".
+ *
+ * XXX: is there any reason not to remove the logic that inhibits
+ * calling fstat if using stdin and not a filename? It should be safe
+ * to call fstat on any fd.
+ *
+ * Note: I have ruled that if there is one file on the command line
+ * and it doesn't open, we should exit after complaining about it and
+ * not then proceed to read stdin; the latter seems like unexpected
+ * and undesirable behavior. Also, it didn't work anyway, because the
+ * freopen call clobbers stdin while failing. -- dholland 20160303
+ */
 int
 next(void)
 {
@@ -311,13 +331,14 @@ next(void)
 
 	for (;;) {
 		if (*_argv) {
+			done = 1;
 			if (!(freopen(*_argv, "r", stdin))) {
 				warn("%s", *_argv);
 				exitval = 1;
 				++_argv;
 				continue;
 			}
-			statok = done = 1;
+			statok = 1;
 		} else {
 			if (done++)
 				return(0);

Reply via email to