On Mon, Jul 19, 2010 at 01:50, newbie01 perl <newbie01.p...@gmail.com> wrote:
> Hi all,
>
> Does anyone know if someone had written a Perl stat script that mimics the
> Linux' stat command?
>
> I want to be able to use the stat command on Solaris, unfortunately, I just
> found out that that command is not available on Solaris and HP-UX so am
> thinking a Perl script should be the way to go then instead so I can have it
> on any UNIX's flavours.
>
> Any suggestion will be very much appreciated.
>
> Thanks in advance.
>

The Linux stat program is based around the C stat function.  Perl
provides access to this same function.  You can find the documentation
for stat in with the perldoc command like this: perldoc -f stat.  The
documentation is also available on the [web][2].  Depending on what
you want to do it can be quite simple.  This is a fairly faithful
reproduction of what my version of stat puts out:

#!/usr/bin/perl

use strict;
use warnings;

for my $file (@ARGV) {
        my ($device, $inode, $mode, $nlink, $uid, $gid, $rdev, $size,
                $atime, $mtime, $ctime, $blksize, $blocks) = stat $file;

        my $string_mode = "";
        #04000, 02000, 01000 left as an exercise for the reader
        $string_mode .= $mode & 0400 ? "r" : "-";
        $string_mode .= $mode & 0200 ? "w" : "-";
        $string_mode .= $mode & 0100 ? "x" : "-";
        $string_mode .= $mode & 0040 ? "r" : "-";
        $string_mode .= $mode & 0020 ? "w" : "-";
        $string_mode .= $mode & 0010 ? "x" : "-";
        $string_mode .= $mode & 0004 ? "r" : "-";
        $string_mode .= $mode & 0002 ? "w" : "-";
        $string_mode .= $mode & 0001 ? "x" : "-";

        my ($username)  = getpwuid $uid || $uid;
        my ($groupname) = getgrgid $gid || $gid;

        print join(" ", $device, $inode, $string_mode, $nlink,
                $username, $groupname, $rdev, $size,
                (map { scalar localtime $_ } $atime, $mtime, $ctime),
                $blksize, $blocks, $file), "\n";
}

 [0]: http://linux.die.net/man/1/stat
 [1]: http://linux.die.net/man/2/stat
 [2]: http://perldoc.perl.org/functions/stat.html

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to