in reply to Re: Cleaning up a path
in thread Cleaning up a path

This breaks in the case depicted by ikegami, but can be adjusted to cope with the portability problem:
#!/usr/bin/perl use strict; use warnings; use File::Spec; my $somepath = "/var/log/../../home/poletti/../../etc/rc.d"; my @true; $somepath = "/../../../"; # Let's pretend $somepath is a directory... or is it? :) /^\.\./ ? pop( @true ) : push( @true, $_ ) foreach( File::Spec->splitdir($somepath) ); print File::Spec->catdir(@true), "\n";

Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

Don't fool yourself.

Replies are listed 'Best First'.
Re^3: Cleaning up a path
by ikegami (Patriarch) on Apr 13, 2005 at 18:03 UTC
    /foo/.. -> / <- Fixed /foo/../bar -> /bar <- Still works foo/.. -> <- Should be . (?) foo/../bar -> bar <- Still works .. -> <- Should be .. ../foo -> foo <- Should be ../foo ../foo/bar -> foo/bar <- Should be ../foo/bar foo/bar/../.. -> <- Should be . (?) /foo/bar/../.. -> / <- Fixed /foo/bar/../../moo -> /moo <- Still works {...}/etc/passwd -> /etc/passwd <- Still works
      Correct, but I did not dare asking for some solution which could be valid for relative filenames: this would be some kind of sphere reading!

      Just before you reply (I can hear your breath on my neck!), I want to admit that it also fails miserably with this valid, absolute path:

      /.. -> <- still broken
      I'm understanding proper testing these days...

      Flavio (perl -e "print(scalar(reverse('ti.xittelop@oivalf')))")

      Don't fool yourself.

        I fixed your code for all cases except '/..' (which returns '.'):

        sub remove_dot_dot_frodo72 { my $path = File::Spec::Unix->canonpath($_[0]); my @true; /^\.\./ && @true ? pop(@true) : push(@true, $_) # <- Added to cond foreach File::Spec::Unix->splitdir($path); $path = File::Spec::Unix->catdir(@true); return length($path) ? $path : '.'; # <- Added. }