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