Skip to content

Commit

Permalink
Merge pull request #155 from mknos/sum-stdin
Browse files Browse the repository at this point in the history
sum: read stdin
  • Loading branch information
briandfoy committed Apr 29, 2023
2 parents 5ae5b25 + a2f3b5e commit 0b74785
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions bin/sum
Expand Up @@ -16,9 +16,9 @@ License:
# An implementation of the 'cksum' utility in Perl. Written for the Perl
# Power Tools (PPT) project by Theo Van Dinter (felicity@kluge.net).

use strict;
use integer;
use Getopt::Std;
use strict;
use vars qw($opt_h $opt_o);

&help unless getopts('ho:');
Expand All @@ -33,9 +33,16 @@ $opt_o = 1 unless ( $0 =~ /cksum$/ || defined($opt_o) );
my($exitval) = 0; # return value

foreach (@ARGV) {
open(IN, '<', $_) || die "Can't open file for reading:$!";
my $fh;
if ($_ eq '-') {
$fh = *STDIN;
} elsif (!open($fh, '<', $_)) {
warn "$0: $_: $!\n";
$exitval = 1;
next;
}
my($rval,$crc,$len)=
($opt_o==0)?&crc32(\*IN):($opt_o==1)?&sum1(\*IN):&sum2(\*IN);
($opt_o==0)?&crc32($fh):($opt_o==1)?&sum1($fh):&sum2($fh);
unless ( defined($rval) && ($rval == 0) ) {
warn "$0: $_: $!\n";
$exitval = 1;
Expand All @@ -44,7 +51,7 @@ foreach (@ARGV) {

# Display output information
printf "%lu %lu%s\n",$crc,$len,($_ eq "-")?"":" $_";
close(IN);
close($fh) if ($_ ne '-');
}

exit $exitval;
Expand Down

0 comments on commit 0b74785

Please sign in to comment.