-2

I have to write out all hard links on file in C. I have no idea how to do that. One possibility is also to call bash command but which command to call?

Given: file name 'foo.txt'

Find: all files that are hard links to 'foo.txt'

Primoz
  • 1,324
  • 2
  • 16
  • 34
  • 4
    `printf("all hard links on file");` will "write out all hard links on file in C". On the assumption that you want a better response, you must provide a better question because SO is not a "write a program for me" service. Start with what you've tried or at least what you've considered. – mah May 30 '14 at 12:11
  • 4
    In the first 3 results of googling to "*show all links to file*": http://superuser.com/questions/12972/how-can-you-see-the-actual-hard-link-by-ls http://stackoverflow.com/questions/6184849/symbolic-link-find-all-files-that-link-to-this-file – alk May 30 '14 at 12:12
  • @alk the answers in your linked posts use executables that are called from the shell... This question wants help with a C implementation. – aglasser May 30 '14 at 14:55
  • @aglasser: Verbatim from the question: "*One possibility is also to call bash ...*" – alk May 30 '14 at 14:57
  • @alk I should probably read the entire question in the future and not assume that people are wrong. Sorry about that!! – aglasser May 30 '14 at 14:59

2 Answers2

2

Get the inode number of the file (ls -i will give it), then use find root_path_of_the_partition -inum inode_number. Beware to find by inode number on the same partition, as it is possible that two different files have the same inode number, provided that they are on different partitions.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

The other answer apparently relies on the ls command, but this can be done without it. Use lstat to put file (inode) information into a struct stat. For example:

#include <sys/stat.h>
// ... inside main ...
struct stat stats;
if (argc == 2)
  lstat(argv[1], &stats)
printf("Link count: %d\n", stats->st_nlink);

You should also check to see if lstat failed (if (lstat(argv[1], &stats) != 0) {...}) before you proceed. Just giving you a starting point for this.


Adding some more code in case you want to find links pertaining to all files in a directory instead of just one file as an argument.

DIR *dp; 
struct stat stats;
// ...
if ((dp = opendir(".")) == NULL) {
    perror("Failed to open directory");
    exit(-1);
}

while ((dir = readdir(dp)) != NULL) {
    if (lstat(dir->d_name, &stats) != 0) {
        perror("lstat failed: ");
        exit(-1);
    }

    printf("Link count: %d\n, stats->st_nlink);
}
aglasser
  • 3,059
  • 3
  • 13
  • 10
  • Yes you're right I didn't saw the "C" requirement. Now writing a C prog to find all links to a given inode is not easy. One need to recurse down all path from a given starting point and find the right inodes. – Jean-Baptiste Yunès May 30 '14 at 15:07
  • It's not hard, we implemented this in an Introduction to Operating Systems course... – aglasser May 30 '14 at 15:24
  • I said "not easy" not "hard". Recurse and find is not hard, what is more tricky is the determination of the partition's root. – Jean-Baptiste Yunès May 30 '14 at 15:27