为什么 PWD 为空以及如何修复?
我正在使用 lighttpd 并编写了以下 cgi 脚本:
main(){
printf("Content-type: text/html\n\n");
char * pwd ="";
pwd=getenv("PWD");
printf ("The current path is: %s",pwd);
}
结果
The current path is: (null)
很好,我不明白为什么。而且我不知道如何找到执行脚本的路径。我正在寻找带有路径的 args[0] ,并使用 pwd 来实现,但也许我应该切换到不同的东西。
更新
不起作用,
char cwd[_PC_PATH_MAX+1];
getcwd(cwd, _PC_PATH_MAX+1);
cwd 是“”。如果我停止使用 1408 号房间作为我的数据中心,也许我的脚本知道它在哪里。 :P
I'm using lighttpd and wrote the following cgi script:
main(){
printf("Content-type: 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请改用 getcwd() 。不需要设置
PWD
。Use
getcwd()
instead.PWD
isn't required to be set._PC_PATH_MAX
不是最大路径长度。这是您传递给pathconf
以请求最大路径长度的密钥,如pathconf("/", _PC_PATH_MAX)
中所示。当然,如果定义了PATH_MAX
,那么直接使用它会更简单。_PC_PATH_MAX
is not the max path length. It's a key you pass topathconf
to request the max path length, as inpathconf("/", _PC_PATH_MAX)
. Of course ifPATH_MAX
is defined it would be simpler to use that directly.