tree.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #define prefix_buf bb_common_bufsiz1
  19. static void tree_print(unsigned count[2], const char* directory_name, char* prefix_pos)
  20. {
  21. struct dirent **entries;
  22. int index, size;
  23. // read directory entries
  24. size = scandir(directory_name, &entries, NULL, alphasort);
  25. if (size < 0) {
  26. fputs_stdout(directory_name);
  27. puts(" [error opening dir]");
  28. return;
  29. }
  30. // print directory name
  31. puts(directory_name);
  32. // switch to sub directory
  33. xchdir(directory_name);
  34. // print all directory entries
  35. for (index = 0; index < size;) {
  36. struct dirent *dirent = entries[index++];
  37. // filter hidden files and directories
  38. if (dirent->d_name[0] != '.') {
  39. int status;
  40. struct stat statBuf;
  41. //TODO: when -l is implemented, use stat, not lstat, if -l
  42. status = lstat(dirent->d_name, &statBuf);
  43. if (index == size) {
  44. strcpy(prefix_pos, "└── ");
  45. } else {
  46. strcpy(prefix_pos, "├── ");
  47. }
  48. fputs_stdout(prefix_buf);
  49. if (status == 0 && S_ISLNK(statBuf.st_mode)) {
  50. // handle symlink
  51. char* symlink_path = xmalloc_readlink(dirent->d_name);
  52. printf("%s -> %s\n", dirent->d_name, symlink_path);
  53. free(symlink_path);
  54. count[1]++;
  55. } else if (status == 0 && S_ISDIR(statBuf.st_mode)
  56. && (prefix_pos - prefix_buf) < (COMMON_BUFSIZE - 16)
  57. ) {
  58. // handle directory
  59. char* pos;
  60. if (index == size) {
  61. pos = stpcpy(prefix_pos, " ");
  62. } else {
  63. pos = stpcpy(prefix_pos, "│   ");
  64. }
  65. tree_print(count, dirent->d_name, pos);
  66. count[0]++;
  67. } else {
  68. // handle file
  69. puts(dirent->d_name);
  70. count[1]++;
  71. }
  72. }
  73. // release directory entry
  74. free(dirent);
  75. }
  76. // release directory array
  77. free(entries);
  78. // switch to parent directory
  79. xchdir("..");
  80. }
  81. int tree_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  82. int tree_main(int argc UNUSED_PARAM, char **argv)
  83. {
  84. unsigned count[2] = { 0, 0 };
  85. setup_common_bufsiz();
  86. if (!argv[1])
  87. *argv-- = (char*)".";
  88. // list directories given as command line arguments
  89. while (*(++argv))
  90. tree_print(count, *argv, prefix_buf);
  91. // print statistic
  92. printf("\n%u directories, %u files\n", count[0], count[1]);
  93. return EXIT_SUCCESS;
  94. }