X-Git-Url: https://scm.lunaixsky.com/lunaix-os.git/blobdiff_plain/bef1210245bb3180a57f26405369654eaa477f63..1eeed1150149b63d6e49e033697454bc12b533b9:/lunaix-os/usr/stat.c diff --git a/lunaix-os/usr/stat.c b/lunaix-os/usr/stat.c new file mode 100644 index 0000000..108f462 --- /dev/null +++ b/lunaix-os/usr/stat.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +static char buf[256]; + +int +main(int argc, char* argv[]) +{ + if (argc <= 1) { + printf("missing operand\n"); + return 1; + } + + int fd = open(argv[1], FO_RDONLY | FO_NOFOLLOW); + if (fd < 0) { + printf("fail to open %d\n", fd); + return 1; + } + + struct file_stat stat; + if (fstat(fd, &stat) < 0) { + printf("fail to get stat %d\n", errno); + return 1; + } + + printf("File: %s ", argv[1]); + + char* ftype = "directory"; + int mode = stat.mode; + if ((mode & F_MDEV)) { + if (!((mode & F_SEQDEV) ^ F_SEQDEV)) { + ftype = "sequential device"; + } else if (!((mode & F_VOLDEV) ^ F_VOLDEV)) { + ftype = "volumetric device"; + } else { + ftype = "regular device"; + } + } else if ((mode & F_MSLNK)) { + if (readlinkat(fd, NULL, buf, 256) < 0) { + printf("fail to readlink %d\n", errno); + } else { + printf("-> %s", buf); + } + ftype = "symbolic link"; + } else if ((mode & F_MFILE)) { + ftype = "regular file"; + } + + printf("\nType: %s\n", ftype); + printf("Size: %d; Blocks: %d; FS Block: %d; IO Blocks: %d\n", + stat.st_size, + stat.st_blocks, + stat.st_blksize, + stat.st_ioblksize); + printf("Inode: %d; ", stat.st_ino); + + dev_t* dev; + if (!(stat.mode & F_MDEV)) { + dev = &stat.st_dev; + } else { + dev = &stat.st_rdev; + } + + printf("Device: %xh:%xh@%d;\n", dev->meta, dev->unique, dev->index); + + close(fd); + return 0; +} \ No newline at end of file