tree.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2020, Stijn De Ridder <stijn.deridder@hotmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <AK/QuickSort.h>
  28. #include <AK/String.h>
  29. #include <AK/StringBuilder.h>
  30. #include <AK/Vector.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/DirIterator.h>
  33. #include <cstdio>
  34. #include <sys/stat.h>
  35. static bool flag_show_hidden_files = false;
  36. static bool flag_show_only_directories = false;
  37. static int max_depth = INT_MAX;
  38. static int g_directories_seen = 0;
  39. static int g_files_seen = 0;
  40. static void print_directory_tree(const String& root_path, int depth, const String& indent_string)
  41. {
  42. if (depth > 0) {
  43. String root_indent_string;
  44. if (depth > 1) {
  45. root_indent_string = indent_string.substring(0, (depth - 1) * 4);
  46. } else {
  47. root_indent_string = "";
  48. }
  49. printf("%s|-- ", root_indent_string.characters());
  50. }
  51. String root_dir_name = AK::LexicalPath(root_path).basename();
  52. printf("\033[34;1m%s\033[0m\n", root_dir_name.characters());
  53. if (depth >= max_depth) {
  54. return;
  55. }
  56. Core::DirIterator di(root_path, flag_show_hidden_files ? Core::DirIterator::SkipParentAndBaseDir : Core::DirIterator::SkipDots);
  57. if (di.has_error()) {
  58. fprintf(stderr, "%s: %s\n", root_path.characters(), di.error_string());
  59. return;
  60. }
  61. Vector<String> names;
  62. while (di.has_next()) {
  63. String name = di.next_path();
  64. if (di.has_error()) {
  65. fprintf(stderr, "%s: %s\n", root_path.characters(), di.error_string());
  66. continue;
  67. }
  68. names.append(name);
  69. }
  70. quick_sort(names);
  71. for (size_t i = 0; i < names.size(); i++) {
  72. String name = names[i];
  73. StringBuilder builder;
  74. builder.append(root_path);
  75. if (!root_path.ends_with('/')) {
  76. builder.append('/');
  77. }
  78. builder.append(name);
  79. String full_path = builder.to_string();
  80. struct stat st;
  81. int rc = lstat(full_path.characters(), &st);
  82. if (rc == -1) {
  83. fprintf(stderr, "lstat(%s) failed: %s\n", full_path.characters(), strerror(errno));
  84. continue;
  85. }
  86. if (S_ISDIR(st.st_mode)) {
  87. g_directories_seen++;
  88. bool at_last_entry = i == names.size() - 1;
  89. String new_indent_string;
  90. if (at_last_entry) {
  91. new_indent_string = String::format("%s ", indent_string.characters());
  92. } else {
  93. new_indent_string = String::format("%s| ", indent_string.characters());
  94. }
  95. print_directory_tree(full_path.characters(), depth + 1, new_indent_string);
  96. } else if (!flag_show_only_directories) {
  97. g_files_seen++;
  98. printf("%s|-- %s\n", indent_string.characters(), name.characters());
  99. }
  100. }
  101. }
  102. int main(int argc, char** argv)
  103. {
  104. if (pledge("stdio rpath tty", nullptr) < 0) {
  105. perror("pledge");
  106. return 1;
  107. }
  108. Vector<const char*> directories;
  109. Core::ArgsParser args_parser;
  110. args_parser.add_option(flag_show_hidden_files, "Show hidden files", "all", 'a');
  111. args_parser.add_option(flag_show_only_directories, "Show only directories", "only-directories", 'd');
  112. args_parser.add_option(max_depth, "Maximum depth of the tree", "maximum-depth", 'L', "level");
  113. args_parser.add_positional_argument(directories, "Directories to print", "directories", Core::ArgsParser::Required::No);
  114. args_parser.parse(argc, argv);
  115. if (max_depth < 1) {
  116. fprintf(stderr, "%s: Invalid level, must be greater than 0.\n", argv[0]);
  117. return 1;
  118. }
  119. if (directories.is_empty()) {
  120. print_directory_tree(".", 0, "");
  121. printf("\n");
  122. } else {
  123. for (const char* directory : directories) {
  124. print_directory_tree(directory, 0, "");
  125. printf("\n");
  126. }
  127. }
  128. printf("%d directories, %d files\n", g_directories_seen, g_files_seen);
  129. return 0;
  130. }