X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=dus.c;h=9f050717dc9b2a0866803f1f987815a1528bdf6a;hb=9dcd9ccfb923fb815825b354ff51028d206925e1;hp=4ad1fff1b03ffe3fa546812fc6a4cb9c42f336f0;hpb=d04ae65cca82b350617e81778e4e6d3b9f221b0f;p=dus.git diff --git a/dus.c b/dus.c index 4ad1fff..9f05071 100644 --- a/dus.c +++ b/dus.c @@ -1,24 +1,26 @@ -/*************************************************************************/ -/* START_IP_HEADER */ -/* */ -/* This program is free software: you can redistribute it and/or modify */ -/* it under the terms of the version 3 of the GNU General Public License */ -/* as published by the Free Software Foundation. */ -/* */ -/* This program is distributed in the hope that it will be useful, but */ -/* WITHOUT ANY WARRANTY; without even the implied warranty of */ -/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ -/* General Public License for more details. */ -/* */ -/* You should have received a copy of the GNU General Public License */ -/* along with this program. If not, see . */ -/* */ -/* Written by and Copyright (C) Francois Fleuret */ -/* Contact for comments & bug reports */ -/* */ -/* END_IP_HEADER */ -/*************************************************************************/ +/* + * dus is a simple utility designed to display the list of files and + * directories with disk occupancy, sorted according to it. + * + * Copyright (c) 2009 Francois Fleuret + * Written by Francois Fleuret + * + * This file is part of dus. + * + * dus is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * dus is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + * License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dus. If not, see . + * + */ #define _BSD_SOURCE @@ -31,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; @@ -75,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)); @@ -84,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; @@ -93,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); }