I played with my own team pwd
. To find the whole path, I need to go through the tree inode
until I get to the root, and the way to say that I got to the root is by checking the equal inode
numbers stored for .
and ..
.
But on my Mac, which doesn't seem to be the case, at least if you look at this table below.
dirent | stat | link to
-----------+------------+--------
34078072 | 34078072 | self
31103058 | 31103058 | parent
31103058 | 31103058 | self
31103020 | 31103020 | parent
31103020 | 31103020 | self
613497 | 613497 | parent
613497 | 613497 | self
603204 | 603204 | parent
603204 | 603204 | self
157433 | 157433 | parent
157433 | 157433 | self
2 | 2 | parent
2 | 2 | self
1 | 2 | parent
This has been generated using the code below. The structure stat
seems to be doing well, but dirent
has a different meaning when it comes to /
. Why is this so? Should tags dirent
and stat
have the same values ββfor inode number? Why is it different from a Mac?
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
void inodes();
int main()
{
printf(" dirent | stat | link to\n");
printf("-----------+------------+--------\n");
inodes();
return 0;
}
void inodes()
{
DIR* directory = opendir(".");
struct dirent* entry = NULL;
struct stat status;
ino_t self = -1;
ino_t parent = -1;
while ((entry = readdir(directory))) {
stat(entry->d_name, &status);
if (strcmp(entry->d_name, ".") == 0) {
self = status.st_ino;
printf("%10.llu | %10.llu | self\n", entry->d_ino, self);
}
if (strcmp(entry->d_name, "..") == 0) {
parent = status.st_ino;
printf("%10.llu | %10.llu | parent\n", entry->d_ino, parent);
}
}
if (self != parent) {
if (chdir("..") != -1) {
inodes();
}
}
}