Skip to content

Commit 01c2bf9

Browse files
authored
cut: use error exit-code if a file argument does not open (#1054)
* cut: use error exit-code if a file argument does not open * When testing cut today I discovered that it uses the idiomatic while(<>) loop for reading input in handle_b() and handle_f() * If file arguments are provided, all should process correctly in order for a success exit-code * Hijack the WARN signal to set exit code; in future handle_b() and handle_f() could be restructured if necessary * If a directory is passed as an argument it is ignored; this is consistent with OpenIndiana & OpenBSD, but GNU cut raises a warning for it * existing warn() messages no longer need $me
1 parent 528774f commit 01c2bf9

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

bin/cut

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,17 @@ use constant EX_SUCCESS => 0;
2020
use constant EX_FAILURE => 1;
2121

2222
my $me = basename($0);
23-
23+
my $rc = EX_SUCCESS;
2424
my %opt;
25+
26+
$SIG{'__WARN__'} = sub {
27+
my $msg = shift;
28+
$msg =~ s/\n*\z/\n/;
29+
print { *STDERR } "$me: $msg";
30+
$rc = EX_FAILURE;
31+
return;
32+
};
33+
2534
getopts('b:c:d:f:ns', \%opt) or usage();
2635

2736
# There's no difference between -b and -c on any unix I
@@ -38,19 +47,19 @@ elsif (defined $opt{'f'}) {
3847
handle_f($opt{'f'}, $opt{'d'}, $opt{'s'});
3948
}
4049
else {
41-
warn "$me: byte, character or field list required\n";
50+
warn "byte, character or field list required\n";
4251
usage();
4352
}
44-
exit EX_SUCCESS;
53+
exit $rc;
4554

4655
sub checknum {
4756
my $n = shift;
4857
if ($n !~ m/\A\-?[0-9]+\Z/) {
49-
warn "$me: unexpected byte or field number: '$n'\n";
58+
warn "unexpected byte or field number: '$n'\n";
5059
exit EX_FAILURE;
5160
}
5261
if ($n == 0) {
53-
warn "$me: bytes and fields are numbered from 1\n";
62+
warn "bytes and fields are numbered from 1\n";
5463
exit EX_FAILURE;
5564
}
5665
}
@@ -69,18 +78,18 @@ sub parse_fields {
6978
$to = $3;
7079
}
7180
else {
72-
warn "$me: invalid byte list: '$item'\n";
81+
warn "invalid byte list: '$item'\n";
7382
exit EX_FAILURE;
7483
}
7584
checknum($from) if length($from);
7685
checknum($to) if length($to);
7786
if (!length($from) && !length($to)) { # reject lone '-'
78-
warn "$me: invalid byte list\n";
87+
warn "invalid byte list\n";
7988
exit EX_FAILURE;
8089
}
8190
if (length($from) && length($to)) {
8291
if ($from > $to) {
83-
warn "$me: invalid range $from-$to\n";
92+
warn "invalid range $from-$to\n";
8493
exit EX_FAILURE;
8594
}
8695
$is_range = 0 if $from == $to;

0 commit comments

Comments
 (0)