Skip to content

Commit

Permalink
file: die for bad options (#546)
Browse files Browse the repository at this point in the history
* If an invalid option was given along with a file argument, the option was ignored
* Follow GNU file by printing usage and exiting in this case; nothing should be processed
* While here, re-order declarations so pragma appears before library modules
  • Loading branch information
mknos committed Apr 11, 2024
1 parent 77d72e8 commit ebd398e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions bin/file
Expand Up @@ -16,10 +16,11 @@ License: bsd
# file -- report the type of a file
#

use strict;

use FindBin;
use FileHandle;
use Getopt::Long;
use strict;

my $F = $FindBin::Script;

Expand Down Expand Up @@ -116,7 +117,7 @@ GetOptions(
"c!", \$checkMagic,
"L!", \$followLinks,
"f=s", \$fileList
);
) or usage();

# the names of the files are in $fileList.
if ($fileList) {
Expand All @@ -126,10 +127,7 @@ if ($fileList) {
$fileListFH->close();
}

if (!@ARGV && !$checkMagic) {
die "usage: $F [-cL] [-f filelist] [-m magicfile] file ...\n";
}

usage() if (!@ARGV && !$checkMagic);
if ( ! -f $magicFile ) {
# have a fallback for now until a distribution heirarchy is done.
# this works on many unix systems.
Expand Down Expand Up @@ -280,6 +278,11 @@ exit 0;

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

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

# compare the magic item with the filehandle.
# if success, print info and return true. otherwise return undef.
#
Expand Down

0 comments on commit ebd398e

Please sign in to comment.