如果 getcwd() 和 getenv(“PWD”) 不匹配怎么办?
我有一个构建系统工具,它使用 getcwd() 来获取当前工作目录。这很好,只是有时人们的路径中有空格,而构建系统不支持这种情况。你可能会认为你可以建立一个符号链接:
ln -s "Directory With Spaces" DirectoryWithoutSpaces
然后就开心了。但不幸的是,对我来说, getcwd() 解析了所有符号链接。我尝试使用 getenv("PWD"),但它没有指向与我从 getcwd() 返回的路径相同的路径。我认为我责怪 make -C
没有更新环境变量。现在, getcwd() 给我返回这样的路径:
/Users/carl/Directory With Spaces/Some/Other/Directories
并且 getenv("PWD") 给我:
/Users/carl/DirectoryWithoutSpaces
那么 - 是否有像 getcwd( ) 不能解析符号链接?
编辑:
我更改
make -C Some/Other/Directories
为
cd Some/Other/Directories ; make
然后 getenv("PWD")
有效。如果没有其他解决方案,我可以使用它。
I have a build system tool that is using getcwd()
to get the current working directory. That's great, except that sometimes people have spaces in their paths, which isn't supported by the build system. You'd think that you could just make a symbolic link:
ln -s "Directory With Spaces" DirectoryWithoutSpaces
And then be happy. But unfortunately for me, getcwd()
resolves all the symbolic links. I tried to use getenv("PWD")
, but it is not pointing at the same path as I get back from getcwd()
. I blame make -C
for not updating the environment variable, I think. Right now, getcwd()
gives me back a path like this:
/Users/carl/Directory With Spaces/Some/Other/Directories
And getenv("PWD")
gives me:
/Users/carl/DirectoryWithoutSpaces
So - is there any function like getcwd()
that doesn't resolve the symbolic links?
Edit:
I changed
make -C Some/Other/Directories
to
cd Some/Other/Directories ; make
And then getenv("PWD")
works.. If there's no other solution, I can use that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Stevens 的《UNIX 环境高级编程》圣经,第 112 页:
抱歉,看来您确实需要以其他方式解决此问题。
According to the Advanced Programming in the UNIX Environment bible by Stevens, p.112:
Sorry, looks like you do need to work around this in another way.
getcwd() 无法确定您通过符号链接所遵循的路径。
getcwd()
的基本实现统计当前目录“.
”,然后打开父目录“..
”并扫描条目直到找到与“.
”具有相同索引节点号的目录名称。然后它向上重复该过程,直到找到根目录,此时它具有完整路径。它在任何时候都不会遍历符号链接。因此,让getcwd()
通过符号链接计算路径的目标是不可能的,无论它是作为系统调用还是作为库函数实现。最好的解决方案是确保构建系统处理包含空格的路径名。这意味着引用通过 shell 传递的路径名。 C 程序不关心名称中的空格;只有当像 shell 这样的程序解释字符串时,您才会遇到问题。 (根据经验,作为运行预处理器的 shell 脚本实现的编译器通常会遇到包含空格的路径名问题。)
There is no way for
getcwd()
to determine the path you followed via symbolic links. The basic implementation ofgetcwd()
stats the current directory '.
', and then opens the parent directory '..
' and scans the entries until it finds the directory name with the same inode number as '.
' has. It then repeats the process upwards until it finds the root directory, at which point it has the full path. At no point does it ever traverse a symbolic link. So the goal of havinggetcwd()
calculate the path followed via symlinks is impossible, whether it is implemented as a system call or as a library function.The best resolution is to ensure that the build system handles path names containing spaces. That means quoting pathnames passed through the shell. C programs don't care about the spaces in the name; it is only when a program like the shell interprets the strings that you run into problems. (Compilers implemented as shell scripts that run pre-processors often have problems with pathnames that contain spaces - speaking from experience.)