You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

72 lines
1.6 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dirent.h>
  5. #include <sys/stat.h>
  6. struct file {
  7. ino_t inode;
  8. char names[64][256];
  9. int sz;
  10. };
  11. int putToFiles(char name[256], ino_t inode, struct file *files, int sz) {
  12. // if found the same inode
  13. for (int i = 0; i < sz; ++i) {
  14. if (files[i].inode == inode) {
  15. memcpy(files[i].names[files[i].sz++], name, 256);
  16. return sz;
  17. }
  18. }
  19. // if not found
  20. files[sz].inode = inode;
  21. memcpy(files[sz].names[0], name, 256);
  22. files[sz].sz = 1;
  23. sz++;
  24. return sz;
  25. }
  26. int main() {
  27. DIR *d = opendir("./tmp");
  28. if (d == NULL) {
  29. perror("opendir");
  30. exit(1);
  31. }
  32. struct file files[64];
  33. int sz = 0;
  34. struct dirent *entry;
  35. while ((entry = readdir(d))!= NULL) {
  36. if (entry->d_name[0] == '.')
  37. continue;
  38. //printf("%s ", entry->d_name);
  39. struct stat s;
  40. char buf[256] = "./tmp/";
  41. strncpy(buf + strlen(buf), entry->d_name, 200);
  42. if (stat(buf, &s) == -1) {
  43. perror("stat");
  44. exit(1);
  45. }
  46. sz = putToFiles(entry->d_name, s.st_ino, files, sz);
  47. }
  48. printf("File --- Hard links\n");
  49. for (int i = 0; i < sz; ++i) {
  50. if (files[i].sz > 1) {
  51. for (int j = 0; j < files[i].sz; ++j) {
  52. printf("%s --- ", files[i].names[j]);
  53. for (int k = 0; k < files[i].sz; ++k) {
  54. printf("%s ", files[i].names[k]);
  55. }
  56. printf("\n");
  57. }
  58. }
  59. }
  60. }