link.c 1.1 KB

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