summaryrefslogtreecommitdiff
path: root/lib/fastcwd.pl
diff options
context:
space:
mode:
authorLarry Wall <lwall@netlabs.com>1991-11-05 06:28:36 +0000
committerLarry Wall <lwall@netlabs.com>1991-11-05 06:28:36 +0000
commit99b89507a1fb507cf2635775ed834be00409c207 (patch)
tree6dc74b33ff0198c248ff530ef457b4286ee476ef /lib/fastcwd.pl
parentdb4e6270383b6e0b809aef95676865769ae4ca61 (diff)
downloadperl-99b89507a1fb507cf2635775ed834be00409c207.tar.gz
perl 4.0 patch 14: patch #11, continued
See patch #11.
Diffstat (limited to 'lib/fastcwd.pl')
-rw-r--r--lib/fastcwd.pl35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/fastcwd.pl b/lib/fastcwd.pl
new file mode 100644
index 0000000000..6b452e8d78
--- /dev/null
+++ b/lib/fastcwd.pl
@@ -0,0 +1,35 @@
+# By John Bazik
+#
+# Usage: $cwd = &fastcwd;
+#
+# This is a faster version of getcwd. It's also more dangerous because
+# you might chdir out of a directory that you can't chdir back into.
+
+sub fastcwd {
+ local($odev, $oino, $cdev, $cino, $tdev, $tino);
+ local(@path, $path);
+ local(*DIR);
+
+ ($cdev, $cino) = stat('.');
+ for (;;) {
+ ($odev, $oino) = ($cdev, $cino);
+ chdir('..');
+ ($cdev, $cino) = stat('.');
+ last if $odev == $cdev && $oino == $cino;
+ opendir(DIR, '.');
+ for (;;) {
+ $_ = readdir(DIR);
+ next if $_ eq '.';
+ next if $_ eq '..';
+
+ last unless $_;
+ ($tdev, $tino) = lstat($_);
+ last unless $tdev != $odev || $tino != $oino;
+ }
+ closedir(DIR);
+ unshift(@path, $_);
+ }
+ chdir($path = '/' . join('/', @path));
+ $path;
+}
+1;