Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

/bin/pwd (Re: Taint checking)

by tye (Sage)
on Sep 25, 2000 at 20:39 UTC ( #33927=note: print w/replies, xml ) Need Help??

in reply to Taint checking, File::Find and Cwd

The code for Cwd::cwd() looks like chop(`pwd`) which is rather unpleasant in my opinion

In my opinion as well. Unfortunately there aren't really any less unpleasant alternatives. Unix tracks the current working directory of the process by number (you can think of it as tracking the CWD i-node number or having a handle to opendir() handy -- depending on whether you think like a kernel or a user). So there is no "good" way to tell what you current directory is. Shells usually resort to caching the path used to get to this particular directory because of this.

So /bin/pwd does stat(".") and opendir("..") then stat("..") and opendir("../.."), etc. trying to build a path to the current directory. But you can cd into a directory where you have execute access but no read access. This means that /bin/pwd would fail, except that it is often set-UID to root for exactly this reason.

Unix provides a subroutine, getcwd(), that does the same thing as /bin/pwd. Often, this functionality if provided by fork()ing and exec()ing /bin/pwd and reading the results from a pipe. Sometimes, the Unix supports privileged subroutines so that /bin/pwd can be implemented directly in the subroutine and still succeed more often (it isn't that hard to have a current working directory for which there is no path).

So it would be nice if had an XS component so that it could call getcwd() on versions of Unix where that doesn't just run /bin/pwd.

Or you can just install a real O/S and avoid this whole mess and just call Win32::GetCwd(). (No, I'm not serious. This suggestion is just as useful as the usual "install a real O/S" suggestion that I still hear too often.)

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
RE: /bin/pwd (Re: Taint checking)
by ncw (Friar) on Sep 25, 2000 at 22:12 UTC
    I always thought of this tracking of your current directory to be shell magic just for the convenience of the user, eg an example (run on linux)

    $ cd /usr/X11
    $ pwd
    $ /bin/pwd
    $ ls -ld /usr/X11
    lrwxrwxrwx   1 root     root            5 Oct 21  1999 /usr/X11 -> X11R6
    But I see what you are saying in the case of the current directory being a deleted directory, eg :-
    $ mkdir test
    cd test
    pwd ; /bin/pwd
    rmdir .
    $ pwd ; /bin/pwd
    /bin/pwd: cannot get current directory: No such file or directory
    Anyway, getcwd() was what I expected Cwd to use, and I was very suprised to see this /bin/pwd thing!

    Thanks for the explanation. I think from the above experinents /bin/pwd == getcwd() under linux, so on that platform at least it should be replaced with getcwd(). According to my man pages getcwd() is POSIX so pretty much any unix should support it now-a-days.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://33927]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-05-26 02:18 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (92 votes). Check out past polls.