hdir.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 = item.size / 1024.0f;
  15. if (hsize < 1.0f) {
  16. printf("%lu\t", item.size);
  17. }
  18. else if (hsize > 1024.0f) {
  19. printf("%.02fM\t", hsize/1024.0f);
  20. }
  21. else {
  22. printf("%.02fK\t", hsize);
  23. }
  24. struct tm* tm = localtime(&item.mtime);
  25. printf("%04d-%02d-%02d %02d:%02d:%02d\t%s\n",
  26. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  27. item.name);
  28. }
  29. return 0;
  30. }
  31. */
  32. #include <string.h>
  33. #include <time.h>
  34. #include <list>
  35. typedef struct hdir_s {
  36. char name[256];
  37. char type; // f:file d:dir l:link b:block c:char s:socket p:pipe
  38. size_t size;
  39. time_t atime;
  40. time_t mtime;
  41. time_t ctime;
  42. } hdir_t;
  43. // listdir: same as ls on unix, dir on win
  44. int listdir(const char* dir, std::list<hdir_t>& dirs);
  45. #endif // HW_DIR_H_