#include #include #include #include #include #include using std::cout; using std::vector; using std::priority_queue; using std::string; //this code mainly illustrates the recursive mechanism for displaying //the directories. But it contains enough framework to do the other things. class Comparator { public: const bool operator() (const dirent& a, const dirent& b) { return a.d_name>b.d_name; //to sort by type, we can locate the last '.' and //use the remaining part of the strings //for now I will leave it simple } const bool operator() (const string& s, const string& t) { return s>t; } }; void show_info(const dirent& direntp) { cout<, Comparator> q; priority_queue, Comparator> dirq; while ((direntp=readdir(dirp))!=NULL) { q.push(*direntp); //save directory name for recursion string subname(direntp->d_name); if (subname!="." && subname!="..") { struct stat info; subname=subdir(dirname, subname); lstat(subname.c_str(), &info); //using lstat to avoid stating a symlink if (S_ISDIR(info.st_mode)) dirq.push(subname); } } closedir(dirp); while (!q.empty()) { show_info(q.top()); q.pop(); } //recursion while (!dirq.empty()) { do_ls(dirq.top()); dirq.pop(); } } } int main(int ac, char ** av) { if (ac==1) do_ls("."); else while (--ac) do_ls(*++av); }