From: Mikko Rasa Date: Sun, 3 Sep 2023 22:17:21 +0000 (+0300) Subject: Make the ls example print symlink targets X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=6d1594b62f169e3786247a2a62a255986b56d763;p=libs%2Fcore.git Make the ls example print symlink targets --- diff --git a/examples/ls.cpp b/examples/ls.cpp index 42667d5..cb7d6be 100644 --- a/examples/ls.cpp +++ b/examples/ls.cpp @@ -23,7 +23,7 @@ public: virtual int main(); private: - Row create_row(const string &, const FS::Stat &); + Row create_row(const string &, const FS::Stat &, const string & = string()); void print_rows(const vector &); }; @@ -55,7 +55,11 @@ int Ls::main() for(const string &fn: files) { if(long_format) - rows.push_back(create_row(fn, FS::lstat(path/fn))); + { + FS::Path file_path = path/fn; + FS::Stat file_stat = FS::lstat(file_path); + rows.push_back(create_row(fn, file_stat, (file_stat.is_symlink() ? readlink(file_path).str() : string()))); + } else { Row r; @@ -92,7 +96,7 @@ int Ls::main() return 0; } -Ls::Row Ls::create_row(const string &name, const FS::Stat &st) +Ls::Row Ls::create_row(const string &name, const FS::Stat &st, const string &link_target) { Row result; if(st.is_regular()) @@ -109,6 +113,8 @@ Ls::Row Ls::create_row(const string &name, const FS::Stat &st) result.push_back(format("%d", st.get_size())); result.push_back(Time::DateTime(st.get_modify_time()).format("%Y-%m-%d %H:%M:%S")); result.push_back(name); + if(!link_target.empty()) + result.back() += format(" -> %s", link_target); return result; } @@ -136,7 +142,7 @@ void Ls::print_rows(const vector &rows) if(j==3) line += string(padding, ' '); line += row[j]; - if(j!=3) + if(j!=3 && j+1