listdir_test.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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%c%c%c%c%c%c%c%c%c\t",
  12. item.type,
  13. (item.mode & 0400) ? 'r' : '-',
  14. (item.mode & 0200) ? 'w' : '-',
  15. (item.mode & 0100) ? 'x' : '-',
  16. (item.mode & 0040) ? 'r' : '-',
  17. (item.mode & 0020) ? 'w' : '-',
  18. (item.mode & 0010) ? 'x' : '-',
  19. (item.mode & 0004) ? 'r' : '-',
  20. (item.mode & 0002) ? 'w' : '-',
  21. (item.mode & 0001) ? 'x' : '-');
  22. float hsize;
  23. if (item.size < 1024) {
  24. printf("%lu\t", item.size);
  25. }
  26. else if ((hsize = item.size/1024.0f) < 1024.0f) {
  27. printf("%.1fK\t", hsize);
  28. }
  29. else if ((hsize /= 1024.0f) < 1024.0f) {
  30. printf("%.1fM\t", hsize);
  31. }
  32. else {
  33. hsize /= 1024.0f;
  34. printf("%.1fG\t", hsize);
  35. }
  36. struct tm* tm = localtime(&item.mtime);
  37. printf("%04d-%02d-%02d %02d:%02d:%02d\t",
  38. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  39. printf("%s%s\n", item.name, item.type == 'd' ? "/" : "");
  40. }
  41. return 0;
  42. }