documentation update, readme reformatted
[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 #include <sys/types.h>
6
7 static char buf[256];
8
9 int
10 main(int argc, char* argv[])
11 {
12     if (argc <= 1) {
13         printf("missing operand\n");
14         return 1;
15     }
16
17     int fd = open(argv[1], FO_RDONLY | FO_NOFOLLOW);
18     if (fd < 0) {
19         printf("fail to open %d\n", errno);
20         return 1;
21     }
22
23     struct file_stat stat;
24     if (fstat(fd, &stat) < 0) {
25         printf("fail to get stat %d\n", errno);
26         return 1;
27     }
28
29     printf("File: %s ", argv[1]);
30
31     char* ftype = "directory";
32     int mode = stat.st_mode >> 16;
33     if ((mode & F_DEV)) {
34         ftype = "mappable (sequential) device";
35
36         if (!((mode & F_SVDEV) ^ F_SVDEV)) {
37             ftype = "volumetric device";
38         }
39
40     } else if ((mode & F_SYMLINK)) {
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_FILE) {
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
58     dev_t* dev;
59     if (!(mode & F_DEV)) {
60         dev = &stat.st_dev;
61     } else {
62         dev = &stat.st_rdev;
63     }
64     printf("Device: %xh:%xh@%d;  Inode: %d;  Links: %d\n", 
65            dev->meta, dev->unique, dev->index,
66            stat.st_ino, stat.st_nlink);
67
68     printf("Access: 0%o;  Uid: %d;  Gid: %d\n",
69            stat.st_mode & 0xffff,
70            stat.st_uid,
71            stat.st_gid);
72
73     printf("Access: %lu\n", stat.st_atim);
74     printf("Modify: %lu\n", stat.st_mtim);
75     printf("Create: %lu\n", stat.st_ctim);
76
77     close(fd);
78     return 0;
79 }