How does the ls command find the hard link count?

后端 未结 2 1051
情歌与酒
情歌与酒 2021-01-27 09:21

How does the command ls -l count the number of hard links of an inode? Does it use the Linux API or is it code that requires deeper knowledge of the Linux kernel so

2条回答
  •  孤独总比滥情好
    2021-01-27 09:41

    Here is a real simple program illustrating the user of stat() to find hard-link counts:

    #include 
    #include 
    
    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;
    }
    

    (Pass it one or more file-names on the command line)

提交回复
热议问题