hdir.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef HW_DIR_H_
  2. #define HW_DIR_H_
  3. /*
  4. *@code
  5. int main(int argc, char* argv[]) {
  6. const char* dir = ".";
  7. if (argc > 1) {
  8. dir = argv[1];
  9. }
  10. std::list<hdir_t> dirs;
  11. listdir(dir, dirs);
  12. for (auto& item : dirs) {
  13. printf("%c\t", item.type);
  14. float hsize;
  15. if (item.size < 1024) {
  16. printf("%lu\t", item.size);
  17. }
  18. else if ((hsize = item.size/1024.0f) < 1024.0f) {
  19. printf("%.1fK\t", hsize);
  20. }
  21. else if ((hsize /= 1024.0f) < 1024.0f) {
  22. printf("%.1fM\t", hsize);
  23. }
  24. else {
  25. hsize /= 1024.0f;
  26. printf("%.1fG\t", hsize);
  27. }
  28. struct tm* tm = localtime(&item.mtime);
  29. printf("%04d-%02d-%02d %02d:%02d:%02d\t",
  30. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  31. printf("%s%s\n", item.name, item.type == 'd' ? "/" : "");
  32. }
  33. return 0;
  34. }
  35. */
  36. #include <string.h>
  37. #include <time.h>
  38. #include <list>
  39. typedef struct hdir_s {
  40. char name[256];
  41. char type; // f:file d:dir l:link b:block c:char s:socket p:pipe
  42. size_t size;
  43. time_t atime;
  44. time_t mtime;
  45. time_t ctime;
  46. } hdir_t;
  47. // listdir: same as ls on unix, dir on win
  48. int listdir(const char* dir, std::list<hdir_t>& dirs);
  49. #endif // HW_DIR_H_