-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstat.cpp
More file actions
171 lines (158 loc) · 6.71 KB
/
stat.cpp
File metadata and controls
171 lines (158 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <cstdio>
#include <cstring>
#include <string>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <pwd.h>
#include <grp.h>
#include <ctime>
#include <cfbox/args.hpp>
#include <cfbox/help.hpp>
#include <cfbox/error.hpp>
namespace {
constexpr cfbox::help::HelpEntry HELP = {
.name = "stat",
.version = CFBOX_VERSION_STRING,
.one_line = "display file or file system status",
.usage = "stat [-c FORMAT] FILE...",
.options = " -c FORMAT use the specified FORMAT instead of the default\n"
" -f display file system status",
.extra = "Format escapes: %n name, %s size, %b blocks, %f blocks*512, %F type,\n"
" %U user, %G group, %a octal perms, %A human perms, %y mtime",
};
} // namespace
static auto file_type_string(mode_t mode) -> const char* {
if (S_ISREG(mode)) return "regular file";
if (S_ISDIR(mode)) return "directory";
if (S_ISLNK(mode)) return "symbolic link";
if (S_ISCHR(mode)) return "character special file";
if (S_ISBLK(mode)) return "block special file";
if (S_ISFIFO(mode)) return "fifo";
if (S_ISSOCK(mode)) return "socket";
return "unknown";
}
static auto format_perms(mode_t mode) -> std::string {
char buf[12];
buf[0] = S_ISDIR(mode) ? 'd' : S_ISLNK(mode) ? 'l' : S_ISCHR(mode) ? 'c' :
S_ISBLK(mode) ? 'b' : S_ISFIFO(mode) ? 'p' : S_ISSOCK(mode) ? 's' : '-';
buf[1] = (mode & S_IRUSR) ? 'r' : '-';
buf[2] = (mode & S_IWUSR) ? 'w' : '-';
buf[3] = (mode & S_IXUSR) ? 'x' : '-';
buf[4] = (mode & S_IRGRP) ? 'r' : '-';
buf[5] = (mode & S_IWGRP) ? 'w' : '-';
buf[6] = (mode & S_IXGRP) ? 'x' : '-';
buf[7] = (mode & S_IROTH) ? 'r' : '-';
buf[8] = (mode & S_IWOTH) ? 'w' : '-';
buf[9] = (mode & S_IXOTH) ? 'x' : '-';
buf[10] = '\0';
return buf;
}
static auto format_time(const timespec& ts) -> std::string {
char buf[64];
auto* tm = localtime(&ts.tv_sec);
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm);
return buf;
}
auto stat_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv, {
cfbox::args::OptSpec{'c', true, "format"},
cfbox::args::OptSpec{'f', false},
});
if (parsed.has_long("help")) { cfbox::help::print_help(HELP); return 0; }
if (parsed.has_long("version")) { cfbox::help::print_version(HELP); return 0; }
auto format = parsed.get_any('c', "format");
bool fs_stat = parsed.has('f');
const auto& pos = parsed.positional();
if (pos.empty()) {
CFBOX_ERR("stat", "missing operand");
return 1;
}
int rc = 0;
for (auto p : pos) {
std::string path{p};
if (fs_stat) {
struct statvfs vfs;
if (statvfs(path.c_str(), &vfs) != 0) {
CFBOX_ERR("stat", "cannot stat '%s': %s", path.c_str(), std::strerror(errno));
rc = 1;
continue;
}
std::printf(" File: \"%s\"\n", path.c_str());
std::printf(" ID: %-12lx Namelen: %-8lu Type: %s\n",
static_cast<unsigned long>(vfs.f_fsid),
static_cast<unsigned long>(vfs.f_namemax),
vfs.f_type == 0xEF53 ? "ext2/ext3" : "unknown");
std::printf("Block size: %-10lu Total blocks: %-10lu Free blocks: %lu\n",
static_cast<unsigned long>(vfs.f_bsize),
static_cast<unsigned long>(vfs.f_blocks),
static_cast<unsigned long>(vfs.f_bfree));
continue;
}
struct stat st;
if (lstat(path.c_str(), &st) != 0) {
CFBOX_ERR("stat", "cannot stat '%s': %s", path.c_str(), std::strerror(errno));
rc = 1;
continue;
}
if (format) {
std::string fmt{*format};
for (std::size_t i = 0; i < fmt.size(); ++i) {
if (fmt[i] == '%' && i + 1 < fmt.size()) {
char spec = fmt[++i];
switch (spec) {
case 'n': std::fputs(path.c_str(), stdout); break;
case 's': std::printf("%lu", static_cast<unsigned long>(st.st_size)); break;
case 'b': std::printf("%lu", static_cast<unsigned long>(st.st_blocks)); break;
case 'f': std::printf("%lu", static_cast<unsigned long>(st.st_blocks * 512)); break;
case 'F': std::fputs(file_type_string(st.st_mode), stdout); break;
case 'U': {
auto* pw = getpwuid(st.st_uid);
std::fputs(pw ? pw->pw_name : std::to_string(st.st_uid).c_str(), stdout);
break;
}
case 'G': {
auto* gr = getgrgid(st.st_gid);
std::fputs(gr ? gr->gr_name : std::to_string(st.st_gid).c_str(), stdout);
break;
}
case 'a': std::printf("%o", st.st_mode & 07777); break;
case 'A': std::fputs(format_perms(st.st_mode).c_str(), stdout); break;
case 'y': {
#if defined(__linux__)
std::fputs(format_time(st.st_mtim).c_str(), stdout);
#else
std::fputs(format_time({st.st_mtime, 0}).c_str(), stdout);
#endif
break;
}
default: std::putchar('%'); std::putchar(spec); break;
}
} else {
std::putchar(fmt[i]);
}
}
std::putchar('\n');
} else {
std::printf(" File: %s\n", path.c_str());
std::printf(" Size: %-15lu Blocks: %-10lu IO Block: %lu %s\n",
static_cast<unsigned long>(st.st_size),
static_cast<unsigned long>(st.st_blocks),
static_cast<unsigned long>(st.st_blksize),
file_type_string(st.st_mode));
auto* pw = getpwuid(st.st_uid);
auto* gr = getgrgid(st.st_gid);
std::printf("Access: (%04o/%s) Uid: (%5u/%-8s) Gid: (%5u/%-8s)\n",
st.st_mode & 07777u,
format_perms(st.st_mode).c_str(),
st.st_uid, pw ? pw->pw_name : "",
st.st_gid, gr ? gr->gr_name : "");
#if defined(__linux__)
std::printf("Modify: %s\n", format_time(st.st_mtim).c_str());
std::printf("Change: %s\n", format_time(st.st_ctim).c_str());
#else
std::printf("Modify: %s\n", format_time({st.st_mtime, 0}).c_str());
#endif
}
}
return rc;
}