package filedata;

sub new($) {
        my ($class, $filename) = @_;
        my $self = [];
        push @{$self}, $filename;
        push @{$self}, stat $filename;
        return bless $self, $class;
}

sub size {
my $self = shift;
my $comma = shift || 2;
my $size = $self->[8];

if ($size < 1024) {
                $size .= " bytes";
        } elsif ( $size > 1024 && $size < 1048576) {
                $size = sprintf("%.${comma}f", $size / 1024);
                $size .= " kb";
        } elsif ( $size > 1048576 && $size < 1073741824) {
                $size = sprintf("%.${comma}f", $size / 1048576);
                $size .= " mb";
        } elsif ($size > 1073741824 && $size < 1099511627776) {
                $size = sprintf("%.${comma}f", $size / 1073741824);
                $size .= " gb";
        } else {
                $size = sprintf("%.${comma}f", $size / 1099511627776);
                $size .= " tb";
        }
return $size;
}

sub atime {
my $self = shift;
my $option = shift;
my $atime = $self->[9];
my $epoch = time();
my $atime = $epoch-$atime;

if($option eq "time") {
if($atime < 60) {
                my $sek = $atime % 60;
                $atime = $sek . " sec";
        } elsif($atime >= 60 && $atime < 3600) {
                my $min = $atime / 60 % 60;
                my $sek = $atime % 60;
                $atime = $min.":".$sek . " min";
        } else {
                my $hour = $atime / 3600 % 60;
                my $min = $atime / 60 % 60;
                my $sek = $atime % 60;
                $atime = $hour.":".$min.":".$sek . " hour";
        }
} elsif ($option eq "date") {
        my $atime = scalar localtime $atime;
}
}

sub ctime {
my $self = shift;
my $option = shift;
my $ctime = $self->[11];
my $epoch = time();
my $ctime = $epoch-$ctime;

if($option eq "time") {
if($ctime < 60) {
                my $sek = $ctime % 60;
                $ctime = $sek . " sec";
        } elsif($ctime >= 60 && $ctime < 3600) {
                my $min = $ctime / 60 % 60;
                my $sek = $ctime % 60;
                $ctime = $min.":".$sek . " min";
        } else {
                my $hour = $ctime / 3600 % 60;
                my $min = $ctime / 60 % 60;
                my $sek = $ctime % 60;
                $ctime = $hour.":".$min.":".$sek . " hour";
        }
} elsif ($option eq "date") {
        my $ctime = scalar localtime $atime;
}
}

sub mtime {
my $self = shift;
my $option = shift;
my $mtime = $self->[10];
my $epoch = time();
my $mtime = $epoch-$atime;

if($option eq "time") {
if($mtime < 60) {
                my $sek = $mtime % 60;
                $mtime = $sek . " sec";
        } elsif($mtime >= 60 && $mtime < 3600) {
                my $min = $mtime / 60 % 60;
                my $sek = $mtime % 60;
                $mtime = $min.":".$sek . " min";
        } else {
                my $hour = $mtime / 3600 % 60;
                my $min = $mtime / 60 % 60;
                my $sek = $mtime % 60;
                $mtime = $hour.":".$min.":".$sek . " hour";
        }
} elsif ($option eq "date") {
        $mtime = scalar localtime $mtime;
}
}

sub permission {
my $self = shift;
my $permission = sprintf("%04o", $self->[3] & 07777);
}

sub name {
my $self = shift;
my $name = $self->[0];
}

sub hardlinks {
my $self = shift;
my $hardlinks = $self->[4];
}

sub devicenumber {
my $self = shift;
my $devicenumber = $self->[1];
}

sub inodenumber {
my $self = shift;
my $inodenumber = $self->[2];
}

sub uid {
my $self = shift;
my $uid = $self->[4];
}

sub gid {
my $self = shift;
my $gid = $self->[5];
}

sub device_identify {
my $self = shift;
my $device_identify = $self->[7];
}

sub blocksize {
my $self = shift;
my $blocksize = $self->[12];
}

sub blocks {
my $self = shift;
my $blocks = $self->[13];
}

1;

__END__

=head1 NAME

filedata - This Module is an OO Interface to Perl`s stat Funktion

=head1 SYNOPSIS

use filedata;

my $option = "date" || "time";

my $file = filedata->new("$ARGV[0]");

print "Name:\t", $file->name() . "\n";

print "Size:\t", $file->size($n) . "\n";

print "Atime:\t", $file->atime($option) . "\n";

print "Ctime:\t", $file->ctime($option) . "\n";

print "Mtime:\t", $file->mtime($option) . "\n";

print "Permissions are:\t", $file->permission() . "\n";

print "Hardlinks:\t", $file->hardlinks() . "\n";

print "Devicenumber:\t", $file->devicenumber() . "\n";

print "Inodenumber:\t", $file->inodenumber() . "\n";

print "UID:\t", $file->uid() . "\n";

print "GID:\t", $file->gid() . "\n";

print "Blocksize:\t", $file->blocksize() . "\n";

print "Blocks:\t", $file->blocks() . "\n";

print "Device Identifier:\t", $file->device_identify() . "\n";

=head1 DESCRIPTION

This Module allows you to look behind some file informations.
The Funktions in this Version are:

=over 4

=item * Name: Shows you the current filename of file

=item * Size: This Funktion shows you the current size of the file with $n decimal places in a readable format

=item * Mtime: This Funktion shows you the mtime of the file

=item * Ctime: Shows you the time since the last Inode Change

=item * Atime: This Funktion shows you the atime of the file

=item * Permission: Shows the permissions of the file

=item * Hardlink: Shows the number of hardlinks on this file

=item * Devicenumber: Shows the Devicenumber og the current filesystem

=item * Inodenumber: Shows you the Inodes of file

=item * UID: Shows you the UID of the file owner

=item * GID: Shows you the GID of the file owner

=item * Device Identifier: Shows you the device identifier (special files only)

=item * Blocksize: Shows you the preferd Blocksize of the current filesystem

=item * Blocks: actual number of blocks allocated

=back

=head1 BUGS

I thank everybody, who reports me bugs in this module !

=head1 AUTHOR

Ingo Walz, [ ingo@j-jayz-z.de ]

=head1 COPYRIGHT

Copyright (c) 2005 Ingo Walz. All rights reserved. This program is
free software; you can redistribute it and/or modify it under the same
terms as Perl itself.

=cut

