为什么 PWD 为空以及如何修复?

发布于 2024-12-02 14:28:53 字数 535 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撑一把青伞 2024-12-09 14:28:53

请改用 getcwd() 。不需要设置PWD

#include <unistd.h>

char *getcwd(char *buf, size_t size);

Use getcwd() instead. PWD isn't required to be set.

#include <unistd.h>

char *getcwd(char *buf, size_t size);
书间行客 2024-12-09 14:28:53

_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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文