Compare commits

..

No commits in common. "536aa57761229c27d4773c720f97e4a875d0800e" and "e1b0831854b58eea3381e29cee39f3f076ee5d7c" have entirely different histories.

38
sdn.cpp
View File

@ -318,9 +318,9 @@ fun invert (cchar_t &ch) {
}
fun apply_attrs (const wstring &w, attr_t attrs) -> ncstring {
ncstring res (w.size (), cchar_t {});
for (size_t i = 0; i < w.size (); i++)
res[i] = cchar (attrs, w[i]);
ncstring res;
for (auto c : w)
res += cchar (attrs, c);
return res;
}
@ -557,8 +557,8 @@ static struct {
// Refreshed by reload():
map<uid_t, wstring> unames; ///< User names by UID
map<gid_t, wstring> gnames; ///< Group names by GID
map<uid_t, string> unames; ///< User names by UID
map<gid_t, string> gnames; ///< Group names by GID
struct tm now; ///< Current local time for display
} g;
@ -638,8 +638,9 @@ fun make_entry (const struct dirent *f) -> entry {
e.info.st_mode = DTTOIF (f->d_type);
auto &info = e.info;
// io_uring is only at most about 50% faster, though it might help with
// slowly statting devices, at a major complexity cost.
// TODO: benchmark just readdir() vs. lstat(), also on dead mounts;
// it might make sense to stat asynchronously in threads
// http://lkml.iu.edu/hypermail//linux/kernel/0804.3/1616.html
if (lstat (f->d_name, &info)) {
e.cols[entry::MODES] = apply_attrs ({ decode_type (info.st_mode),
L'?', L'?', L'?', L'?', L'?', L'?', L'?', L'?', L'?' }, 0);
@ -674,12 +675,12 @@ fun make_entry (const struct dirent *f) -> entry {
auto usr = g.unames.find (info.st_uid);
e.cols[entry::USER] = (usr != g.unames.end ())
? apply_attrs (usr->second, 0)
? apply_attrs (to_wide (usr->second), 0)
: apply_attrs (to_wstring (info.st_uid), 0);
auto grp = g.gnames.find (info.st_gid);
e.cols[entry::GROUP] = (grp != g.gnames.end ())
? apply_attrs (grp->second, 0)
? apply_attrs (to_wide (grp->second), 0)
: apply_attrs (to_wstring (info.st_gid), 0);
auto size = to_wstring (info.st_size);
@ -689,16 +690,16 @@ fun make_entry (const struct dirent *f) -> entry {
else if (info.st_size >> 10) size = to_wstring (info.st_size >> 10) + L"K";
e.cols[entry::SIZE] = apply_attrs (size, 0);
wchar_t buf[32] = L"";
char buf[32] = "";
auto tm = localtime (&info.st_mtime);
wcsftime (buf, sizeof buf / sizeof *buf,
(tm->tm_year == g.now.tm_year) ? L"%b %e %H:%M" : L"%b %e %Y", tm);
e.cols[entry::MTIME] = apply_attrs (buf, 0);
strftime (buf, sizeof buf,
(tm->tm_year == g.now.tm_year) ? "%b %e %H:%M" : "%b %e %Y", tm);
e.cols[entry::MTIME] = apply_attrs (to_wide (buf), 0);
auto &fn = e.cols[entry::FILENAME] =
apply_attrs (to_wide (e.filename), ls_format (e, false));
if (!e.target_path.empty ()) {
fn.append (apply_attrs (L" -> ", 0));
fn.append (apply_attrs (to_wide (" -> "), 0));
fn.append (apply_attrs (to_wide (e.target_path), ls_format (e, true)));
}
return e;
@ -784,10 +785,9 @@ fun update () {
}
fun operator< (const entry &e1, const entry &e2) -> bool {
static string dotdot {".."};
auto t1 = make_tuple (e1.filename != dotdot,
auto t1 = make_tuple (e1.filename != "..",
!S_ISDIR (e1.info.st_mode) && !S_ISDIR (e1.target_info.st_mode));
auto t2 = make_tuple (e2.filename != dotdot,
auto t2 = make_tuple (e2.filename != "..",
!S_ISDIR (e2.info.st_mode) && !S_ISDIR (e2.target_info.st_mode));
if (t1 != t2)
return t1 < t2;
@ -836,12 +836,12 @@ fun resort (const string anchor = at_cursor ().filename) {
fun reload (bool keep_anchor) {
g.unames.clear();
while (auto *ent = getpwent ())
g.unames.emplace (ent->pw_uid, to_wide (ent->pw_name));
g.unames.emplace (ent->pw_uid, ent->pw_name);
endpwent();
g.gnames.clear();
while (auto *ent = getgrent ())
g.gnames.emplace (ent->gr_gid, to_wide (ent->gr_name));
g.gnames.emplace (ent->gr_gid, ent->gr_name);
endgrent();
string anchor;