From: Eric S. Raymond Date: Fri, 29 Aug 2003 22:24:38 +0000 (+0000) Subject: Sort the output. X-Git-Tag: 1.0~123 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=496f2fdc874f0382b3eecd8bb2dc4e41bb5854be;p=comparator.git Sort the output. --- diff --git a/shredtree.c b/shredtree.c index 0419e77..00290b1 100644 --- a/shredtree.c +++ b/shredtree.c @@ -8,10 +8,19 @@ #include #include "md5.h" -static c_only = 0; -static rws = 0; -static debug = 0; -static shredsize = 5; +static int c_only = 0; +static int rws = 0; +static int debug = 0; +static int shredsize = 5; +static int filecount; + +struct item +{ + char *file; + struct item *next; +} +dummy; +struct item *head = &dummy; static int eligible(const char *file) /* is the specified file eligible to be compared? */ @@ -153,17 +162,30 @@ int treewalker(const char *file, const struct stat *sb, int flag) /* walk the tree, emitting hash sections for eligible files */ { if (flag == FTW_F && sb->st_size > 0 && eligible(file)) - shredfile(file); + { + struct item *new; + + new = (struct item *)malloc(sizeof(new)); + new->file = strdup(file); + new->next = head; + head = new; + filecount++; + } return(0); } +int stringsort(void *a, void *b) +{ + return strcmp(*(char **)a, *(char **)b); +} + main(int argc, char *argv[]) { extern char *optarg; /* set by getopt */ extern int optind; /* set by getopt */ int status; - char *dir = "."; + char **place, **list, *dir = "."; while ((status = getopt(argc, argv, "cdhs:w")) != EOF) { switch (status) @@ -204,5 +226,19 @@ main(int argc, char *argv[]) printf("Normalization: %s\n", rws ? "remove_whitespace" : "none"); puts("%%"); + /* make file list */ ftw(argv[optind], treewalker, 16); + + printf("Filecount: %d\n", filecount); + + /* now that we know the length, copy into an array */ + list = place = (char **)calloc(sizeof(char *), filecount); + for (; head->next; head = head->next) + *place++ = head->file; + + /* the objective -- sort */ + qsort(list, filecount, sizeof(char *), stringsort); + + for (place = list; place < list + filecount; place++) + shredfile(*place); }