xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/lib/fastcwd.pl (revision 0:68f95e015346)
1*0Sstevel@tonic-gate# By John Bazik
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# This library is no longer being maintained, and is included for backward
4*0Sstevel@tonic-gate# compatibility with Perl 4 programs which may require it.
5*0Sstevel@tonic-gate#
6*0Sstevel@tonic-gate# In particular, this should not be used as an example of modern Perl
7*0Sstevel@tonic-gate# programming techniques.
8*0Sstevel@tonic-gate#
9*0Sstevel@tonic-gate# Suggested alternative: Cwd
10*0Sstevel@tonic-gate#
11*0Sstevel@tonic-gate# Usage: $cwd = &fastcwd;
12*0Sstevel@tonic-gate#
13*0Sstevel@tonic-gate# This is a faster version of getcwd.  It's also more dangerous because
14*0Sstevel@tonic-gate# you might chdir out of a directory that you can't chdir back into.
15*0Sstevel@tonic-gate
16*0Sstevel@tonic-gatesub fastcwd {
17*0Sstevel@tonic-gate	local($odev, $oino, $cdev, $cino, $tdev, $tino);
18*0Sstevel@tonic-gate	local(@path, $path);
19*0Sstevel@tonic-gate	local(*DIR);
20*0Sstevel@tonic-gate
21*0Sstevel@tonic-gate	($cdev, $cino) = stat('.');
22*0Sstevel@tonic-gate	for (;;) {
23*0Sstevel@tonic-gate		($odev, $oino) = ($cdev, $cino);
24*0Sstevel@tonic-gate		chdir('..');
25*0Sstevel@tonic-gate		($cdev, $cino) = stat('.');
26*0Sstevel@tonic-gate		last if $odev == $cdev && $oino == $cino;
27*0Sstevel@tonic-gate		opendir(DIR, '.');
28*0Sstevel@tonic-gate		for (;;) {
29*0Sstevel@tonic-gate			$_ = readdir(DIR);
30*0Sstevel@tonic-gate			next if $_ eq '.';
31*0Sstevel@tonic-gate			next if $_ eq '..';
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate			last unless $_;
34*0Sstevel@tonic-gate			($tdev, $tino) = lstat($_);
35*0Sstevel@tonic-gate			last unless $tdev != $odev || $tino != $oino;
36*0Sstevel@tonic-gate		}
37*0Sstevel@tonic-gate		closedir(DIR);
38*0Sstevel@tonic-gate		unshift(@path, $_);
39*0Sstevel@tonic-gate	}
40*0Sstevel@tonic-gate	chdir($path = '/' . join('/', @path));
41*0Sstevel@tonic-gate	$path;
42*0Sstevel@tonic-gate}
43*0Sstevel@tonic-gate1;
44