#! /usr/local/bin/perl # resolve or abbreviate file path # path -r path resolve path (make it absolute and without symbolic links) # path -a path abbreviate path sub resolve_path { local ($path) = $_[0]; local ($_, $dir, $fil, $orig); $orig = $path; if ($path =~ m-/+$-) { $path = $`; }; if ($path !~ m-/-) { $_ = `pwd`; chop; $path = "$_/$path"; }; $it = 0; while ( -l $path) { if ($path =~ m-^(.+)/([^/]*)$-) { $dir = $1; } else { $dir = ''; }; $path = readlink ($path); if ($path !~ m-^/-) { $path = "$dir/$path"; }; die "circular link $path" if $it++ > 40; }; if ($path =~ m-^(.+)/([^/]*)$-) { $dir = $1; $fil = $2; $dir = &resolve_path ($dir); if ($fil eq '.') { $path = $dir; } elsif ($fil eq '..' && $dir =~ m-^(.*)/([^/]*)$-) { $path = $1; } elsif ($fil ne '..' || $dir ne '') { $path = "$dir/$fil"; }; }; # print "\"$orig\" is \"$path\"\n"; return $path; } sub abbrev_path { local ($path) = &resolve_path ($_[0]); if ($path =~ m+^/afs/lrz-muenchen.de/home/./+) { $path = "~$'"; } elsif ($path =~ m+^/afs/lrz-muenchen.de/common/+) { $path = $'; } elsif ($path =~ m+^/afs/lrz-muenchen.de/+) { $path = $'; }; if ($path =~ m+^([^/]*/).*(/[^/]*/\d*)$+) { return $1 . "\267\267\267" . $2; } elsif ($path =~ m+^([^/]*/[^/]*/).*(/[^/]*)$+) { return $1 . "\267\267\267" . $2; } else { return $path; }; } if ($#ARGV == 1) { if ($ARGV[0] eq '-r') { print &resolve_path($ARGV[1]) . "\n"; } elsif ($ARGV[0] eq '-a') { print &abbrev_path($ARGV[1]) . "\n"; } else { die "incorrect options"; }; } else { die "incorrect options"; };