CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
absoulte path for symbolic link

 



rahulgupta70
New User

May 19, 2009, 9:52 PM

Post #1 of 4 (3762 views)
absoulte path for symbolic link Can't Post

In unix in creatd a symbolic link

/data/rdmst11/abc.v -> /home/user/pqr/file_name123

Now to my perl script (test.pl) in directory /data/rdmst11/abc/mydir/ I provided ../../abc.v as the input.

When i use cwd::abs_path("../../abc.v") it gives me /home/user/pqr/file_name123 as the return value. That means it has resolved the symbolic link.
I want a way by which I get /data/rdmst11/abc.v as the output in which the symbolic link has not been resolved

Regards


1arryb
User

May 20, 2009, 1:55 PM

Post #2 of 4 (3745 views)
Re: [rahulgupta70] absoulte path for symbolic link [In reply to] Can't Post

Hi rahu,

Get the abs_path of the directory component to avoid traversing the link at the file level (of course, this won't work if '../../' is also a link):

Code
#!/usr/bin/perl 
#
use strict;
use warnings;

use Cwd qw(abs_path);
use File::Basename;

my $link = $ARGV[0];
die "usage: $0 <link_name>" unless $link;
unless (-l $link) {
print "WARNING: $link isn't a symlink. This won't prove anything.\n";
}
print abs_path(dirname($link)) . '/' . basename($link) . "\n";


Cheers,

Larry


(This post was edited by 1arryb on May 20, 2009, 1:56 PM)


rahulgupta70
New User

May 20, 2009, 10:23 PM

Post #3 of 4 (3728 views)
Re: [1arryb] absoulte path for symbolic link [In reply to] Can't Post

sub utilGetAbsoluteFileName{

my $fileName = shift;
$fileName =~ s/\/+/\//g;

if(! -e $fileName){
return $fileName;
}

my $dir = getcwd;
if ($fileName !~ /^\//){
$fileName = $dir."\/".$fileName;
}

my @targetArr = ();
my @fileArray = split "\/", $fileName;
for (my $index=0; $index<@fileArray; $index++){
next if($fileArray[$index] eq "\.");

if($fileArray[$index] eq "\.\."){
pop @targetArr;
}else{
push @targetArr, $fileArray[$index];
}
}

my $absFileName = join "/", @targetArr;

$absFileName =~ s/\/+/\//g;
return $absFileName;
}

I have written this API which calculates the absolute path for any filename in unix. It takes care of "../../" and "./". Please tell if i have missed any case.
Thanks

Code

In Reply To


Code



rahulgupta70
New User

May 20, 2009, 10:24 PM

Post #4 of 4 (3727 views)
Re: [1arryb] absoulte path for symbolic link [In reply to] Can't Post

Thanks Larry !!!


Rahul

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives