Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!iwarp.intel.com!news From: merlyn@iwarp.intel.com (Randal L. Schwartz) Newsgroups: comp.lang.perl Subject: Re: pwd? Message-ID: <1991Apr17.165554.16507@iwarp.intel.com> Date: 17 Apr 91 16:55:54 GMT References: <6330@male.EBay.Sun.COM> Sender: news@iwarp.intel.com Reply-To: merlyn@iwarp.intel.com (Randal L. Schwartz) Organization: Stonehenge; netaccess via Intel, Beaverton, Oregon, USA Lines: 41 In-Reply-To: me@anywhere.EBay.Sun.COM (Wayne Thompson - IR Workstation Support SE) In article <6330@male.EBay.Sun.COM>, me@anywhere (Wayne Thompson - IR Workstation Support SE) writes: | Here's what I use to get an absolute path. As always, comments welcomed. | | Wayne | | sub FullPath { # FullPath ($name) | local ($name, $path, $dir) = @_; | if ($name =~ m#^/#) { | $path = $name; | } | elsif ($name !~ m#/#) { | foreach $dir (split (/:/, $ENV{'PATH'})) { | $path = join ('/', $dir, $name); | last if -x $path && -f _; | $path = ''; | } | } | else { | chop ($dir = `pwd`), $path = join ('/', $dir, $name); | } | chop ($dir = `pwd`), $path = join ('/', $dir, $name) | if $path eq "./$name"; | $path =~ s#[^/]+/\.\.##go; | $path =~ s#\./##go; | $path =~ s#//#/#go; | $path; | } | 1; Which doesn't work if $name contains "foo/../bar", where "foo" is a symlink. Unless you're willing to stat a lot of directories, the fastest way is: chop($path = `cd $name && pwd`); $z="/tmp/Just another Perl hacker,";mkdir($z,0777);chdir$z;$_=`pwd`;chdir;rmdir$z;s#.*/##;print -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/