Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/String.h>
28#include <LibCore/DateTime.h>
29#include <grp.h>
30#include <pwd.h>
31#include <stdio.h>
32#include <sys/stat.h>
33#include <time.h>
34#include <unistd.h>
35
36int main(int argc, char** argv)
37{
38 if (pledge("stdio rpath", nullptr) < 0) {
39 perror("pledge");
40 return 1;
41 }
42
43 if (argc == 1) {
44 printf("stat <file>\n");
45 return 1;
46 }
47 struct stat st;
48 int rc = lstat(argv[1], &st);
49 if (rc < 0) {
50 perror("lstat");
51 return 1;
52 }
53 printf(" File: %s\n", argv[1]);
54 printf(" Inode: %u\n", st.st_ino);
55 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
56 printf(" Device: %u,%u\n", major(st.st_rdev), minor(st.st_rdev));
57 else
58 printf(" Size: %u\n", st.st_size);
59 printf(" Links: %u\n", st.st_nlink);
60 printf(" Blocks: %u\n", st.st_blocks);
61 printf(" UID: %u", st.st_uid);
62 if (auto* pwd = getpwuid(st.st_uid)) {
63 printf(" (%s)", pwd->pw_name);
64 }
65 printf("\n");
66 printf(" GID: %u", st.st_gid);
67 if (auto* grp = getgrgid(st.st_gid)) {
68 printf(" (%s)", grp->gr_name);
69 }
70 printf("\n");
71 printf(" Mode: (%o/", st.st_mode);
72
73 if (S_ISDIR(st.st_mode))
74 printf("d");
75 else if (S_ISLNK(st.st_mode))
76 printf("l");
77 else if (S_ISBLK(st.st_mode))
78 printf("b");
79 else if (S_ISCHR(st.st_mode))
80 printf("c");
81 else if (S_ISFIFO(st.st_mode))
82 printf("f");
83 else if (S_ISSOCK(st.st_mode))
84 printf("s");
85 else if (S_ISREG(st.st_mode))
86 printf("-");
87 else
88 printf("?");
89
90 printf("%c%c%c%c%c%c%c%c",
91 st.st_mode & S_IRUSR ? 'r' : '-',
92 st.st_mode & S_IWUSR ? 'w' : '-',
93 st.st_mode & S_ISUID ? 's' : (st.st_mode & S_IXUSR ? 'x' : '-'),
94 st.st_mode & S_IRGRP ? 'r' : '-',
95 st.st_mode & S_IWGRP ? 'w' : '-',
96 st.st_mode & S_ISGID ? 's' : (st.st_mode & S_IXGRP ? 'x' : '-'),
97 st.st_mode & S_IROTH ? 'r' : '-',
98 st.st_mode & S_IWOTH ? 'w' : '-');
99
100 if (st.st_mode & S_ISVTX)
101 printf("t");
102 else
103 printf("%c", st.st_mode & S_IXOTH ? 'x' : '-');
104
105 printf(")\n");
106
107 auto print_time = [](time_t t) {
108 printf("%s\n", Core::DateTime::from_timestamp(t).to_string().characters());
109 };
110
111 printf("Accessed: ");
112 print_time(st.st_atime);
113 printf("Modified: ");
114 print_time(st.st_mtime);
115 printf(" Changed: ");
116 print_time(st.st_ctime);
117
118 return 0;
119}