Cosmetic changes in the license header.
[dus.git] / dus.c
1
2 /*
3  *  dus is a simple utility to display the files and directories
4  *  according to their total disk occupancy.
5  *
6  *  Copyright (c) 2010 Francois Fleuret
7  *  Written by Francois Fleuret <francois@fleuret.org>
8  *
9  *  This file is part of dus.
10  *
11  *  dus is free software: you can redistribute it and/or modify it
12  *  under the terms of the GNU General Public License version 3 as
13  *  published by the Free Software Foundation.
14  *
15  *  dus is distributed in the hope that it will be useful, but WITHOUT
16  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  *  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
18  *  License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with dus.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  */
24
25 #define _BSD_SOURCE
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36
37 #define BUFFER_SIZE 1024
38
39 typedef int64_t size_sum_t;
40
41 size_sum_t file_or_dir_size(char *name) {
42   DIR *dir;
43   struct dirent *dir_e;
44   struct stat dummy;
45   size_sum_t result;
46   char subname[BUFFER_SIZE];
47
48   result = 0;
49
50   if(lstat(name, &dummy) != 0) {
51     printf("Can not stat %s: %s\n", name, strerror(errno));
52     exit (1);
53   }
54
55   if(S_ISLNK(dummy.st_mode)) {
56     return 0;
57   }
58
59   dir = opendir(name);
60
61   if(dir) {
62     while((dir_e = readdir(dir))) {
63       if(strcmp(dir_e->d_name, ".") &&
64          strcmp(dir_e->d_name, "..")) {
65         snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
66         result += file_or_dir_size(subname);
67       }
68     }
69     closedir(dir);
70   } else {
71     if(S_ISREG(dummy.st_mode)) {
72       /* printf("%d %s\n", dummy.st_size, name); */
73       result += dummy.st_size;
74     }
75   }
76
77   return result;
78 }
79
80 /**********************************************************************/
81
82 struct file_with_size {
83   char *filename;
84   size_sum_t size;
85   struct file_with_size *next;
86 };
87
88 struct file_with_size *create(char *name, struct file_with_size *current) {
89   struct file_with_size *result;
90   result = malloc(sizeof(struct file_with_size));
91   result->filename = strdup(name);
92   result->size = file_or_dir_size(name);
93   result->next = current;
94   return result;
95 }
96
97 void destroy(struct file_with_size *node) {
98   struct file_with_size *next;
99   while(node) {
100     next = node->next;
101     free(node->filename);
102     free(node);
103     node = next;
104   }
105 }
106
107 /**********************************************************************/
108
109 int compare_files(const void *x1, const void *x2) {
110   const struct file_with_size **f1, **f2;
111
112   f1 = (const struct file_with_size **) x1;
113   f2 = (const struct file_with_size **) x2;
114
115   if((*f1)->size < (*f2)->size) {
116     return -1;
117   } else if((*f1)->size > (*f2)->size) {
118     return 1;
119   } else {
120     return 0;
121   }
122 }
123
124
125 void print_sorted(struct file_with_size *root, int height) {
126   struct file_with_size *node;
127   struct file_with_size **nodes;
128   int nb, n, first;
129
130   nb = 0;
131   for(node = root; node; node = node->next) {
132     nb++;
133   }
134
135   nodes = malloc(nb * sizeof(struct file_with_size *));
136
137   n = 0;
138   for(node = root; node; node = node->next) {
139     nodes[n++] = node;
140   }
141
142   qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
143
144   /*
145   for(n = 0; n < nb; n++) {
146     printf("%lld %s\n",
147            nodes[n]->size,
148            nodes[n]->filename);
149   }
150   */
151
152   first = 0;
153   if(height > 0 && height < nb) {
154     first = nb - height;
155   }
156   for(n = first; n < nb; n++) {
157     if(nodes[n]->size < 1024) {
158       printf("% 7d %s\n",
159              ((int) nodes[n]->size),
160              nodes[n]->filename);
161     } else if(nodes[n]->size < 1024 * 1024) {
162       printf("% 6.1fK %s\n",
163              ((double) (nodes[n]->size))/(1024.0),
164              nodes[n]->filename);
165     } else if(nodes[n]->size < 1024 * 1024 * 1024) {
166       printf("% 6.1fM %s\n",
167              ((double) (nodes[n]->size))/(1024.0 * 1024),
168              nodes[n]->filename);
169     } else {
170       printf("% 6.1fG %s\n",
171              ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
172              nodes[n]->filename);
173     }
174   }
175
176   free(nodes);
177 }
178
179 /**********************************************************************/
180
181 int main(int argc, char **argv) {
182   int k;
183   struct file_with_size *root;
184
185   root = 0;
186
187   if(argc > 1) {
188     for(k = 1; k < argc; k++) {
189       root = create(argv[k], root);
190     }
191   } else {
192     DIR *dir;
193     struct dirent *dir_e;
194     dir = opendir(".");
195     if(dir) {
196       while((dir_e = readdir(dir))) {
197         if(strcmp(dir_e->d_name, ".") &&
198            strcmp(dir_e->d_name, "..")) {
199           root = create(dir_e->d_name, root);
200         }
201       }
202       closedir(dir);
203     }
204   }
205
206   if(isatty(STDOUT_FILENO)) {
207     struct winsize win;
208     if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
209       printf("Can not get the tty size: %s\n", strerror(errno));
210       exit (1);
211     }
212     print_sorted(root, win.ws_row - 2);
213   } else {
214     print_sorted(root, -1);
215   }
216
217   destroy(root);
218
219   exit(0);
220 }