This is a maintenance fork
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.7 KiB

  1. #if 0
  2. /*─────────────────────────────────────────────────────────────────╗
  3. To the extent possible under law, Justine Tunney has waived
  4. all copyright and related or neighboring rights to this file,
  5. as it is written in the following disclaimers:
  6. http://unlicense.org/ │
  7. http://creativecommons.org/publicdomain/zero/1.0/ │
  8. */
  9. #endif
  10. #include "libc/alg/alg.h"
  11. #include "libc/alg/arraylist2.internal.h"
  12. #include "libc/bits/safemacros.h"
  13. #include "libc/calls/calls.h"
  14. #include "libc/calls/struct/dirent.h"
  15. #include "libc/calls/struct/stat.h"
  16. #include "libc/calls/struct/timespec.h"
  17. #include "libc/log/check.h"
  18. #include "libc/mem/mem.h"
  19. #include "libc/runtime/gc.h"
  20. #include "libc/stdio/stdio.h"
  21. #include "libc/sysv/consts/dt.h"
  22. #include "libc/x/x.h"
  23. struct stat st;
  24. struct Files {
  25. size_t i, n;
  26. struct File {
  27. struct timespec mt;
  28. char *path;
  29. } * p;
  30. } g_files;
  31. int CompareFiles(struct File *a, struct File *b) {
  32. if (a->mt.tv_sec > b->mt.tv_sec) return 1;
  33. if (a->mt.tv_sec < b->mt.tv_sec) return -1;
  34. if (a->mt.tv_nsec > b->mt.tv_nsec) return 1;
  35. if (a->mt.tv_nsec < b->mt.tv_nsec) return -1;
  36. return strcmp(a->path, b->path);
  37. }
  38. void WalkPaths(const char *dirpath) {
  39. DIR *d;
  40. char *path;
  41. struct File f;
  42. struct dirent *e;
  43. CHECK((d = opendir(dirpath)));
  44. while ((e = readdir(d))) {
  45. if (strcmp(e->d_name, ".") == 0) continue;
  46. if (strcmp(e->d_name, "..") == 0) continue;
  47. path = xjoinpaths(dirpath, e->d_name);
  48. if (strcmp(e->d_name, "o") == 0) continue;
  49. if (strcmp(e->d_name, ".git") == 0) continue;
  50. if (e->d_type == DT_DIR) {
  51. WalkPaths(gc(path));
  52. } else {
  53. CHECK_NE(-1, lstat(path, &st), "%s", path);
  54. f.mt = st.st_mtim;
  55. f.path = path;
  56. APPEND(&g_files.p, &g_files.i, &g_files.n, &f);
  57. }
  58. }
  59. closedir(d);
  60. }
  61. void SortPaths(void) {
  62. qsort(g_files.p, g_files.i, sizeof(*g_files.p), (void *)CompareFiles);
  63. }
  64. void PrintPaths(void) {
  65. long i;
  66. char *ts;
  67. for (i = 0; i < g_files.i; ++i) {
  68. ts = xiso8601(&g_files.p[i].mt);
  69. printf("%s %s\n", ts, g_files.p[i].path);
  70. free(ts);
  71. }
  72. }
  73. void FreePaths(void) {
  74. long i;
  75. for (i = 0; i < g_files.i; ++i) free(g_files.p[i].path);
  76. g_files.i = 0;
  77. }
  78. void LsTime(void) {
  79. WalkPaths(".");
  80. SortPaths();
  81. PrintPaths();
  82. FreePaths();
  83. }
  84. int main(int argc, char *argv[]) {
  85. LsTime();
  86. return 0;
  87. }