]> git.tdb.fi Git - libs/core.git/commitdiff
Make the ls example print symlink targets
authorMikko Rasa <tdb@tdb.fi>
Sun, 3 Sep 2023 22:17:21 +0000 (01:17 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 3 Sep 2023 22:17:21 +0000 (01:17 +0300)
examples/ls.cpp

index 42667d5e07acd998038ee4e1a584f606aa6987b7..cb7d6be2dc9aea6ed230bd9ad73717a830f198cb 100644 (file)
@@ -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<Row> &);
 };
 
@@ -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<Row> &rows)
                        if(j==3)
                                line += string(padding, ' ');
                        line += row[j];
-                       if(j!=3)
+                       if(j!=3 && j+1<row.size())
                                line += string(padding, ' ');
                }
                line += '\n';