Merge branch 'master' into isa/arm64
[lunaix-os.git] / lunaix-os / usr / stat.c
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <unistd.h>
5
6 static char buf[256];
7
8 int
9 main(int argc, char* argv[])
10 {
11     if (argc <= 1) {
12         printf("missing operand\n");
13         return 1;
14     }
15
16     int fd = open(argv[1], FO_RDONLY | FO_NOFOLLOW);
17     if (fd < 0) {
18         printf("fail to open %d\n", errno);
19         return 1;
20     }
21
22     struct file_stat stat;
23     if (fstat(fd, &stat) < 0) {
24         printf("fail to get stat %d\n", errno);
25         return 1;
26     }
27
28     printf("File: %s ", argv[1]);
29
30     char* ftype = "directory";
31     int mode = stat.mode;
32     if ((mode & F_DEV)) {
33         ftype = "mappable (sequential) device";
34
35         if (!((mode & F_SVDEV) ^ F_SVDEV)) {
36             ftype = "volumetric device";
37         }
38
39     } else if ((mode & F_SYMLINK)) {
40         if (readlinkat(fd, NULL, buf, 256) < 0) {
41             printf("fail to readlink %d\n", errno);
42         } else {
43             printf("-> %s", buf);
44         }
45         ftype = "symbolic link";
46     } else if (mode == F_FILE) {
47         ftype = "regular file";
48     }
49
50     printf("\nType: %s\n", ftype);
51     printf("Size: %d;  Blocks: %d;  FS Block: %d;  IO Blocks: %d\n",
52            stat.st_size,
53            stat.st_blocks,
54            stat.st_blksize,
55            stat.st_ioblksize);
56     printf("Inode: %d;  ", stat.st_ino);
57
58     dev_t* dev;
59     if (!(stat.mode & F_DEV)) {
60         dev = &stat.st_dev;
61     } else {
62         dev = &stat.st_rdev;
63     }
64
65     printf("Device: %xh:%xh@%d;\n", dev->meta, dev->unique, dev->index);
66
67     close(fd);
68     return 0;
69 }