Why is PWD empty and how to fix it?
I'm using lighttpd and wrote the following cgi script:
main(){
printf("Content-ty开发者_如何学Gope: text/html\n\n");
char * pwd ="";
pwd=getenv("PWD");
printf ("The current path is: %s",pwd);
}
The Result is
The current path is: (null)
Well, I don't get why. And I don't know how to find the path of the script executed. I'm looking for args[0] with a path, and used pwd for that, but maybe I should switch to something different.
UPDATE
Not working aswell is
char cwd[_PC_PATH_MAX+1];
getcwd(cwd, _PC_PATH_MAX+1);
cwd is " ". Maybe my script knew where it was if I stoped using room 1408 as my datacenter. :P
Use getcwd()
instead. PWD
isn't required to be set.
#include <unistd.h>
char *getcwd(char *buf, size_t size);
_PC_PATH_MAX
is not the max path length. It's a key you pass to pathconf
to request the max path length, as in pathconf("/", _PC_PATH_MAX)
. Of course if PATH_MAX
is defined it would be simpler to use that directly.
精彩评论