#!/usr/local/apps/perl/bin/perl ############################################################################# # # # WHO: John L. Moreland # # # # WHAT: dstat # # # # WHY: Demonstrates the use of directory functions and stat. # # # # WHERE: Opus Software # # # # WHEN: Thu Oct 1 08:52:16 PDT 1998 # # # # HOW: PERL # # # ############################################################################# ############################################################################# ############################### SUBROUTINES ############################### ############################################################################# ############################################################################ # # # Parse the specified directory and return a list of files/directories. # # # ############################################################################ sub dirlist { opendir( DIR, $_[0] ) || die "Can't open directory $_[0]"; my @result = readdir( DIR ); closedir( DIR ); shift( @result ); # STRIP OFF THE "." shift( @result ); # STRIP OFF THE ".." return( @result ); } ############################################################################ # # # Print the results of a stat function in a nice readable form. # # # ############################################################################ sub pstat { my $path = shift; my $entry = shift; print "$entry\n"; @stat = stat( "$path/$entry" ); print " dev = $stat[0]\n"; print " inode = $stat[1]\n"; @mode = ( "-", "-", "-", "-", "-", "-", "-", "-", "-", "-" ); $mode[0] = "d" if ( -d "$path/$entry" ); $mode[1] = "r" if ( $stat[2] & 256 ); $mode[2] = "w" if ( $stat[2] & 128 ); $mode[3] = "x" if ( $stat[2] & 64 ); $mode[3] = "s" if ( $stat[2] & 4000 ); $mode[4] = "r" if ( $stat[2] & 32 ); $mode[5] = "w" if ( $stat[2] & 16 ); $mode[6] = "x" if ( $stat[2] & 8 ); $mode[6] = "s" if ( $stat[2] & 2000 ); $mode[7] = "r" if ( $stat[2] & 4 ); $mode[8] = "w" if ( $stat[2] & 2 ); $mode[9] = "x" if ( $stat[2] & 1 ); $mode = join( "", @mode ); print " mode = $stat[2] ($mode)\n"; print " nlink = $stat[3]\n"; @pw = getpwuid( $stat[4] ); print " uid = $stat[4] ($pw[0])\n"; $gnam = getgrgid( $stat[5] ); print " gid = $stat[5] ($gnam)\n"; print " rdev = $stat[6]\n"; print " size = $stat[7]\n"; print " atime = $stat[8] (" . localtime( $stat[8] ) . ")\n"; print " mtime = $stat[9] (" . localtime( $stat[9] ) . ")\n"; print " ctime = $stat[10] (" . localtime( $stat[10] ) . ")\n"; print " blksiz = $stat[11]\n"; print " blocks = $stat[12]\n"; } ############################################################################# ################################## MAIN ################################### ############################################################################# if ( -d $ARGV[0] ) { foreach $entry ( dirlist( $ARGV[0] ) ) { pstat( $ARGV[0], $entry ); } } else { print "Usage:\n"; print " $0 directory_path\n"; }