--- embedtools/src/dircmp.c 2010/07/13 11:49:49 1.1.2.4 +++ embedtools/src/dircmp.c 2010/07/13 12:38:23 1.1.2.5 @@ -59,6 +59,27 @@ func_comp(struct tagDirName const *d1, struct tagDirNa return d1->tag - d2->tag; } +static struct tagDirName * +find_tag(int const * __restrict tags, struct tagDirName const * __restrict l, u_short t, u_int hash) +{ + struct tagDirName *find = NULL; + register int i; + + // search in index tags + if (tags[t] != -1 && l[tags[t]].tag == t) { + // search in sorted hashes + for (i = 0; l[tags[t] + i].tag == t; i++) + if (l[tags[t] + i].hash == hash) { + // finded & marked for delete! + find = (struct tagDirName*) &l[tags[t] + i]; + find->ch = '*'; + break; + } + } + + return find; +} + int cmpDir(const char *csDir1, const char *csDir2, struct tagDirName **list) { @@ -71,7 +92,8 @@ cmpDir(const char *csDir1, const char *csDir2, struct u_short t; u_int hash; struct stat sb; - char szStr[MAX_STR], szType[MAX_STR]; + char szLine[MAX_STR], szStr[MAX_STR], szType[MAX_STR], *str, *pbrk; + FILE *f = stdin; if (!csDir1 || !list) return -1; @@ -164,6 +186,58 @@ cmpDir(const char *csDir1, const char *csDir2, struct // if only 1 dir supplied and output filename is set, goto end if (lm & 2 && !csDir2) goto end; + // + if (lm & 4) { + if (strcmp(csDir2, "-")) { + f = fopen(csDir2, "r"); + if (!f) { + printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno)); + free(l); + return -1; + } + } + while (fgets(szLine, MAX_STR, f)) { + if (!*szLine || *szLine == '#') + continue; + + str = strtok_r(szLine, " \t", &pbrk); + if (!str) + continue; + str = strtok_r(NULL, " \t", &pbrk); + if (!str) + continue; + else { + i = strlen(str); + t = crcFletcher16((u_short*) str, i / 2 + i % 2); + hash = crcAdler((u_char*) str, i); + } + + find = find_tag(tags, l, t, hash); + // element not find in dir1, added + if (!find) { + l = realloc(l, sizeof(struct tagDirName) * (n + 2)); + if (!l) { + printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno)); + closedir(dir); + return -1; + } else + memset(&l[n + 1], 0, sizeof(struct tagDirName)); + + l[n].ch = '>'; + l[n].tag = t; + l[n].hash = hash; + strlcpy(l[n].name, str, MAXPATHLEN); + if (lm & 1 && (str = strtok_r(NULL, "\r\n", &pbrk))) + strlcpy(l[n].extra, str, MAX_STR); + + n++; + } + } + if (strcmp(csDir2, "-")) + fclose(f); + + goto delel; + } //// // open dir 2 for diff ... @@ -186,18 +260,7 @@ cmpDir(const char *csDir1, const char *csDir2, struct hash = crcAdler((u_char*) d.d_name, d.d_namlen); } - // search in index tags - find = NULL; - if (tags[t] != -1 && l[tags[t]].tag == t) { - // search in sorted hashes - for (i = 0; l[tags[t] + i].tag == t; i++) - if (l[tags[t] + i].hash == hash) { - // finded & marked for delete! - find = &l[tags[t] + i]; - find->ch = '*'; - break; - } - } + find = find_tag(tags, l, t, hash); // element not find in dir1, added if (!find) { l = realloc(l, sizeof(struct tagDirName) * (n + 2)); @@ -256,7 +319,7 @@ cmpDir(const char *csDir1, const char *csDir2, struct } } closedir(dir); - +delel: // delete equal elemets !!! for (i = cx = 0; i < n; i++) if (l[i].ch == '*') { @@ -301,7 +364,7 @@ main(int argc, char **argv) argc -= optind; argv += optind; - if (argc < 1 || (lm < 2 && argc < 2)) { + if (!argc || (!(lm & 2) && argc < 2)) { Usage(); return 127; } @@ -327,10 +390,10 @@ main(int argc, char **argv) } } - if (lm < 4 && cmpDir(argv[0], argc > 1 ? argv[1] : NULL, &list) == -1) + if (cmpDir(argv[0], argc > 1 ? argv[1] : NULL, &list) == -1) return 127; - if (lm & 2) { + if (lm & 2 && strcmp(szFName, "-")) { f = fopen(szFName, "w"); if (!f) { printf("Error:: %s(%d) #%d - %s\n", __func__, __LINE__, errno, strerror(errno));