I need to list all hard links of a given file in "pure" C, so without help of bash commands.
I googled for hours but couldn't find anything useful. My current idea is to get inode number then loop through directory to find files with same inode.
In bash something like
sudo find / -inum 199053
Any better suggestion?
Thanks
To get the inode number of a single file, invoke the stat function and reference the st_ino value of the returned struct.
int result;
struct stat s;
result = stat("filename.txt", &s);
if ((result == 0) && (s.st_ino == 199053))
{
// match
}
You could build a solution with the stat function using opendir, readdir, and closedir to recursively scan a directory hierarchy to look for matching inode values.
You could also use scandir to scan an entire directory:
int filter(const struct dirent* entry)
{
return (entry->d_ino == 199053) ? 1 : 0;
}
int main()
{
struct dirent **namelist = NULL;
scandir("/home/selbie", &namelist, filter, NULL);
free(namelist);
return 0;
}