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

/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

Replies are listed 'Best First'.
Re^4: Cleaning up a path
by polettix (Vicar) on Apr 13, 2005 at 18:14 UTC
    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. }