scandir 的相对路径处理
Relative Path Handling by scandir
函数 scandir 是否处理相对路径?
也就是说,下面两段代码是等价的吗?
int NUM = scandir("/home/hello/wait/../pqr/../../hello", &LIST, 0, alphasort);
int NUM = scandir("/home/hello", &LIST, 0, alphasort);
如果不是,是否有一些简单的内置函数可以将 /home/hello/wait/../pqr/../../hello
转换为 /home/hello
?
Does the function scandir handle relative paths?
我相信答案是肯定的。从 posix scandir 我们只知道:
The scandir() function shall scan the directory dir, [...]
来自posix定义:
3.129 Directory
A file that contains directory entries. No two directory entries in the same directory have the same name
scandir
的参数是char *
,所以它是一个字符串。从 posix 定义我们知道:
3.271 Pathname
A string that is used to identify a file. [...]
另一个名为 4.13 Pathname Resolution
的部分深入研究了路径名是如何解析的,并告诉我们:
If the pathname does not begin with a <slash>, the predecessor of the first filename of the pathname shall be taken to be either the current working directory of the process [...] (such pathnames are referred to as "relative pathnames").
所以因为 scandir
需要一个目录的路径名并且因为路径名是如何解析的,是的,你可以给 scandir 的相对路径。
In other words
这不是“其他词”——您显示的两个路径名都是绝对的,而不是相对的。在路径名中使用 dot-dot 不会使路径名成为相对路径。
are the following two pieces of code equivalent?
是的。
is there some simple buit-in function to convert /home/hello/wait/../pqr/../../hello to /home/hello?
没有"builtin function" to canonicalize a pathname in any compiler I am aware of. There exists a completely normal function provided from POSIX for that purpose and is called realpath(). GNU library may allocated the returned string dynamically and it also has canonicalize_file_name.
函数 scandir 是否处理相对路径?
也就是说,下面两段代码是等价的吗?
int NUM = scandir("/home/hello/wait/../pqr/../../hello", &LIST, 0, alphasort);
int NUM = scandir("/home/hello", &LIST, 0, alphasort);
如果不是,是否有一些简单的内置函数可以将 /home/hello/wait/../pqr/../../hello
转换为 /home/hello
?
Does the function scandir handle relative paths?
我相信答案是肯定的。从 posix scandir 我们只知道:
The scandir() function shall scan the directory dir, [...]
来自posix定义:
3.129 Directory
A file that contains directory entries. No two directory entries in the same directory have the same name
scandir
的参数是char *
,所以它是一个字符串。从 posix 定义我们知道:
3.271 Pathname
A string that is used to identify a file. [...]
另一个名为 4.13 Pathname Resolution
的部分深入研究了路径名是如何解析的,并告诉我们:
If the pathname does not begin with a <slash>, the predecessor of the first filename of the pathname shall be taken to be either the current working directory of the process [...] (such pathnames are referred to as "relative pathnames").
所以因为 scandir
需要一个目录的路径名并且因为路径名是如何解析的,是的,你可以给 scandir 的相对路径。
In other words
这不是“其他词”——您显示的两个路径名都是绝对的,而不是相对的。在路径名中使用 dot-dot 不会使路径名成为相对路径。
are the following two pieces of code equivalent?
是的。
is there some simple buit-in function to convert /home/hello/wait/../pqr/../../hello to /home/hello?
没有"builtin function" to canonicalize a pathname in any compiler I am aware of. There exists a completely normal function provided from POSIX for that purpose and is called realpath(). GNU library may allocated the returned string dynamically and it also has canonicalize_file_name.