/lib/pwd.pl

https://github.com/Leont/perl · Perl · 73 lines · 58 code · 5 blank · 10 comment · 7 complexity · 1d283f39cb51dd78c0a1cf1da2cbeb43 MD5 · raw file

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