From df412914dc7b4ea7bd3324e69d0fe19cb80c7ed1 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Sun, 28 Jan 2024 21:06:28 +0800 Subject: [PATCH] cat: explicitly close files * Guarantee that if close() failed for file1 that we print a warning for file1 before proceeding to process file2, then eventually exit with error code * To preserve "cat - -", make sure *STDIN is not closed (on my linux system I type ctrl-d twice to terminate do_file() with EOF) --- bin/cat | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/cat b/bin/cat index 5b7d5cc9..c89c08ac 100755 --- a/bin/cat +++ b/bin/cat @@ -93,6 +93,10 @@ sub do_file { print; } + if ($name ne '-' && !close($fh)) { + warn "$Program: failed to close '$name': $!\n"; + return 1; + } return 0; }