getcwd:使用符号链接
getcwd: work with symbolic links
int
main(void)
{
char *ptr;
size_t size;
if (chdir("/usr/spool/uucppublic") < 0)
err_sys("chdir failed");
ptr = path_alloc(&size);
/* our own function */
if (getcwd(ptr, size) == NULL)
err_sys("getcwd failed");
printf("cwd = %s\n", ptr);
exit(0);
}
$ ./a.out
cwd = /var/spool/uucppublic
$ ls -l /usr/spool
lrwxrwxrwx 1 root 12 Jan 31 07:57 /usr/spool -> ../var/spool
Note that chdir follows the symbolic link—as we expect it to, from
Figure 4.17 — but when it goes up the directory tree, getcwd has no
idea when it hits the /var/spool directory that it is pointed to by
the symbolic link /usr/spool. This is a characteristic of symbolic
links.
以上内容均来自 Rago 和 Stevens 合着的 Advanced Unix Programming 一书。
首先,chdir
遵循符号链接,但是内核在进程的当前工作目录下存储什么?只是 uucppublic
?
其次,作者想表达什么
getcwd has no idea when hits /var/spool
据我了解,getcwd
应该开始读取文件夹 uucppublic
中 ..
的 inode 以跳转到父级 var
的目录 spool
,而不是 usr
。这就是为什么 getcwd
不应该关心是否有符号。因为 chdir
遵循符号链接。
看起来你明白了,但是你解析的英文不对。
getcwd has no idea when it hits the /var/spool directory that it is pointed to by the symbolic link /usr/spool
"when it hits the /var/spool directory"是整个子句的修饰语:
getcwd has no idea that it is pointed to by the symbolic link /usr/spool
在那句话中,"it" 是 "the /var/spool directory"。所以这样读:
getcwd has no idea that the /var/spool directory is pointed to by the symbolic link /usr/spool
您提取的片段:
getcwd has no idea when hits /var/spool
不是有意义的片段,因为它保留了修改 "when" 子句但删除了更重要的 "that" 子句,它是 "has no idea..."
的对象
附带说明一下,您是从一本旧书开始工作的,因此您应该意识到事情已经发生了一些变化。 getcwd
现在是系统调用(至少在 Linux 中),因此不再使用旧算法(遍历 ..
并搜索匹配的 inode 编号)。专用系统调用更快地给出相同的结果。
int
main(void)
{
char *ptr;
size_t size;
if (chdir("/usr/spool/uucppublic") < 0)
err_sys("chdir failed");
ptr = path_alloc(&size);
/* our own function */
if (getcwd(ptr, size) == NULL)
err_sys("getcwd failed");
printf("cwd = %s\n", ptr);
exit(0);
}
$ ./a.out
cwd = /var/spool/uucppublic
$ ls -l /usr/spool
lrwxrwxrwx 1 root 12 Jan 31 07:57 /usr/spool -> ../var/spool
Note that chdir follows the symbolic link—as we expect it to, from Figure 4.17 — but when it goes up the directory tree, getcwd has no idea when it hits the /var/spool directory that it is pointed to by the symbolic link /usr/spool. This is a characteristic of symbolic links.
以上内容均来自 Rago 和 Stevens 合着的 Advanced Unix Programming 一书。
首先,chdir
遵循符号链接,但是内核在进程的当前工作目录下存储什么?只是 uucppublic
?
其次,作者想表达什么
getcwd has no idea when hits /var/spool
据我了解,getcwd
应该开始读取文件夹 uucppublic
中 ..
的 inode 以跳转到父级 var
的目录 spool
,而不是 usr
。这就是为什么 getcwd
不应该关心是否有符号。因为 chdir
遵循符号链接。
看起来你明白了,但是你解析的英文不对。
getcwd has no idea when it hits the /var/spool directory that it is pointed to by the symbolic link /usr/spool
"when it hits the /var/spool directory"是整个子句的修饰语:
getcwd has no idea that it is pointed to by the symbolic link /usr/spool
在那句话中,"it" 是 "the /var/spool directory"。所以这样读:
getcwd has no idea that the /var/spool directory is pointed to by the symbolic link /usr/spool
您提取的片段:
getcwd has no idea when hits /var/spool
不是有意义的片段,因为它保留了修改 "when" 子句但删除了更重要的 "that" 子句,它是 "has no idea..."
的对象附带说明一下,您是从一本旧书开始工作的,因此您应该意识到事情已经发生了一些变化。 getcwd
现在是系统调用(至少在 Linux 中),因此不再使用旧算法(遍历 ..
并搜索匹配的 inode 编号)。专用系统调用更快地给出相同的结果。