make rule for user header file install
[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.st_mode >> 16;
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
57     dev_t* dev;
58     if (!(mode & F_DEV)) {
59         dev = &stat.st_dev;
60     } else {
61         dev = &stat.st_rdev;
62     }
63     printf("Device: %xh:%xh@%d;  Inode: %d;  Links: %d\n", 
64            dev->meta, dev->unique, dev->index,
65            stat.st_ino, stat.st_nlink);
66
67     printf("Access: 0%o;  Uid: %d;  Gid: %d\n",
68            stat.st_mode & 0xffff,
69            stat.st_uid,
70            stat.st_gid);
71
72     printf("Access: %lu\n", stat.st_atim);
73     printf("Modify: %lu\n", stat.st_mtim);
74     printf("Create: %lu\n", stat.st_ctim);
75
76     close(fd);
77     return 0;
78 }