From 2be150a7799fab25c332f41414c907ab78f52324 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Mon, 13 May 2024 08:28:55 +0800 Subject: [PATCH] wc: indicate error in exitcode * Follow GNU and OpenBSD versions by doing non-zero exit() if one or more file arguments cannot be processed * Add return check for close() as done in other scripts --- bin/wc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/bin/wc b/bin/wc index 32934c55..f5995b70 100755 --- a/bin/wc +++ b/bin/wc @@ -198,19 +198,25 @@ sub wc_fh { print "$out"; } +my $rc = EX_SUCCESS; if (@ARGV) { foreach my $filename (@ARGV) { if (-d $filename) { warn "$Program: '$filename' is a directory\n"; + $rc = EX_FAILURE; next; } my $fh; unless (open $fh, '<', $filename) { - warn "$Program: '$filename': $!\n"; + warn "$Program: failed to open '$filename': $!\n"; + $rc = EX_FAILURE; next; } wc_fh($fh, $filename); - close $fh; + unless (close $fh) { + warn "$Program: failed to close '$filename': $!\n"; + $rc = EX_FAILURE; + } } } else { wc_fh(\*STDIN); @@ -225,6 +231,7 @@ if ($#ARGV >= 1) { $out = sprintf(" %9u%s",$total_paras,$out) if ($opt{'p'}); print "$out"; } +exit $rc; __END__