tree.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2022 Roger Knecht <rknecht@pm.me>
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. //config:config TREE
  8. //config: bool "tree (0.6 kb)"
  9. //config: default y
  10. //config: help
  11. //config: List files and directories in a tree structure.
  12. //applet:IF_TREE(APPLET(tree, BB_DIR_USR_BIN, BB_SUID_DROP))
  13. //kbuild:lib-$(CONFIG_TREE) += tree.o
  14. //usage:#define tree_trivial_usage NOUSAGE_STR
  15. //usage:#define tree_full_usage ""
  16. #include "libbb.h"
  17. #include "common_bufsiz.h"
  18. #include "unicode.h"
  19. #define prefix_buf bb_common_bufsiz1
  20. static void tree_print(unsigned count[2], const char* directory_name, char* prefix_pos)
  21. {
  22. struct dirent **entries;
  23. int index, size;
  24. const char *bar = "| ";
  25. const char *mid = "|-- ";
  26. const char *end = "`-- ";
  27. #if ENABLE_UNICODE_SUPPORT
  28. if (unicode_status == UNICODE_ON) {
  29. bar = "│ ";
  30. mid = "├── ";
  31. end = "└── ";
  32. }
  33. #endif
  34. // read directory entries
  35. size = scandir(directory_name, &entries, NULL, alphasort);
  36. if (size < 0) {
  37. fputs_stdout(directory_name);
  38. puts(" [error opening dir]");
  39. return;
  40. }
  41. // print directory name
  42. puts(directory_name);
  43. // switch to sub directory
  44. xchdir(directory_name);
  45. // print all directory entries
  46. for (index = 0; index < size;) {
  47. struct dirent *dirent = entries[index++];
  48. // filter hidden files and directories
  49. if (dirent->d_name[0] != '.') {
  50. int status;
  51. struct stat statBuf;
  52. //TODO: when -l is implemented, use stat, not lstat, if -l
  53. status = lstat(dirent->d_name, &statBuf);
  54. if (index == size) {
  55. strcpy(prefix_pos, end);
  56. } else {
  57. strcpy(prefix_pos, mid);
  58. }
  59. fputs_stdout(prefix_buf);
  60. if (status == 0 && S_ISLNK(statBuf.st_mode)) {
  61. // handle symlink
  62. char* symlink_path = xmalloc_readlink(dirent->d_name);
  63. printf("%s -> %s\n", dirent->d_name, symlink_path);
  64. free(symlink_path);
  65. count[1]++;
  66. } else if (status == 0 && S_ISDIR(statBuf.st_mode)
  67. && (prefix_pos - prefix_buf) < (COMMON_BUFSIZE - 16)
  68. ) {
  69. // handle directory
  70. char* pos;
  71. if (index == size) {
  72. pos = stpcpy(prefix_pos, " ");
  73. } else {
  74. pos = stpcpy(prefix_pos, bar);
  75. }
  76. tree_print(count, dirent->d_name, pos);
  77. count[0]++;
  78. } else {
  79. // handle file
  80. puts(dirent->d_name);
  81. count[1]++;
  82. }
  83. }
  84. // release directory entry
  85. free(dirent);
  86. }
  87. // release directory array
  88. free(entries);
  89. // switch to parent directory
  90. xchdir("..");
  91. }
  92. int tree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  93. int tree_main(int argc UNUSED_PARAM, char **argv)
  94. {
  95. unsigned count[2] = { 0, 0 };
  96. setup_common_bufsiz();
  97. init_unicode();
  98. if (!argv[1])
  99. *argv-- = (char*)".";
  100. // list directories given as command line arguments
  101. while (*(++argv))
  102. tree_print(count, *argv, prefix_buf);
  103. // print statistic
  104. printf("\n%u directories, %u files\n", count[0], count[1]);
  105. return EXIT_SUCCESS;
  106. }