diffdir.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include "diff.h"
  5. static int
  6. itemcmp(void *v1, void *v2)
  7. {
  8. char **d1 = v1, **d2 = v2;
  9. return strcmp(*d1, *d2);
  10. }
  11. static char **
  12. scandir(char *name)
  13. {
  14. char **cp;
  15. Dir *db;
  16. int nitems;
  17. int fd, n;
  18. if ((fd = open(name, OREAD)) < 0)
  19. panic(2, "can't open %s\n", name);
  20. cp = 0;
  21. nitems = 0;
  22. if((n = dirreadall(fd, &db)) > 0){
  23. while (n--) {
  24. cp = REALLOC(cp, char *, (nitems+1));
  25. cp[nitems] = MALLOC(char, strlen((db+n)->name)+1);
  26. strcpy(cp[nitems], (db+n)->name);
  27. nitems++;
  28. }
  29. free(db);
  30. }
  31. cp = REALLOC(cp, char*, (nitems+1));
  32. cp[nitems] = 0;
  33. close(fd);
  34. qsort((char *)cp, nitems, sizeof(char*), itemcmp);
  35. return cp;
  36. }
  37. static int
  38. isdotordotdot(char *p)
  39. {
  40. if (*p == '.') {
  41. if (!p[1])
  42. return 1;
  43. if (p[1] == '.' && !p[2])
  44. return 1;
  45. }
  46. return 0;
  47. }
  48. void
  49. diffdir(char *f, char *t, int level)
  50. {
  51. char **df, **dt, **dirf, **dirt;
  52. char *from, *to;
  53. int res;
  54. char fb[MAXPATHLEN+1], tb[MAXPATHLEN+1];
  55. df = scandir(f);
  56. dt = scandir(t);
  57. dirf = df;
  58. dirt = dt;
  59. while (*df || *dt) {
  60. from = *df;
  61. to = *dt;
  62. if (from && isdotordotdot(from)) {
  63. df++;
  64. continue;
  65. }
  66. if (to && isdotordotdot(to)) {
  67. dt++;
  68. continue;
  69. }
  70. if (!from)
  71. res = 1;
  72. else if (!to)
  73. res = -1;
  74. else
  75. res = strcmp(from, to);
  76. if (res < 0) {
  77. if (mode == 0 || mode == 'n')
  78. Bprint(&stdout, "Only in %s: %s\n", f, from);
  79. df++;
  80. continue;
  81. }
  82. if (res > 0) {
  83. if (mode == 0 || mode == 'n')
  84. Bprint(&stdout, "Only in %s: %s\n", t, to);
  85. dt++;
  86. continue;
  87. }
  88. if (mkpathname(fb, f, from))
  89. continue;
  90. if (mkpathname(tb, t, to))
  91. continue;
  92. diff(fb, tb, level+1);
  93. df++; dt++;
  94. }
  95. for (df = dirf; *df; df++)
  96. FREE(*df);
  97. for (dt = dirt; *dt; dt++)
  98. FREE(*dt);
  99. FREE(dirf);
  100. FREE(dirt);
  101. }