Skip to content

Commit

Permalink
Merge pull request #195 from mknos/split-stdin
Browse files Browse the repository at this point in the history
split: support stdin
  • Loading branch information
briandfoy committed Jul 8, 2023
2 parents 2baf895 + 704bdc1 commit 8ab2ead
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions bin/split
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,14 @@ unless (defined $prefix && length $prefix) {
}

die("$me: $infile: is a directory\n") if (-d $infile);
open (INFILE, '<', $infile) || die "$me: Can't open $infile: $!\n";
binmode(INFILE);
my $in;
if ($infile eq '-') {
$in = *STDIN;
}
else {
open($in, '<', $infile) or die("$me: Can't open $infile: $!\n");
}
binmode $in;
## Byte operations.
if ($opt{b} and (! $opt{p}) and (! $opt{l}) and (! $opt{"?"})) {

Expand All @@ -119,16 +125,16 @@ if ($opt{b} and (! $opt{p}) and (! $opt{l}) and (! $opt{"?"})) {

unless ($count) { die qq($me: "$opt{b}" is invalid number of bytes.\n) }

while (read (INFILE, $chunk, $count) == $count) {
while (read ($in, $chunk, $count) == $count) {
$fh = nextfile ($prefix);
print $fh $chunk;
print {$fh} $chunk;
}

# leftover bit. Last file will be >= $count.
# There's gotta be something more elegant than this, too.
if (length($chunk)) {
$fh = nextfile ($prefix);
print $fh $chunk;
print {$fh} $chunk;
}
}

Expand All @@ -138,9 +144,9 @@ elsif ($opt{p} and (! $opt{b}) and (! $opt{l}) and (! $opt{"?"})) {
my $regex = $opt{p};
my $fh = nextfile ($prefix);

while (<INFILE>) {
while (<$in>) {
$fh = nextfile ($prefix) if /$regex/;
print $fh $_;
print {$fh} $_;
}
}

Expand All @@ -154,16 +160,19 @@ elsif ((! $opt{p}) and (! $opt{b}) and (! $opt{"?"})) {

unless ($count) { die qq($me: "$opt{l}" is invalid number of lines.\n) }

while (<INFILE>) {
while (<$in>) {
$fh = nextfile ($prefix) if $line % $count == 0;
print $fh $_;
print {$fh} $_;
$line++;
}

}

else { clue };

close $in;
exit;

# (Thanks to Abigail for the pod template.)

=pod
Expand Down Expand Up @@ -243,4 +252,3 @@ distribute and sell this program (and any modified variants) in any
way you wish, provided you do not restrict others to do the same.
=cut

0 comments on commit 8ab2ead

Please sign in to comment.