Merge branch 'master' of http://repo.or.cz/r/msysgit into devel
[msysgit/historical-msysgit.git] / lib / perl5 / 5.6.1 / pwd.pl
blob6b429eb5a6849d4c2975bd425fe5069e8c2323f1
1 ;# pwd.pl - keeps track of current working directory in PWD environment var
2 ;#
4 # This library is no longer being maintained, and is included for backward
5 # compatibility with Perl 4 programs which may require it.
7 # In particular, this should not be used as an example of modern Perl
8 # programming techniques.
10 # Suggested alternative: Cwd
12 ;# $RCSfile: pwd.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:11 $
14 ;# $Log: pwd.pl,v $
16 ;# Usage:
17 ;# require "pwd.pl";
18 ;# &initpwd;
19 ;# ...
20 ;# &chdir($newdir);
22 package pwd;
24 sub main'initpwd {
25 if ($ENV{'PWD'}) {
26 local($dd,$di) = stat('.');
27 local($pd,$pi) = stat($ENV{'PWD'});
28 if (!defined $dd or !defined $pd or $di != $pi or $dd != $pd) {
29 chop($ENV{'PWD'} = `pwd`);
32 else {
33 chop($ENV{'PWD'} = `pwd`);
35 if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) {
36 local($pd,$pi) = stat($2);
37 local($dd,$di) = stat($1);
38 if (defined $pd and defined $dd and $di == $pi and $dd == $pd) {
39 $ENV{'PWD'}="$2$3";
44 sub main'chdir {
45 local($newdir) = shift;
46 $newdir =~ s|/{2,}|/|g;
47 if (chdir $newdir) {
48 if ($newdir =~ m#^/#) {
49 $ENV{'PWD'} = $newdir;
51 else {
52 local(@curdir) = split(m#/#,$ENV{'PWD'});
53 @curdir = '' unless @curdir;
54 foreach $component (split(m#/#, $newdir)) {
55 next if $component eq '.';
56 pop(@curdir),next if $component eq '..';
57 push(@curdir,$component);
59 $ENV{'PWD'} = join('/',@curdir) || '/';
62 else {