The original text is below.It is in Section 4.22
The program in Figure 4.24 changes to a specific directory and then calls
getcwdto print the working directory. If we run the program, we get$ ./a.out cwd = /var/spool/uucppublic $ ls -l /usr/spool lrwxrwxrwx 1 root 12 Jan 31 07:57 /usr/spool -> ../var/spoolNote 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.
What does the author really mean by saying that the program hits the /var/spool?
What is the characteristic of symbolic links pointed out by the author?
I did not really understand.
Note that some shells, notably
bash, keep track of whether you arrived at a given directory by chasing a symbolic link, and print the current directory accordingly. At leastbashhas options tocdto do a physical or logical change directory:In the scenario shown, where
/usr/spoolis a symbolic link to/var/spool, then:For most people, a plain
cd ..would do the same ascd -L ... You can choose to havebashdo the same ascd -P ..instead if you prefer (usingset -Porset -L).The process of finding the pathname of the current directory should be understood too. Logically, the process (kernel) opens the current directory (
.) and reads the inode number (and device number). It then opens the parent directory (..), and reads entries from that until it finds one with the matching inode number (and device number). This then gives it the last component of the pathname. It can now repeat the process, finding the the inode number of the next directory up, and opening its parent directory (../..), etc, until it reaches the root directory (where the inode number for both.and..is the same, and the value is conventionally 2). Note that this even works across mount points. Beware of auto-mounted remote (NFS) file systems, though; it can be really slow if you scan through a directory containing several hundred automounted machines – as the naïve search outline above mounts all the machines until it finds the correct one. So, actualgetcwd()functions are cleverer than this, but it explains how the path of the current directory is found. And it also shows why the process will not encounter/usr/spoolwhen evaluating the directory under/var/spool/uucppublic– it simply never opens the/usrdirectory.Note that the
realpath()function (system call) takes a name possibly referencing symlinks and resolves it to a name that contains no symlinks at all. Passed/usr/spool/uucppublic, it would return/var/spool/uucppublic, for example.