命令ls -l
如何计算inode的硬链接数量? 它使用Linux API还是需要更深入的Linux内核源代码知识的代码?
我还没有能够理解ls
的源代码,因为我刚刚开始学习C.
这是一个真正简单的程序,说明stat()
用户查找硬链接计数:
#include <stdio.h> #include <sys/stat.h> int main ( int argc, char ** argv ) { int i; struct stat st; /* stat puts info here */ for (i = 1; i < argc; ++i) { if (stat(argv[i], &st) == -1) perror(argv[i]); else printf("%s has %d hard links\n", argv[i], st.st_nlink); } return 0; }
(在命令行上传递一个或多个文件名)
它调用stat
来获取关于该文件的信息(在struct stat
)。 然后它查看struct stat
的st_nlink
字段。