X-Git-Url: https://www.fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=dus.git;a=blobdiff_plain;f=dus.c;h=536d60723d32492b0a03da338265be9850fd75a9;hp=9f050717dc9b2a0866803f1f987815a1528bdf6a;hb=5e8a9db72b7db4795cbbc77e562454919c603993;hpb=9dcd9ccfb923fb815825b354ff51028d206925e1 diff --git a/dus.c b/dus.c index 9f05071..536d607 100644 --- a/dus.c +++ b/dus.c @@ -100,6 +100,22 @@ void destroy(struct file_with_size *node) { /**********************************************************************/ +int compare_files(const void *x1, const void *x2) { + const struct file_with_size **f1, **f2; + + f1 = (const struct file_with_size **) x1; + f2 = (const struct file_with_size **) x2; + + if((*f1)->size < (*f2)->size) { + return -1; + } else if((*f1)->size > (*f2)->size) { + return 1; + } else { + return 0; + } +} + + void print_sorted(struct file_with_size *root) { struct file_with_size *node; struct file_with_size **nodes; @@ -117,6 +133,8 @@ void print_sorted(struct file_with_size *root) { nodes[n++] = node; } + qsort(nodes, nb, sizeof(struct file_with_size *), compare_files); + for(n = 0; n < nb; n++) { printf("%u %s\n", nodes[n]->size, nodes[n]->filename); }