在Linux上解析$ PATH时,需要考虑哪些角落? [英] What corner cases must we consider when parsing $PATH on Linux?
问题描述
我正在使用一个C应用程序,必须走$ PATH才能找到二进制文件的完整路径名,唯一允许的依赖关系是glibc(即不调用外部程序)。在正常情况下,这只需要通过冒号分割getenv(PATH)并逐个检查每个目录,但我想确保覆盖所有可能的角落。我应该注意什么?特别是相对路径,以〜意为扩展为$ HOME的路径,或包含以下内容的路径允许?
I'm working on a C application that has to walk $PATH to find full pathnames for binaries, and the only allowed dependency is glibc (i.e. no calling external programs like which). In the normal case, this just entails splitting getenv("PATH") by colons and checking each directory one by one, but I want to be sure I cover all of the possible corner cases. What gotchas should I look out for? In particular, are relative paths, paths starting with ~ meant to be expanded to $HOME, or paths containing the : char allowed?
推荐答案
p>有一件让我惊讶的是, PATH
中的空字符串表示当前目录。 PATH
结尾或开头的两个相邻冒号或冒号表示包含当前目录。这在 man bash 中有所记载。
One thing that once surprised me is that the empty string in PATH
means the current directory. Two adjacent colons or a colon at the end or beginning of PATH
means the current directory is included. This is documented in man bash for instance.
它也位于 POSIX规范。
所以
PATH=:/bin
PATH=/bin:
PATH=/bin::/usr/bin
在 PATH
这篇关于在Linux上解析$ PATH时,需要考虑哪些角落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!