#!/bin/perl # # This is something I worked out when I wanted to do file/directory exists tests # for things in AFS. With the Perl we have (5.05_003), the normal -f/-d tests # always return true for AFS objects. So I thought I would give stat a try. # # Given that nada does not exist, and these do, # drwxr-xr-x 3 jasper system 4096 Dec 14 14:35 bin # lrwxr-xr-x 1 jasper staff 3 Dec 20 14:24 junk.dlink -> bin # -rwxr-xr-x 1 jasper staff 4219 Dec 20 16:48 stat # lrwxr-xr-x 1 jasper staff 7 Dec 20 14:49 junk.flink -> test.pl # lrwxr-xr-x 1 jasper staff 4 Dec 20 14:59 junk.nada -> nada # crw-rw-rw- 1 root system 2, 2 Dec 20 16:48 /dev/null # srwxrwxrwx 1 root system 0 Nov 12 11:48 /tmp/.PMDV1 # # Then calling this script like so, # stat bin junk.dlink stat junk.flink nada junk.nada /dev/null /tmp/.PMDV1 # returns # Name stat[3]-oct stat[3]-dec Fifth-oct Fifth-dec >> 12 # ============================== =========== =========== ========= ========= ===== # bin is a directory 40755 16877 40000 16384 4 # junk.dlink is a link 40755 16877 40000 16384 4 # stat is not there 0 0 0 0 0 # junk.flink is a link 0 0 0 0 0 # nada is not there 0 0 0 0 0 # junk.nada is a link 0 0 0 0 0 # /dev/null is something else 20666 8630 20000 8192 2 # /tmp/.PMDV1 is something else 140777 49663 140000 49152 12 # # Note that when using stat, there is no way to differientiate # - between a directory and a link to a directory, # - between a file and a link to a file, # - nor between something that doesn't exist and a link to something that doesn't exist. # Appartently, stat follows links. # # I didn't want to live with these limitations, so I wound up coding my own ls -ld command # and parsing the output. chdir(); # Start in my own home directory (of whichever ID I'm running as) print " Name stat[3]-oct stat[3]-dec Fifth-oct Fifth-dec >> 12 \n"; print "============================== =========== =========== ========= ========= ===== \n"; while (@ARGV) { $thing=shift @ARGV; $ls=`ls -ld $thing 2>/dev/null`; if ($? >> 8) { $ls = "not there"; } elsif (substr($ls,0,1) eq "d") { $ls = "a directory"; } elsif (substr($ls,0,1) eq "-") { $ls = "a file"; } elsif (substr($ls,0,1) eq "l") { $ls = "a link"; } else { $ls = "something else"; } my (undef,undef,$mymode) = stat($thing); $mymode1= $mymode & 07770000; # The fifth octal digit. $mymode2= $mymode >> 12; # Drop off the four low-order octal digits. printf "%12s is %-14s %12o %11d %11o %10d %6d \n", $thing, $ls, $mymode, $mymode, $mymode1, $mymode1, $mymode2; }