1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "mosapi.h"
4
5int main(int argc, char **argv)
6{
7 if (argc < 2)
8 {
9 fprintf(stderr, format: "Usage: %s <path>...\n", argv[0]);
10 return 1;
11 }
12
13 for (int i = 1; i < argc; i++)
14 {
15 file_stat_t statbuf;
16 if (!lstatat(AT_FDCWD, path: argv[i], buf: &statbuf))
17 {
18 fprintf(stderr, format: "%s: No such file or directory\n", argv[i]);
19 return 1;
20 }
21
22 printf(format: "File: %s\n", argv[i]);
23 printf(format: "File size: %zd bytes\n", statbuf.size);
24 char link_target[MOS_PATH_MAX_LENGTH] = { 0 };
25 switch (statbuf.type)
26 {
27 case FILE_TYPE_REGULAR: puts(s: "Type: Regular file"); break;
28 case FILE_TYPE_DIRECTORY: puts(s: "Type: Directory"); break;
29 case FILE_TYPE_CHAR_DEVICE: puts(s: "Type: Character device"); break;
30 case FILE_TYPE_BLOCK_DEVICE: puts(s: "Type: Block device"); break;
31 case FILE_TYPE_NAMED_PIPE: puts(s: "Type: Pipe"); break;
32 case FILE_TYPE_SOCKET: puts(s: "Type: Socket"); break;
33 case FILE_TYPE_SYMLINK:
34 {
35 puts(s: "Type: Symbolic link");
36 const size_t size = syscall_vfs_readlinkat(AT_FDCWD, path: argv[i], buf: link_target, buf_size: sizeof(link_target));
37 if (size == (size_t) -1)
38 {
39 puts(s: "readlink failed");
40 return 1;
41 }
42 printf(format: "Link target: %s\n", link_target);
43 break;
44 }
45 default: puts(s: "Type: Unknown"); break;
46 }
47
48 printf(format: "Owner: %u:%u\n", statbuf.uid, statbuf.gid);
49
50 char buf[16] = { 0 };
51 file_format_perm(perms: statbuf.perm, buf);
52 printf(format: "Permissions: %.9s", buf);
53
54 if (statbuf.suid)
55 printf(format: "[SUID]");
56 if (statbuf.sgid)
57 printf(format: "[SGID]");
58 if (statbuf.sticky)
59 printf(format: "[STICKY]");
60
61 printf(format: "\n");
62
63 printf(format: "Inode: %llu\n", statbuf.ino);
64 printf(format: "Links: %ld\n", statbuf.nlinks);
65 printf(format: "\n");
66 }
67
68 return 0;
69}
70