-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.cpp
More file actions
322 lines (291 loc) · 12.5 KB
/
Copy pathls.cpp
File metadata and controls
322 lines (291 loc) · 12.5 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <grp.h>
#include <pwd.h>
#include <string>
#include <string_view>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <vector>
#include <cfbox/args.hpp>
#include <cfbox/error.hpp>
#include <cfbox/fs_util.hpp>
#include <cfbox/help.hpp>
namespace {
auto format_size_human(std::uintmax_t bytes) -> std::string {
const char* units[] = {"", "K", "M", "G", "T"};
int unit_idx = 0;
double size = static_cast<double>(bytes);
while (size >= 1024.0 && unit_idx < 4) {
size /= 1024.0;
++unit_idx;
}
char buf[32];
if (unit_idx == 0) {
std::snprintf(buf, sizeof(buf), "%ju", bytes);
} else {
std::snprintf(buf, sizeof(buf), "%.1f%s", size, units[unit_idx]);
}
return buf;
}
auto format_permissions(std::filesystem::perms p) -> std::string {
char buf[10];
buf[0] = (p & std::filesystem::perms::owner_read) != std::filesystem::perms::none ? 'r' : '-';
buf[1] = (p & std::filesystem::perms::owner_write) != std::filesystem::perms::none ? 'w' : '-';
buf[2] = (p & std::filesystem::perms::owner_exec) != std::filesystem::perms::none ? 'x' : '-';
buf[3] = (p & std::filesystem::perms::group_read) != std::filesystem::perms::none ? 'r' : '-';
buf[4] = (p & std::filesystem::perms::group_write) != std::filesystem::perms::none ? 'w' : '-';
buf[5] = (p & std::filesystem::perms::group_exec) != std::filesystem::perms::none ? 'x' : '-';
buf[6] = (p & std::filesystem::perms::others_read) != std::filesystem::perms::none ? 'r' : '-';
buf[7] = (p & std::filesystem::perms::others_write) != std::filesystem::perms::none ? 'w' : '-';
buf[8] = (p & std::filesystem::perms::others_exec) != std::filesystem::perms::none ? 'x' : '-';
buf[9] = '\0';
return std::string{buf, 9}; // type char is prepended by the caller
}
auto format_type_char(std::filesystem::file_type type) -> char {
switch (type) {
case std::filesystem::file_type::directory:
return 'd';
case std::filesystem::file_type::symlink:
return 'l';
case std::filesystem::file_type::block:
return 'b';
case std::filesystem::file_type::character:
return 'c';
case std::filesystem::file_type::fifo:
return 'p';
case std::filesystem::file_type::socket:
return 's';
default:
return '-';
}
}
auto format_time(std::filesystem::file_time_type ftime) -> std::string {
auto sctp = std::chrono::time_point_cast<std::chrono::system_clock::duration>(
ftime - std::filesystem::file_time_type::clock::now() + std::chrono::system_clock::now());
auto time_t_val = std::chrono::system_clock::to_time_t(sctp);
char buf[64];
std::strftime(buf, sizeof(buf), "%b %e %H:%M", std::localtime(&time_t_val));
return buf;
}
enum class ColorMode { Never, Auto, Always };
struct LsOptions {
bool all = false; // -a
bool long_format = false; // -l
bool human = false; // -h
bool recursive = false; // -R
};
// ANSI SGR code for a file type, or nullptr when no coloring applies. Type-based
// fixed colors only (LS_COLORS glob parsing is deferred to control binary size).
auto color_code(std::filesystem::file_type type, std::filesystem::perms perms, bool use_color) -> const char* {
if (!use_color) return nullptr;
using ft = std::filesystem::file_type;
using pm = std::filesystem::perms;
switch (type) {
case ft::directory: return "01;34"; // bold blue
case ft::symlink: return "01;36"; // bold cyan
case ft::fifo: return "33"; // yellow
case ft::socket: return "01;35"; // bold magenta
case ft::block:
case ft::character: return "01;33"; // bold yellow
case ft::regular: {
bool exec = (perms & (pm::owner_exec | pm::group_exec | pm::others_exec)) != pm::none;
return exec ? "01;32" : nullptr; // bold green
}
default: return nullptr;
}
}
auto colorize(const std::string& name, std::filesystem::file_type type,
std::filesystem::perms perms, bool use_color) -> std::string {
const char* code = color_code(type, perms, use_color);
if (!code) return name;
return "\033[" + std::string{code} + "m" + name + "\033[0m";
}
// Read a directory, drop hidden entries (unless -a), and sort by name.
auto collect_visible(const std::string& path, const LsOptions& opts)
-> cfbox::base::Result<std::vector<std::filesystem::directory_entry>> {
auto entries = cfbox::fs::directory_entries(path);
if (!entries) return std::unexpected(entries.error());
std::vector<std::filesystem::directory_entry> visible;
visible.reserve(entries->size());
for (const auto& e : *entries) {
std::string name = e.path().filename().string();
if (!opts.all && !name.empty() && name[0] == '.') continue;
visible.push_back(e);
}
std::sort(visible.begin(), visible.end(),
[](const std::filesystem::directory_entry& a, const std::filesystem::directory_entry& b) {
return a.path().filename().string() < b.path().filename().string();
});
return visible;
}
// Print a single entry (file or directory member) in short or long form.
// The name is colorized when use_color is set; the symlink "-> target" suffix
// in long form is appended after the colorized name.
auto print_entry(const std::string& path, const LsOptions& opts, bool use_color) -> void {
auto st_r = cfbox::fs::symlink_status(path);
std::filesystem::file_type type = st_r ? st_r->type() : std::filesystem::file_type::none;
std::filesystem::perms perms = st_r ? st_r->permissions() : std::filesystem::perms::none;
std::string name = std::filesystem::path{path}.filename().string();
std::string display = colorize(name, type, perms, use_color);
if (!opts.long_format) {
std::printf("%s\n", display.c_str());
return;
}
char type_char = format_type_char(type);
auto perm_str = format_permissions(perms);
perm_str.insert(perm_str.begin(), type_char);
struct stat lst {};
bool have_stat = (::lstat(path.c_str(), &lst) == 0);
auto nlinks = cfbox::fs::hard_link_count(path).value_or(1);
// Report st_size for every type, not just regular files — directories
// have a meaningful byte size (busybox/GNU show it); cfbox formerly
// printed 0 for non-regular entries.
std::uintmax_t size = have_stat ? static_cast<std::uintmax_t>(lst.st_size) : 0;
auto time_r = cfbox::fs::last_write_time(path);
std::string time_str = time_r ? format_time(*time_r) : "";
std::string size_str = opts.human ? format_size_human(size) : std::to_string(size);
std::string owner = "?";
std::string group = "?";
if (have_stat) {
owner = cfbox::fs::owner_name(lst.st_uid);
group = cfbox::fs::group_name(lst.st_gid);
}
if (type == std::filesystem::file_type::symlink) {
std::error_code ec;
auto target = std::filesystem::read_symlink(std::filesystem::path{path}, ec);
if (!ec) display += " -> " + target.string();
}
std::printf("%s %3ju %-8s %-8s %*s %s %s\n", perm_str.c_str(),
static_cast<std::uintmax_t>(nlinks), owner.c_str(), group.c_str(),
opts.human ? 5 : 8, size_str.c_str(), time_str.c_str(), display.c_str());
}
auto list_directory(const std::string& path, const LsOptions& opts, bool use_color) -> int {
auto visible = collect_visible(path, opts);
if (!visible) {
CFBOX_ERR("ls", "cannot access '%s': %s", path.c_str(), visible.error().msg.c_str());
return 1;
}
for (const auto& e : *visible) {
print_entry(e.path().string(), opts, use_color);
}
return 0;
}
// GNU-style recursive listing: each directory is its own block with a "path:"
// header, blocks separated by a blank line. Symlinked directories are NOT
// descended into (matches GNU ls and prevents cycles).
auto list_recursive(const std::string& path, const LsOptions& opts, bool use_color, bool leading_blank) -> int {
if (leading_blank) std::printf("\n");
std::printf("%s:\n", path.c_str());
int rc = list_directory(path, opts, use_color);
auto visible = collect_visible(path, opts);
if (!visible) return rc;
for (const auto& e : *visible) {
auto st = cfbox::fs::symlink_status(e.path().string());
if (st && st->type() == std::filesystem::file_type::directory) {
if (list_recursive(e.path().string(), opts, use_color, true) != 0) rc = 1;
}
}
return rc;
}
auto list_path(const std::string& path, const LsOptions& opts, bool use_color, bool show_header) -> int {
if (!cfbox::fs::exists(path)) {
CFBOX_ERR("ls", "cannot access '%s': No such file or directory", path.c_str());
return 1;
}
if (!cfbox::fs::is_directory(path)) {
print_entry(path, opts, use_color);
return 0;
}
if (show_header) std::printf("%s:\n", path.c_str());
return list_directory(path, opts, use_color);
}
constexpr cfbox::help::HelpEntry HELP = {
.name = "ls",
.version = CFBOX_VERSION_STRING,
.one_line = "list directory contents",
.usage = "ls [OPTIONS] [FILE]...",
.options = " -a, --all do not ignore entries starting with .\n"
" -l, --long use a long listing format\n"
" -h, --human-readable print sizes in human readable format\n"
" -R, --recursive list subdirectories recursively\n"
" --color[=WHEN] colorize output (always/auto/never)",
.extra = "",
};
} // namespace
auto ls_main(int argc, char* argv[]) -> int {
auto parsed = cfbox::args::parse(argc, argv,
{
cfbox::args::OptSpec{'a', false, "all"},
cfbox::args::OptSpec{'l', false, "long"},
cfbox::args::OptSpec{'h', false, "human-readable"},
cfbox::args::OptSpec{'R', false, "recursive"},
// --color is long-only with an optional value; left
// unregistered so parse records it without eating the
// next positional argument as its value.
});
if (parsed.has_long("help")) {
cfbox::help::print_help(HELP);
return 0;
}
if (parsed.has_long("version")) {
cfbox::help::print_version(HELP);
return 0;
}
LsOptions opts;
opts.all = parsed.has('a');
opts.long_format = parsed.has('l');
opts.human = parsed.has('h');
opts.recursive = parsed.has('R') || parsed.has_long("recursive");
// --color: a bare flag means "always"; otherwise honor always/auto/never.
ColorMode color = ColorMode::Auto;
if (parsed.has_long("color")) {
auto v = parsed.get_long("color");
if (!v || *v == "always") color = ColorMode::Always;
else if (*v == "never") color = ColorMode::Never;
else if (*v == "auto") color = ColorMode::Auto;
else {
CFBOX_ERR("ls", "invalid argument '%.*s' for '--color'",
static_cast<int>(v->size()), v->data());
return 2;
}
}
bool use_color = (color == ColorMode::Always) ||
(color == ColorMode::Auto && ::isatty(STDOUT_FILENO) == 1);
const auto& pos = parsed.positional();
if (opts.recursive) {
if (pos.empty()) {
return list_recursive(".", opts, use_color, /*leading_blank=*/false);
}
int rc = 0;
bool first = true;
for (const auto& p : pos) {
std::string path{p};
if (!cfbox::fs::exists(path)) {
CFBOX_ERR("ls", "cannot access '%s': No such file or directory", path.c_str());
rc = 1;
first = false;
continue;
}
if (!cfbox::fs::is_directory(path)) {
print_entry(path, opts, use_color);
} else {
if (list_recursive(path, opts, use_color, /*leading_blank=*/!first) != 0) rc = 1;
}
first = false;
}
return rc;
}
bool multi = pos.size() > 1;
if (pos.empty()) {
return list_path(".", opts, use_color, false);
}
int rc = 0;
for (const auto& p : pos) {
if (list_path(std::string{p}, opts, use_color, multi) != 0) rc = 1;
}
return rc;
}