541378f05997c9f7e95b6b6dc1675324336538e6
[lunaix-os.git] / lunaix-os / usr / stat / main.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", fd);
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_MDEV)) {
33         if (!((mode & F_SEQDEV) ^ F_SEQDEV)) {
34             ftype = "sequential device";
35         } else if (!((mode & F_VOLDEV) ^ F_VOLDEV)) {
36             ftype = "volumetric device";
37         } else {
38             ftype = "regular device";
39         }
40     } else if ((mode & F_MSLNK)) {
41         if (readlinkat(fd, NULL, buf, 256) < 0) {
42             printf("fail to readlink %d\n", errno);
43         } else {
44             printf("-> %s", buf);
45         }
46         ftype = "symbolic link";
47     } else if ((mode & F_MFILE)) {
48         ftype = "regular file";
49     }
50
51     printf("\nType: %s\n", ftype);
52     printf("Size: %d;  Blocks: %d;  FS Block: %d;  IO Blocks: %d\n",
53            stat.st_size,
54            stat.st_blocks,
55            stat.st_blksize,
56            stat.st_ioblksize);
57     printf("Inode: %d;  ", stat.st_ino);
58
59     dev_t* dev;
60     if (!(stat.mode & F_MDEV)) {
61         dev = &stat.st_dev;
62     } else {
63         dev = &stat.st_rdev;
64     }
65
66     printf("Device: %xh:%d:%d;\n",
67            dev->meta,
68            dev->devident >> 16,
69            dev->devident & 0xffff);
70
71     close(fd);
72     return 0;
73 }