1

i am trying to get the current working directory path using Perl

when i execute from ubuntu: $root@ubuntu:/var/test/geek# firefox http:/localhost/test.html, i get /var/cgi-bin as output in perl cgi page instead of /var/test/geek.

used perl code:

my $pwd=cwd();
 bla bla
print "<h1> pwd </h1>";

above code gives path of test.pl not users working directory path

Edit: When i run the script alone from the terminal it works fine. for example:

$root@ubuntu:/var/test/geek# /var/cgi-bin/test.pl 

i get /var/test/geek. but when i call the script in html page using submit button it gives path of perl script.

2
  • 2
    You're asking for trouble to be using root all the time. You should really do your development work as an unprivileged user, and configure sudo for the privileged commands you need. Dec 31, 2015 at 11:39
  • 2
    You can't get the current working directory on the user's system because you're not running on the user's system; you're running on your webserver. Please explain exactly why you think you need to do this. Dec 31, 2015 at 14:28

2 Answers 2

3

Each process has its own working directory that it inherits from its parent when it gets created.

cwd() returns the current process's working directory.

For a CGI script, the browser doesn't pass its working directory to the server as part of the request. To obtain that, you need to have code running on the client system that submits it. That might be an application that the user download, or possibly, but unlikely, some in-browser code, like Javascript / a Java applet (This info is likely hidden from in-browser code for security reasons though).

(The rest assumes Linux, it will likely differ on other operating systems)

The part below assumes that you are looking for the working directory of a user on the server:

In order to get a specific shell for a specific user's working directory, you would need to identify the PID for the shell and get the working directory from the /proc/<pid>/cwd symlink (To read these, the process must belong to the user running the code, or the code must run as root (Which is a bad idea for a CGI script)...). To get the PID of the shell, you likely need to start from the w command output, or its data source, /var/run/utmp. Sys::Utmp might be useful for this... You might then also need to retreive a whole lot of extra info to find all the processes that might have the working directory that you are looking for.

4
  • You would need to be root to poke around in /proc for other user's processes. And running a CGI script as root is just stupid. Dec 31, 2015 at 11:38
  • 1
    @glennjackman: Correct, depending on what details you are looking for... The w output needs to be accessible to multiple users, but that does not include the working directory... (I can actually confirm that you can only read that for your own user) The asker seems to have decided that he/she needs the working directory (of something, either client or server, he/she isn't telling yet) to accomplish something, but isn't telling what he/she is trying to accomplish... Dec 31, 2015 at 11:42
  • upvote for explaining @gert. i was trying to get the user directory and display it on the html page. and download or show a perticular text file which will be der in that directory, from the web browser.
    – geek
    Jan 2, 2016 at 7:41
  • @geek The user would need to upload the file... (At least if it is a file on the user PC, not one on the web server...) Allowing a web page to read a random file from a user PC is a huge security risk (at least without permission)... You MIGHT be able to do it via Javascript, on a file:// URL, depending on the browser... (Chrome won't work: code.google.com/p/chromium/issues/detail?id=47416 Firefox is complicated: developer.mozilla.org/en-US/docs/… ) Jan 3, 2016 at 21:00
0

I think you are mixing the web server and the local user. The web server has a working directory when you run the script, and that is the one that cwd() returns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.