hdir.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef HV_DIR_H_
  2. #define HV_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%c%c%c%c%c%c%c%c%c\t",
  14. item.type,
  15. item.mode & 0400 ? 'r' : '-',
  16. item.mode & 0200 ? 'w' : '-',
  17. item.mode & 0100 ? 'x' : '-',
  18. item.mode & 0040 ? 'r' : '-',
  19. item.mode & 0020 ? 'w' : '-',
  20. item.mode & 0010 ? 'x' : '-',
  21. item.mode & 0004 ? 'r' : '-',
  22. item.mode & 0002 ? 'w' : '-',
  23. item.mode & 0001 ? 'x' : '-');
  24. float hsize;
  25. if (item.size < 1024) {
  26. printf("%lu\t", item.size);
  27. }
  28. else if ((hsize = item.size/1024.0f) < 1024.0f) {
  29. printf("%.1fK\t", hsize);
  30. }
  31. else if ((hsize /= 1024.0f) < 1024.0f) {
  32. printf("%.1fM\t", hsize);
  33. }
  34. else {
  35. hsize /= 1024.0f;
  36. printf("%.1fG\t", hsize);
  37. }
  38. struct tm* tm = localtime(&item.mtime);
  39. printf("%04d-%02d-%02d %02d:%02d:%02d\t",
  40. tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
  41. printf("%s%s\n", item.name, item.type == 'd' ? "/" : "");
  42. }
  43. return 0;
  44. }
  45. */
  46. #include <string.h>
  47. #include <time.h>
  48. #include <list>
  49. #include "hexport.h"
  50. typedef struct hdir_s {
  51. char name[256];
  52. char type; // f:file d:dir l:link b:block c:char s:socket p:pipe
  53. char reserverd;
  54. unsigned short mode;
  55. size_t size;
  56. time_t atime;
  57. time_t mtime;
  58. time_t ctime;
  59. } hdir_t;
  60. // listdir: same as ls on unix, dir on win
  61. HV_EXPORT int listdir(const char* dir, std::list<hdir_t>& dirs);
  62. #endif // HV_DIR_H_