#!/usr/bin/perl -w
#
# $Revision: 1.5 $ $Date: 2012-07-26 17:11:47-04 $
# $Source: /home/vogelke/bin/RCS/ftype,v $
# $Host: sys7.com $
# $UUID: c3fe9fe0-27b9-3253-b2d6-a773082c8ae3 $
#
#<ftype: show filetype, owner, modtime for each file.

use strict;
use Fcntl ':mode';
use subs qw/filetype/;

if (@ARGV) {
    foreach (@ARGV) {
        filetype($_);
    }
}
else {
    while (<>) {
        chomp;
        filetype($_);
    }
}

exit(0);

# ------------------------------------------------------------------
sub filetype {
    my ($file) = @_;

    # Don't use lstat(), we want linked directories
    # to show up as directories.
    my ($mode, $uid, $gid, $mt) = (stat($file))[2, 4, 5, 9];

    if ($mode) {
        my ($user)  = (getpwuid($uid))[0] || $uid;
        my ($group) = (getgrgid($gid))[0] || $gid;

        # FIXME: store user and group information in hash

        my $ft = 'u';
        $ft = 'b' if S_ISBLK($mode);
        $ft = 'd' if S_ISDIR($mode);
        $ft = 'p' if S_ISFIFO($mode);
        $ft = 'l' if S_ISLNK($mode);
        $ft = 'f' if S_ISREG($mode);
        $ft = 's' if S_ISSOCK($mode);
        $ft = 'c' if S_ISCHR($mode);

        printf "%s\t%d\t%4.4o\t%s\t%s\t%s\n",
          $ft, $mt, $mode & 0xfff, $user, $group, $file;
    }

    1;
}
