From: Eric S. Raymond Date: Tue, 2 Sep 2003 10:05:54 +0000 (+0000) Subject: More refactoring. X-Git-Tag: 1.0~65 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=9da392f6e85b61e5585e7dad8b0dae59ee2ea02e;p=comparator.git More refactoring. --- diff --git a/report.c b/report.c index f064eca..963008d 100644 --- a/report.c +++ b/report.c @@ -81,7 +81,7 @@ static void report_time(char *legend, ...) mark_time = endtime; } -void init_intern(const int count) +static void init_intern(const int count) /* prepare an alllocation area of a given size */ { np=obarray=(struct sorthash_t *)calloc(sizeof(struct sorthash_t),count); @@ -91,145 +91,146 @@ void init_intern(const int count) } } - void file_intern(const char *file) - /* stash the name of a file */ - { - struct item *new; +static void file_intern(const char *file) +/* stash the name of a file */ +{ + struct item *new; - new = (struct item *)malloc(sizeof(struct item)); - new->file = strdup(file); - new->next = head; - head = new; - } + new = (struct item *)malloc(sizeof(struct item)); + new->file = strdup(file); + new->next = head; + head = new; +} - void hash_intern(const struct hash_t *chunk) - { +void hash_intern(const struct hash_t *chunk) +/* enter a has into the in-core array */ +{ np->file = head->file; np->hash = *chunk; np++; - } - - static void merge_hashes(int argc, char *argv[]) - /* merge hashes from specified files into an in-code list */ - { - struct shif_t *sp; - struct stat sb; - long total; - - /* set up metadata blocks for the hash files */ - shiflist = (struct shif_t *)calloc(sizeof(struct shif_t), argc); - for (sp = shiflist; sp < shiflist + argc; sp++) - { - char buf[BUFSIZ]; +} - sp->name = strdup(argv[sp - shiflist]); - sp->fp = fopen(sp->name, "r"); - fgets(buf, sizeof(buf), sp->fp); - if (strncmp(buf, "#SHIF-A ", 8)) - { - fprintf(stderr, - "shredcompare: %s is not a SHIF-A file.", - sp->name); - exit(1); - } - while (fgets(buf, sizeof(buf), sp->fp) != NULL) - { - char *value; - - if (!strcmp(buf, "%%\n")) - break; - value = strchr(buf, ':'); - *value++ = '\0'; - while(*value == ' ') - value++; - strchr(value, '\n')[0] = '\0'; - - if (!strcmp(buf, "Normalization")) - sp->normalization = strdup(value); - else if (!strcmp(buf, "Shred-Size")) - sp->shred_size = atoi(value); - else if (!strcmp(buf, "Hash-Method")) - sp->hash_method = strdup(value); - else if (!strcmp(buf, "Generator-Program")) - sp->generator_program = strdup(value); - } - } +static void merge_hashes(int argc, char *argv[]) +/* merge hashes from specified files into an in-code list */ +{ + struct shif_t *sp; + struct stat sb; + long total; - /* consistency checks */ - for (sp = shiflist; sp < shiflist + argc-1; sp++) - { - struct shif_t *next = sp + 1; + /* set up metadata blocks for the hash files */ + shiflist = (struct shif_t *)calloc(sizeof(struct shif_t), argc); + for (sp = shiflist; sp < shiflist + argc; sp++) + { + char buf[BUFSIZ]; - if (strcmp(sp->normalization, next->normalization)) - { - fprintf(stderr, - "shredcompare: normalizations of %s and %s don't match\n", - sp->name, next->name); - exit(1); - } - else if (sp->shred_size != next->shred_size) - { - fprintf(stderr, - "shredcompare: shred sizes of %s and %s don't match\n", - sp->name, next->name); - exit(1); + sp->name = strdup(argv[sp - shiflist]); + sp->fp = fopen(sp->name, "r"); + fgets(buf, sizeof(buf), sp->fp); + if (strncmp(buf, "#SHIF-A ", 8)) + { + fprintf(stderr, + "shredcompare: %s is not a SHIF-A file.", + sp->name); + exit(1); + } + while (fgets(buf, sizeof(buf), sp->fp) != NULL) + { + char *value; + + if (!strcmp(buf, "%%\n")) + break; + value = strchr(buf, ':'); + *value++ = '\0'; + while(*value == ' ') + value++; + strchr(value, '\n')[0] = '\0'; + + if (!strcmp(buf, "Normalization")) + sp->normalization = strdup(value); + else if (!strcmp(buf, "Shred-Size")) + sp->shred_size = atoi(value); + else if (!strcmp(buf, "Hash-Method")) + sp->hash_method = strdup(value); + else if (!strcmp(buf, "Generator-Program")) + sp->generator_program = strdup(value); + } + } - } - else if (strcmp(sp->hash_method, next->hash_method)) - { - fprintf(stderr, - "shredcompare: hash methods of %s and %s don't match\n", - sp->name, next->name); - exit(1); - } - } + /* consistency checks */ + for (sp = shiflist; sp < shiflist + argc-1; sp++) + { + struct shif_t *next = sp + 1; - /* compute total data to be read, we'll use this for the progress meter */ - total = 0; - for (sp = shiflist; sp < shiflist + argc; sp++) - { - stat(sp->name, &sb); - total += sb.st_size - ftell(sp->fp); - } - init_intern(total/sizeof(struct hash_t)); /* real work done here */ + if (strcmp(sp->normalization, next->normalization)) + { + fprintf(stderr, + "shredcompare: normalizations of %s and %s don't match\n", + sp->name, next->name); + exit(1); + } + else if (sp->shred_size != next->shred_size) + { + fprintf(stderr, + "shredcompare: shred sizes of %s and %s don't match\n", + sp->name, next->name); + exit(1); - /* read in all hashes */ - hashcount = 0; - for (sp = shiflist; sp < shiflist + argc; sp++) - { - u_int32_t sectcount; + } + else if (strcmp(sp->hash_method, next->hash_method)) + { + fprintf(stderr, + "shredcompare: hash methods of %s and %s don't match\n", + sp->name, next->name); + exit(1); + } + } - fprintf(stderr, "Reading %s... ", sp->name); - fread(§count, sizeof(u_int32_t), 1, sp->fp); - sectcount = ntohl(sectcount); - while (sectcount--) - { - char buf[BUFSIZ]; - linenum_t chunks; + /* compute total data to be read, we'll use this for the progress meter */ + total = 0; + for (sp = shiflist; sp < shiflist + argc; sp++) + { + stat(sp->name, &sb); + total += sb.st_size - ftell(sp->fp); + } + init_intern(total/sizeof(struct hash_t)); /* real work done here */ - fgets(buf, sizeof(buf), sp->fp); - *strchr(buf, '\n') = '\0'; - file_intern(buf); /* real work done here */ - fread(&chunks, sizeof(linenum_t), 1, sp->fp); - chunks = FROMNET(chunks); + /* read in all hashes */ + hashcount = 0; + for (sp = shiflist; sp < shiflist + argc; sp++) + { + u_int32_t sectcount; - while (chunks--) - { - struct hash_t this; - - fread(&this, sizeof(struct hash_t), 1, sp->fp); - this.start = FROMNET(this.start); - this.end = FROMNET(this.end); - hash_intern(&this); /* real work done here */ - hashcount++; - sp->hashcount++; - if (hashcount % 10000 == 0) - fprintf(stderr, "\b\b\b%02.0f%%", (ftell(sp->fp) / (total * 0.01))); - } - } - fprintf(stderr, "\b\b\b100%%...done, %d entries\n", sp->hashcount); - } - } + fprintf(stderr, "Reading %s... ", sp->name); + fread(§count, sizeof(u_int32_t), 1, sp->fp); + sectcount = ntohl(sectcount); + while (sectcount--) + { + char buf[BUFSIZ]; + linenum_t chunks; + + fgets(buf, sizeof(buf), sp->fp); + *strchr(buf, '\n') = '\0'; + file_intern(buf); /* real work done here */ + fread(&chunks, sizeof(linenum_t), 1, sp->fp); + chunks = FROMNET(chunks); + + while (chunks--) + { + struct hash_t this; + + fread(&this, sizeof(struct hash_t), 1, sp->fp); + this.start = FROMNET(this.start); + this.end = FROMNET(this.end); + hash_intern(&this); /* real work done here */ + hashcount++; + sp->hashcount++; + if (hashcount % 10000 == 0) + fprintf(stderr, "\b\b\b%02.0f%%", (ftell(sp->fp) / (total * 0.01))); + } + } + fprintf(stderr, "\b\b\b100%%...done, %d entries\n", sp->hashcount); + } +} static int merge_ranges(struct range_t *p, struct range_t *q, int nmatches) /* merge t into s, if the ranges in the match are compatible */ @@ -438,7 +439,7 @@ struct match_t *reduce_matches(void) return reduced; } -int sortchunk(void *a, void *b) +static int sortchunk(void *a, void *b) /* sort by hash */ { int cmp = HASHCMP((struct sorthash_t *)a, (struct sorthash_t *)b); @@ -455,7 +456,7 @@ int sortchunk(void *a, void *b) ((struct sorthash_t *)b)->file)); } -int sortmatch(void *a, void *b) +static int sortmatch(void *a, void *b) /* sort by file and first line */ { struct range_t *s = ((struct match_t *)a)->matches; diff --git a/shred.h b/shred.h index 3609783..2746a2f 100644 --- a/shred.h +++ b/shred.h @@ -21,8 +21,4 @@ struct hash_t unsigned char hash[HASHSIZE]; }; -/* hashmap.c */ -void file_intern(const char *); -void hash_intern(const struct hash_t *); - /* shred.h ends here */