如果 getcwd() 和 getenv(“PWD”) 不匹配怎么办?

发布于 2024-09-10 10:01:12 字数 747 浏览 10 评论 0原文

我有一个构建系统工具,它使用 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 技术交流群。

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

发布评论

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

评论(2

念﹏祤嫣 2024-09-17 10:01:12

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

由于内核必须维护当前工作目录的知识,因此我们应该能够获取其当前值。不幸的是,内核为每个进程维护的只是当前工作目录的 i 节点号和设备标识。内核不维护目录的完整路径名。

抱歉,看来您确实需要以其他方式解决此问题。

According to the Advanced Programming in the UNIX Environment bible by Stevens, p.112:

Since the kernel must maintain knowledge of the current working directory, we should be able to fetch its current value. Unfortunately, all the kernel maintains for each process is the i-node number and device identification for the current working directory. The kernel does not maintain the full pathname of the directory.

Sorry, looks like you do need to work around this in another way.

星軌x 2024-09-17 10:01:12

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 of getcwd() 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 having getcwd() 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.)

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