| 24 | namespace fs = boost::filesystem; |
| 25 | |
| 26 | int main(int argc, char* argv[]) |
| 27 | { |
| 28 | fs::path p(fs::current_path()); |
| 29 | |
| 30 | if (argc > 1) |
| 31 | p = fs::system_complete(argv[1]); |
| 32 | else |
| 33 | std::cout << "\nusage: simple_ls [path]" << std::endl; |
| 34 | |
| 35 | unsigned long file_count = 0; |
| 36 | unsigned long dir_count = 0; |
| 37 | unsigned long other_count = 0; |
| 38 | unsigned long err_count = 0; |
| 39 | |
| 40 | if (!fs::exists(p)) |
| 41 | { |
| 42 | std::cout << "\nNot found: " << p << std::endl; |
| 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | if (fs::is_directory(p)) |
| 47 | { |
| 48 | std::cout << "\nIn directory: " << p << "\n\n"; |
| 49 | fs::directory_iterator end_iter; |
| 50 | for (fs::directory_iterator dir_itr(p); |
| 51 | dir_itr != end_iter; |
| 52 | ++dir_itr) |
| 53 | { |
| 54 | try |
| 55 | { |
| 56 | if (fs::is_directory(dir_itr->status())) |
| 57 | { |
| 58 | ++dir_count; |
| 59 | std::cout << dir_itr->path().filename() << " [directory]\n"; |
| 60 | } |
| 61 | else if (fs::is_regular_file(dir_itr->status())) |
| 62 | { |
| 63 | ++file_count; |
| 64 | std::cout << dir_itr->path().filename() << "\n"; |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | ++other_count; |
| 69 | std::cout << dir_itr->path().filename() << " [other]\n"; |
| 70 | } |
| 71 | } |
| 72 | catch (const std::exception& ex) |
| 73 | { |
| 74 | ++err_count; |
| 75 | std::cout << dir_itr->path().filename() << " " << ex.what() << std::endl; |
| 76 | } |
| 77 | } |
| 78 | std::cout << "\n" |
| 79 | << file_count << " files\n" |
| 80 | << dir_count << " directories\n" |
| 81 | << other_count << " others\n" |
| 82 | << err_count << " errors\n"; |
| 83 | } |
nothing calls this directly
no test coverage detected