Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.1 KiB

  1. ;# pwd.pl - keeps track of current working directory in PWD environment var
  2. ;#
  3. ;# $RCSfile: pwd.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:11 $
  4. ;#
  5. ;# $Log: pwd.pl,v $
  6. ;#
  7. ;# Usage:
  8. ;# require "pwd.pl";
  9. ;# &initpwd;
  10. ;# ...
  11. ;# &chdir($newdir);
  12. package pwd;
  13. sub main'initpwd {
  14. if ($ENV{'PWD'}) {
  15. local($dd,$di) = stat('.');
  16. local($pd,$pi) = stat($ENV{'PWD'});
  17. if ($di != $pi || $dd != $pd) {
  18. chop($ENV{'PWD'} = `pwd`);
  19. }
  20. }
  21. else {
  22. chop($ENV{'PWD'} = `pwd`);
  23. }
  24. if ($ENV{'PWD'} =~ m|(/[^/]+(/[^/]+/[^/]+))(.*)|) {
  25. local($pd,$pi) = stat($2);
  26. local($dd,$di) = stat($1);
  27. if ($di == $pi && $dd == $pd) {
  28. $ENV{'PWD'}="$2$3";
  29. }
  30. }
  31. }
  32. sub main'chdir {
  33. local($newdir) = shift;
  34. $newdir =~ s|/{2,}|/|g;
  35. if (chdir $newdir) {
  36. if ($newdir =~ m#^/#) {
  37. $ENV{'PWD'} = $newdir;
  38. }
  39. else {
  40. local(@curdir) = split(m#/#,$ENV{'PWD'});
  41. @curdir = '' unless @curdir;
  42. foreach $component (split(m#/#, $newdir)) {
  43. next if $component eq '.';
  44. pop(@curdir),next if $component eq '..';
  45. push(@curdir,$component);
  46. }
  47. $ENV{'PWD'} = join('/',@curdir) || '/';
  48. }
  49. }
  50. else {
  51. 0;
  52. }
  53. }
  54. 1;