Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

-d fails outside the current working directory

by Anonymous Monk
on Jun 05, 2002 at 09:17 UTC ( [id://171767]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am still persuing a solution to what should be a trivial problem:

How to get a list of the immediate subdirs of a user-supplied path:

I have a solution (thanks to the help of the wanderers of these noble halls) that works for the supplied path of "." but NOT for a supplied relative or absolute path UNLESS that is the cwd!?

Is this a code (dumb user) error? A platform (dumb os) specific error? Or -- horror of horrors -- the result of a Perl library writers off-day?

The following code is used to demonstrate the problem:

C:\www\NB.biz\html>type test.pl #!e:/perl/bin/perl.exe -w use strict; use diagnostics; print "Arg:", $ARGV[0], "\n"; $, = "\n"; opendir(DIR, $ARGV[0] ) or die ".: $!"; print "\nReaddir:\n", readdir(DIR); rewinddir(DIR); print "\nOnly directories:\n", grep {-d } readdir(DIR); rewinddir(DIR); print "\nJust named directories:\n", grep { -d $_ and $_ ne '.' and $ +_ ne '..'} read dir(DIR); closedir(DIR);

I pushed the long demonstration of the code failing here:

C:\www\NB.biz\html>test.pl . Arg:. Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. css graphics Products Just named directories: css graphics Products C:\www\NB.biz\html>cd .. C:\www\NB.biz>html\test.pl html Arg:html Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl html\ Arg:html\ Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl html\. Arg:html\. Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl \www\NB.biz\html Arg:\www\NB.biz\html Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl \www\NB.biz\html\ Arg:\www\NB.biz\html\ Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>

Replies are listed 'Best First'.
(MeowChow) Re: -d fails outside the current working directory
by MeowChow (Vicar) on Jun 05, 2002 at 09:30 UTC
    As you see, readdir returns the unqualified names of the directory entries, but -d operates on them within the context of the current working directory, which is a recipe for failure. You should either change your cwd (not really recommended), or prepend the filenames with the relative/absolute path, as in:
    print "\nOnly directories:\n", grep { -d "$ARGV[0]/$_" } readdir(DIR);
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

      Thankyou!

      Excuses:I did read the readdir docs, I am unfortuantely still at the stage where I get lost in the syntax and therefore don't recognise the significance of the sample code;(

      Am I going to get into problems with using paths (abs or rel) when the "user defined path" is actually a relative path derived from the query parameter of an URL and the script will be running in the context of Apache?

        There are serious security issues you need to consider if you allow users to submit their own paths, depending of course on what you're doing with those paths. For the sake of simplicity, I would recommend that you only allow alphanumerics in these path names. You can then properly untaint and scrub your path as follows:
        my $userpath = $CGI->param('userpath'); my $basepath = "C:/wwwroot/whatever"; my $path = join '/', $basepath, $userpath =~ /\w+/g;
        Speaking of untainting, you are using -T, right?
           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
Re: -d fails outside the current working directory
by cjf (Parson) on Jun 05, 2002 at 09:44 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171767]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-19 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found