Commit | Line | Data |
---|---|---|
00bf170e LW |
1 | ;# pwd.pl - keeps track of current working directory in PWD environment var |
2 | ;# | |
a6d71656 GS |
3 | # |
4 | # This library is no longer being maintained, and is included for backward | |
5 | # compatibility with Perl 4 programs which may require it. | |
3046c4d3 S |
6 | # This legacy library is deprecated and will be removed in a future |
7 | # release of perl. | |
a6d71656 GS |
8 | # |
9 | # In particular, this should not be used as an example of modern Perl | |
10 | # programming techniques. | |
11 | # | |
12 | # Suggested alternative: Cwd | |
3046c4d3 S |
13 | |
14 | warn( "The 'pwd.pl' legacy library is deprecated and will be" | |
15 | . " removed in the next major release of perl. Please use the" | |
16 | . " Cwd module instead." ); | |
17 | ||
79072805 | 18 | ;# $RCSfile: pwd.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:11 $ |
00bf170e LW |
19 | ;# |
20 | ;# $Log: pwd.pl,v $ | |
00bf170e LW |
21 | ;# |
22 | ;# Usage: | |
23 | ;# require "pwd.pl"; | |
24 | ;# &initpwd; | |
25 | ;# ... | |
26 | ;# &chdir($newdir); | |
27 | ||
28 | package pwd; | |
29 | ||
30 | sub main'initpwd { | |
31 | if ($ENV{'PWD'}) { | |
32 | local($dd,$di) = stat('.'); | |
33 | local($pd,$pi) = stat($ENV{'PWD'}); | |
d5c3ff09 | 34 | if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) { |
32c2e4fb LW |
35 | chop($ENV{'PWD'} = `pwd`); |
36 | } | |
37 | } | |
38 | else { | |
39 | chop($ENV{'PWD'} = `pwd`); | |
40 | } | |
41 | if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) { | |
42 | local($pd,$pi) = stat($2); | |
43 | local($dd,$di) = stat($1); | |
d5c3ff09 | 44 | if (defined $pd and defined $dd and $di == $pi and $dd == $pd) { |
32c2e4fb LW |
45 | $ENV{'PWD'}="$2$3"; |
46 | } | |
00bf170e | 47 | } |
00bf170e LW |
48 | } |
49 | ||
50 | sub main'chdir { | |
51 | local($newdir) = shift; | |
748a9306 | 52 | $newdir =~ s|/{2,}|/|g; |
00bf170e LW |
53 | if (chdir $newdir) { |
54 | if ($newdir =~ m#^/#) { | |
55 | $ENV{'PWD'} = $newdir; | |
56 | } | |
57 | else { | |
58 | local(@curdir) = split(m#/#,$ENV{'PWD'}); | |
59 | @curdir = '' unless @curdir; | |
60 | foreach $component (split(m#/#, $newdir)) { | |
61 | next if $component eq '.'; | |
62 | pop(@curdir),next if $component eq '..'; | |
63 | push(@curdir,$component); | |
64 | } | |
65 | $ENV{'PWD'} = join('/',@curdir) || '/'; | |
66 | } | |
67 | } | |
68 | else { | |
69 | 0; | |
70 | } | |
71 | } | |
72 | ||
27e2fb84 | 73 | 1; |