Reputation: 816
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.
Upvotes: 1
Views: 691
Reputation: 2766
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.
Upvotes: 3
Reputation: 6998
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.
Upvotes: 0