link.c 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <sys/stat.h>
  4. #include <unistd.h>
  5. int main(void) {
  6. printf("%ld\n", sizeof(struct stat));
  7. struct stat buf;
  8. // Stat for the inode
  9. if (stat("unistd/link.c", &buf)) {
  10. perror("stat");
  11. return 1;
  12. }
  13. unsigned long inode = buf.st_ino;
  14. printf("%ld\n", inode);
  15. // Create the link
  16. if (link("unistd/link.c", "link.out")) {
  17. perror("link");
  18. return 1;
  19. }
  20. // Make sure inodes match
  21. if (stat("link.out", &buf)) {
  22. perror("stat");
  23. }
  24. printf("%ld\n", inode);
  25. printf("%ld\n", buf.st_ino);
  26. if (inode != buf.st_ino) {
  27. puts("Created file is not a link.");
  28. printf("unistd/link.c inode: %ld\n", inode);
  29. printf("link.out inode: %ld\n", buf.st_ino);
  30. }
  31. // Remove link
  32. if (unlink("link.out")) {
  33. perror("unlink");
  34. return 1;
  35. }
  36. if (!stat("link.out", &buf) || errno != ENOENT) {
  37. perror("stat");
  38. return 1;
  39. }
  40. }