Skip to content

Commit

Permalink
file: avoid exit(0) for stdin case (#571)
Browse files Browse the repository at this point in the history
* file: avoid exit(0) for stdin case

* Usage "file -" is allowed for GNU and OpenBSD: explicitly read stdin
* The usage "file" with no argument is not valid; stdin is not read implicitly
* This version doesn't support "file -", but the argument loop terminates and results in exit(0)
* Using die for this not-implemented error results in non-zero exit code, which is more compatible with the standard[1]

1. https://pubs.opengroup.org/onlinepubs/007904975/utilities/file.html

* exit directly for better control of exit codes
  • Loading branch information
mknos committed Apr 23, 2024
1 parent 6cc25e0 commit 302bad2
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions bin/file
Expand Up @@ -22,6 +22,9 @@ use FindBin;
use FileHandle;
use Getopt::Long;

use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;

my $F = $FindBin::Script;

# translation of type in magic file to unpack template and byte count
Expand Down Expand Up @@ -121,8 +124,10 @@ GetOptions(

# the names of the files are in $fileList.
if ($fileList) {
my $fileListFH = FileHandle->new($fileList, 'r') or
die "$F: $fileList: $!\n";
my $fileListFH = FileHandle->new($fileList, 'r') or do {
warn "$F: $fileList: $!\n";
exit EX_FAILURE;
};
unshift(@ARGV,<$fileListFH>);
$fileListFH->close();
}
Expand All @@ -132,7 +137,8 @@ if ( ! -f $magicFile ) {
# have a fallback for now until a distribution heirarchy is done.
# this works on many unix systems.
if (! -f "/etc/magic" ) {
die "$F: Can't find magic file either in $magicFile or /etc/magic.\n";
warn "$F: Can't find magic file either in $magicFile or /etc/magic.\n";
exit EX_FAILURE;
}
$magicFile = "/etc/magic";
}
Expand All @@ -146,7 +152,10 @@ print STDERR "Using magic file $magicFile\n" if $checkMagic;

# $MF is the magic file state: [ filehandle, buffered last line, line num ]
my $MF = [];
$$MF[0] = FileHandle->new($magicFile, 'r') or die "$magicFile: $!\n";
$$MF[0] = FileHandle->new($magicFile, 'r') or do {
warn "$F: $magicFile: $!\n";
exit EX_FAILURE;
};
$$MF[1] = undef;
$$MF[2] = 0;
readMagicEntry(\@magic,$MF);
Expand All @@ -158,18 +167,18 @@ for my $file (@ARGV) {
# BSD's file just reads the first line. Sounds reasonable, but
# a hassle. Just complain.
if ($file eq '-') {
warn "Can't operate on standard input.\n";
next;
warn "$F: Can't operate on standard input.\n";
exit EX_FAILURE;
}

# the description line. append info to this string
my $desc = "$file:";

# 1) check for various special files first
if ($followLinks) {
stat($file) or die("$F: '$file': $!\n");
} else {
lstat($file) or die("$F: '$file': $!\n");
my $stat_ok = $followLinks ? stat($file) : lstat($file);
unless ($stat_ok) {
warn "$F: failed to stat '$file': $!\n";
exit EX_FAILURE;
}

if (! -f _ or -z _) {
Expand All @@ -190,7 +199,10 @@ for my $file (@ARGV) {
}

# current file handle. or undef if checkMagic (-c option) is true.
my $fh = FileHandle->new($file, 'r') or die "$F: $file: $!\n" ;
my $fh = FileHandle->new($file, 'r') or do {
warn "$F: $file: $!\n";
exit EX_FAILURE;
};

# 2) check for script
if (-x $file && -T _) {
Expand Down Expand Up @@ -274,13 +286,13 @@ if ($checkMagic) {
dumpMagic(\@magic);
}

exit 0;
exit EX_SUCCESS;

####### SUBROUTINES ###########

sub usage {
warn "usage: $F [-cL] [-f filelist] [-m magicfile] file ...\n";
exit 1;
exit EX_FAILURE;
}

# compare the magic item with the filehandle.
Expand Down

0 comments on commit 302bad2

Please sign in to comment.