#!/usr/bin/perl -w # # md5sum - All-Perl implementation of md5sum(1) # # Copyright (C) 2000 Steven Pritchard # This program is free software; you can redistribute it # and/or modify it under the same terms as Perl itself. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # $Id: md5sum,v 1.4 2000/06/16 21:51:17 steve Exp $ use strict; use Digest::MD5; use FileHandle; use Getopt::Std; use vars qw($context $recursive $name %opt $onefs $dev); $context=new Digest::MD5; getopts('rx', \%opt); $recursive=$opt{'r'} if (defined($opt{'r'})); $onefs=$opt{'x'} if (defined($opt{'x'})); if ($#ARGV==-1) { if ($recursive) { $ARGV[0]="."; } else { $ARGV[0]="-"; } } foreach my $file (@ARGV) { if (($file ne "-") && (!lstat($file))) { print STDERR &basename(), ": $file: $!\n"; next; } if ($recursive && (-d(_))) { $dev=(stat(_))[0] if ($onefs); &recurse_into($file); } else { &printsum($file); } } sub printsum { my $file=shift; if ($file eq "-") { $context->reset(); while () { $context->add($_); } print $context->hexdigest(), " $file\n"; } elsif (my $fh=new FileHandle "<$file") { if (-d(_)) { print &basename($0), ": $file: Is a directory\n"; return; } #binmode($fh); $context->reset(); $context->addfile($fh); print $context->hexdigest(), " $file\n"; } else { print STDERR &basename(), ": $file: $!\n"; } } sub recurse_into { my $dir=shift; if (opendir(DIR, $dir)) { my $x; for $x (grep(!/^\.{1,2}$/, readdir(DIR))) { my @statbuf=lstat("$dir/$x"); if (-d(_)) { next if ($onefs && ($statbuf[0]!=$dev)); &recurse_into("$dir/$x"); } elsif (-f(_)) { &printsum("$dir/$x"); } else { print STDERR &basename(), ": '$dir/$x' is not a plain file, skipping...\n"; } } } else { print STDERR "Can't open $dir: $!\n"; } } sub basename { my $foo; return $name if defined($name); my @foo=split('/', $0); $name=$foo[$#foo]; return $name; }