Skip to content

Commit 2906a76

Browse files
authored
Merge pull request #419 from mknos/head-num
head: support -NUM
2 parents 575b8c5 + 8aa9a64 commit 2906a76

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

bin/head

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use constant EX_FAILURE => 1;
2323
my $Program = basename($0);
2424
my ($VERSION) = '1.3';
2525

26+
@ARGV = new_argv();
2627
my %opt;
2728
unless (getopts('n:', \%opt)) {
2829
warn "usage: $Program [-n count] [file ...]\n";
@@ -66,16 +67,16 @@ foreach my $file (@ARGV) {
6667
}
6768
print "==> $file <==\n";
6869
}
69-
tail_fh($fh);
70+
head_fh($fh);
7071
unless (close $fh) {
7172
warn "$Program: failed to close '$file': $!\n";
7273
$rc = EX_FAILURE;
7374
}
7475
}
75-
tail_fh(*STDIN) unless @ARGV;
76+
head_fh(*STDIN) unless @ARGV;
7677
exit $rc;
7778

78-
sub tail_fh {
79+
sub head_fh {
7980
my $fh = shift;
8081

8182
foreach (1 .. $count) {
@@ -85,6 +86,26 @@ sub tail_fh {
8586
}
8687
}
8788

89+
sub new_argv {
90+
my @new;
91+
my $end = 0;
92+
93+
foreach my $arg (@ARGV) {
94+
if ($arg eq '--' || $arg !~ m/\A\-/) {
95+
push @new, $arg;
96+
$end = 1;
97+
next;
98+
}
99+
100+
if (!$end && $arg =~ m/\A\-([0-9]+)\Z/) { # historic
101+
push @new, "-n$1";
102+
} else {
103+
push @new, $arg;
104+
}
105+
}
106+
return @new;
107+
}
108+
88109
__END__
89110
90111
=pod

0 commit comments

Comments
 (0)