link.c 1.0 KB

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