Added an embryo of the main routine to print the result.
[dus.git] / dus.c
diff --git a/dus.c b/dus.c
index 4ad1fff..9f05071 100644 (file)
--- 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 <http://www.gnu.org/licenses/>.  */
-/*                                                                       */
-/* Written by and Copyright (C) Francois Fleuret                         */
-/* Contact <francois.fleuret@idiap.ch> 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 <francois@fleuret.org>
+ *
+ *  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 <http://www.gnu.org/licenses/>.
+ *
+ */
 
 #define _BSD_SOURCE
 
 #include <errno.h>
 #include <string.h>
 
-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);
 }