in reply to Normalized directory paths
First, we get rid of /., because that won't take us anywhere. Next we loop while there's a dot followed by a forward slash. As /.. takes us up a directory, we look for a valid directory followed by that construct, and get rid of the whole thing. Eventually, the loop has to fail, and we must have come up with something.#!/usr/bin/perl -w use strict; my $path = "/foo/mik/../mik/./../mik"; # print "Path is --$path--\n"; $path =~ s!/\.([^.])!$1!g; # print "Path is now --$path--\n"; while ($path =~ m!/\.!) { $path =~ s!/[^\/]+/\.\.!!; # print "Path is now -+$path+-\n"; } print "Ended up with =>>$path<<=\n";
merlyn and turnstep are correct, though -- doing this sort of thing with regular expressions is pretty easy to break. Unless you have *complete* control of the directories being passed to your script and in the filesystem, don't use this.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Re: Normalized directory paths
by mikfire (Deacon) on May 17, 2000 at 00:15 UTC | |
|
Re^2: Normalized directory paths
by znik (Initiate) on Feb 16, 2016 at 08:02 UTC | |
|
RE: Re: Normalized directory paths
by turnstep (Parson) on May 17, 2000 at 00:51 UTC |