From 9dcd9ccfb923fb815825b354ff51028d206925e1 Mon Sep 17 00:00:00 2001 From: Francois Fleuret Date: Wed, 24 Feb 2010 08:01:29 +0100 Subject: [PATCH] Added an embryo of the main routine to print the result. --- dus.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/dus.c b/dus.c index d1774df..9f05071 100644 --- a/dus.c +++ b/dus.c @@ -33,12 +33,6 @@ #include #include -struct file_with_size { - char *filename; - size_t size; - struct file_with_size *next; -}; - size_t file_or_dir_size(char *name) { DIR *dir; struct dirent *dir_e; @@ -77,6 +71,14 @@ size_t file_or_dir_size(char *name) { return result; } +/**********************************************************************/ + +struct file_with_size { + char *filename; + size_t size; + struct file_with_size *next; +}; + struct file_with_size *create(char *name, struct file_with_size *current) { struct file_with_size *result; result = malloc(sizeof(struct file_with_size)); @@ -86,6 +88,44 @@ struct file_with_size *create(char *name, struct file_with_size *current) { return result; } +void destroy(struct file_with_size *node) { + struct file_with_size *next; + while(node) { + next = node->next; + free(node->filename); + free(node); + node = next; + } +} + +/**********************************************************************/ + +void print_sorted(struct file_with_size *root) { + struct file_with_size *node; + struct file_with_size **nodes; + int nb, n; + + nb = 0; + for(node = root; node; node = node->next) { + nb++; + } + + nodes = malloc(nb * sizeof(struct file_with_size *)); + + n = 0; + for(node = root; node; node = node->next) { + nodes[n++] = node; + } + + for(n = 0; n < nb; n++) { + printf("%u %s\n", nodes[n]->size, nodes[n]->filename); + } + + free(nodes); +} + +/**********************************************************************/ + int main(int argc, char **argv) { int k; struct file_with_size *root; @@ -95,12 +135,9 @@ int main(int argc, char **argv) { root = create(argv[k], root); } - while(root) { - printf("%u %s\n", - root->size, - root->filename); - root = root->next; - } + print_sorted(root); + + destroy(root); exit(0); } -- 2.20.1