listdir_test.cpp 951 B

12345678910111213141516171819202122232425262728293031323334
  1. #include <stdio.h>
  2. #include "hdir.h"
  3. int main(int argc, char* argv[]) {
  4. const char* dir = ".";
  5. if (argc > 1) {
  6. dir = argv[1];
  7. }
  8. std::list<hdir_t> dirs;
  9. listdir(dir, dirs);
  10. for (auto& item : dirs) {
  11. printf("%c\t", item.type);
  12. float hsize;
  13. if (item.size < 1024) {
  14. printf("%lu\t", item.size);
  15. }
  16. else if ((hsize = item.size/1024.0f) < 1024.0f) {
  17. printf("%.1fK\t", hsize);
  18. }
  19. else if ((hsize /= 1024.0f) < 1024.0f) {
  20. printf("%.1fM\t", hsize);
  21. }
  22. else {
  23. hsize /= 1024.0f;
  24. printf("%.1fG\t", hsize);
  25. }
  26. struct tm* tm = localtime(&item.mtime);
  27. printf("%04d-%02d-%02d %02d:%02d:%02d\t",
  28. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  29. printf("%s%s\n", item.name, item.type == 'd' ? "/" : "");
  30. }
  31. return 0;
  32. }