Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/hexdump Don't try to use stdin after clobbering it w...



details:   https://anonhg.NetBSD.org/src/rev/dc5e20498eeb
branches:  trunk
changeset: 814065:dc5e20498eeb
user:      dholland <dholland%NetBSD.org@localhost>
date:      Fri Mar 04 03:02:52 2016 +0000

description:
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.

diffstat:

 usr.bin/hexdump/display.c |  27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diffs (67 lines):

diff -r 4581f1c1cd8a -r dc5e20498eeb usr.bin/hexdump/display.c
--- a/usr.bin/hexdump/display.c Fri Mar 04 02:54:38 2016 +0000
+++ b/usr.bin/hexdump/display.c Fri Mar 04 03:02:52 2016 +0000
@@ -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 @@
        }
 }
 
+/*
+ * 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 @@
 
        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);



Home | Main Index | Thread Index | Old Index