[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Testing sub directories using perl AGAIN



On Wed, Apr 28, 1999 at 01:42:48PM +0000, Shao Zhang wrote:
>     But it is still not exactly what I want. All the answers used the
> idea to compare the string, but what about
> the following case:
> 
> dir1 = "/www/info/world";
> dir2 = "/www/info/world/usa/../../../."
> 
> Now, clearly dir1 is a sub directory of dir2. So how do I test. Since
> the security is a big concern of us, we cannot
> afford the case like above.

For the example you've given above, I'd start by taking the two
strings and splitting them on '/' into two arrays.  Now, step through
the arrays in order, constructing a new pathname from the elements.
This time, however, strip out an entry of '..' by removing it and the
previous element, and strip out an entry of '.' by just pulling it.
You should end up with processed directory names that can be compared.

Pseudo-code:

@dir1parts = split '/', $dir1;
@dir2parts = split '/', $dir2;

foreach $dir (@dir1parts) {
	[ Process either a '.', a '..', or other, construct $ndir1 ]
}

foreach $dir (@dir2parts) {
	[ Process either a '.', a '..', or other, construct $ndir2 ]
}

if ($ndir1 == $ndir2) {
	do some foo;
} else {
	do some bar;
}

If you've got any questions with the above, drop me a line and we'll
work on it.

-- 
Ian Peters		"I never let schooling interfere with my education."
itp@gnu.org					-- Mark Twain


Reply to: