如果getcwd()和getenv("PWD")不匹配,我该怎么办?

Car*_*rum 4 c macos makefile getcwd getenv

我有一个getcwd()用于获取当前工作目录的构建系统工具.这很好,除了有时人们的路径中有空格,构建系统不支持.你认为你可以只做一个符号链接:

ln -s "Directory With Spaces" DirectoryWithoutSpaces
Run Code Online (Sandbox Code Playgroud)

然后开心.但不幸的是,对我来说,getcwd()解决了所有的符号链接.我尝试使用getenv("PWD"),但它没有指向我回来的路径getcwd().我认为我责备make -C不更新环境变量.现在,getcwd()给我一条这样的道路:

/Users/carl/Directory With Spaces/Some/Other/Directories
Run Code Online (Sandbox Code Playgroud)

getenv("PWD")告诉我:

/Users/carl/DirectoryWithoutSpaces
Run Code Online (Sandbox Code Playgroud)

那么 - 是否有任何类似的功能getcwd()无法解析符号链接?

编辑:

我变了

make -C Some/Other/Directories
Run Code Online (Sandbox Code Playgroud)

cd Some/Other/Directories ; make
Run Code Online (Sandbox Code Playgroud)

然后getenv("PWD")工作..如果没有其他解决方案,我可以使用它.

eru*_*orm 6

根据Stevens的UNIX环境高级编程,第112页:

由于内核必须保持对当前工作目录的了解,因此我们应该能够获取其当前值.不幸的是,每个进程的所有内核维护都是当前工作目录的索引节点号和设备标识.内核不维护目录的完整路径名.

对不起,看起来你需要以另一种方式解决这个问题.