3 * dus is a simple utility to display the files and directories
4 * according to their total disk occupancy.
6 * Copyright (c) 2010 Francois Fleuret
7 * Written by Francois Fleuret <francois@fleuret.org>
9 * This file is part of dus.
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.
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.
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/>.
25 #define VERSION_NUMBER "1.2"
29 #include <sys/types.h>
31 #include <sys/param.h>
38 #include <sys/ioctl.h>
42 #define BUFFER_SIZE 4096
44 typedef int64_t size_sum_t;
46 /* Yeah, global variables! */
48 int ignore_dotfiles = 0; /* 1 means ignore files and directories
49 starting with a dot */
51 int forced_width = 0; /* -1 means no width limit, strictly positive
52 means limit, 0 means not active */
54 int forced_height = 0; /* -1 means no height limit, strictly positive
55 means limit, 0 means not active */
57 int fancy_size_display = 0; /* 1 means to use floating values with K, M and G
60 int reverse_sorting = 0; /* 1 means to show the large ones first */
62 int show_top = 0; /* 1 means to show the top of the sorted list
63 instead of the bottom */
65 size_sum_t size_min = -1; /* -1 means no minimum size, otherwise lower
66 bound on the size to display a
69 /********************************************************************/
71 /* malloc with error checking. */
73 void *safe_malloc(size_t n) {
77 "dus: Can not allocate memory: %s\n",
84 /********************************************************************/
86 int ignore_entry(const char *name) {
88 strcmp(name, ".") == 0 ||
89 strcmp(name, "..") == 0 ||
90 (ignore_dotfiles && name[0] == '.' && name[1] != '/');
93 size_sum_t entry_size(const char *name) {
98 char subname[PATH_MAX];
102 if(lstat(name, &dummy) != 0) {
104 "dus: Can not stat %s: %s\n",
105 name, strerror(errno));
109 if(S_ISLNK(dummy.st_mode)) {
113 if(S_ISDIR(dummy.st_mode)) {
116 while((dir_e = readdir(dir))) {
117 if(!ignore_entry(dir_e->d_name)) {
118 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
119 result += entry_size(subname);
125 "dus: Can not open directory %s: %s\n",
126 name, strerror(errno));
129 } else if(S_ISREG(dummy.st_mode)) {
130 result += dummy.st_size;
136 size_sum_t atoss(const char *string) {
137 size_sum_t total, partial_total;
142 for(c = string; *c; c++) {
143 if(*c >= '0' && *c <= '9') {
144 partial_total = 10 * partial_total + ((int) (*c - '0'));
145 } else if(*c == 'B' || *c == 'b') {
146 total += partial_total;
148 } else if(*c == 'K' || *c == 'k') {
149 total += partial_total * 1024;
151 } else if(*c == 'M' || *c == 'm') {
152 total += partial_total * 1024 * 1024;
154 } else if(*c == 'G' || *c == 'g') {
155 total += partial_total * 1024 * 1024 * 1024;
159 "dus: Syntax error in size specification `%s'\n",
165 total += partial_total;
170 /**********************************************************************/
173 struct entry_node *next;
178 struct entry_node *push_entry(char *name, struct entry_node *head) {
179 struct entry_node *result;
180 result = safe_malloc(sizeof(struct entry_node));
181 result->name = strdup(name);
182 result->size = entry_size(name);
187 struct entry_node *push_dir_content(char *name, struct entry_node *head) {
188 char subname[PATH_MAX];
190 struct dirent *dir_e;
193 while((dir_e = readdir(dir))) {
194 if(!ignore_entry(dir_e->d_name)) {
195 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
196 head = push_entry(subname, head);
202 "dus: Can not open directory %s: %s\n",
203 name, strerror(errno));
209 void destroy_entry_list(struct entry_node *node) {
210 struct entry_node *next;
219 /**********************************************************************/
221 int compare_files(const void *x1, const void *x2) {
222 const struct entry_node **f1, **f2;
224 f1 = (const struct entry_node **) x1;
225 f2 = (const struct entry_node **) x2;
227 if(reverse_sorting) {
228 if((*f1)->size < (*f2)->size) {
230 } else if((*f1)->size > (*f2)->size) {
236 if((*f1)->size < (*f2)->size) {
238 } else if((*f1)->size > (*f2)->size) {
246 void raw_print(char *buffer, size_t buffer_size,
247 char *filename, size_sum_t size) {
252 if(b >= buffer + buffer_size) {
254 "dus: Buffer overflow in raw_print (hu?!).\n");
257 *(b++) = size%10 + '0';
271 snprintf(b, buffer_size - (b - buffer), "%s\n", filename);
274 void fancy_print(char *buffer, size_t buffer_size,
275 char *filename, size_sum_t size) {
282 } else if(size < 1024 * 1024) {
286 ((double) (size))/(1024.0),
288 } else if(size < 1024 * 1024 * 1024) {
292 ((double) (size))/(1024.0 * 1024),
298 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
303 void print_sorted(struct entry_node *root, int width, int height) {
304 char line[BUFFER_SIZE];
305 struct entry_node *node;
306 struct entry_node **nodes;
307 int nb_nodes, n, first, last;
310 for(node = root; node; node = node->next) {
311 if(size_min < 0 || node->size >= size_min) {
316 nodes = safe_malloc(nb_nodes * sizeof(struct entry_node *));
319 for(node = root; node; node = node->next) {
320 if(size_min < 0 || node->size >= size_min) {
325 qsort(nodes, nb_nodes, sizeof(struct entry_node *), compare_files);
331 height = forced_height;
335 width = forced_width;
338 if(height >= 0 && nb_nodes > height && !show_top && !forced_height) {
342 if(height > 0 && height < nb_nodes) {
343 first = nb_nodes - height;
348 last = nb_nodes - first;
349 first = nb_nodes - n;
352 /* I do not like valgrind to complain about uninitialized data */
353 if(width < BUFFER_SIZE) {
357 for(n = first; n < last; n++) {
358 if(fancy_size_display) {
359 fancy_print(line, BUFFER_SIZE, nodes[n]->name, nodes[n]->size);
361 raw_print(line, BUFFER_SIZE, nodes[n]->name, nodes[n]->size);
363 if(width >= 1 && width + 1 < BUFFER_SIZE && line[width]) {
365 line[width + 1] = '\0';
370 if(height >= 0 && nb_nodes > height && show_top && !forced_height) {
377 /**********************************************************************/
379 void usage(FILE *out) {
380 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
381 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
382 fprintf(out, "Lists files and directories according to their size. The sizes are computed by summing recursively exact file sizes through directories. If a given directory has its name appended with '/', it is not listed, but the elements it contains are. If no files or directories are provided as arguments, the current directory is used as default.\n");
384 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
385 fprintf(out, " -h, --help show this help.\n");
386 fprintf(out, " -v, --version prints the version number and exit\n");
387 fprintf(out, " -d, --ignore-dots ignore files and directories starting with a '.'\n");
388 fprintf(out, " -f, --fancy display size with float values and K, M and G\n");
389 fprintf(out, " units.\n");
390 fprintf(out, " -r, --reverse-order reverse the sorting order.\n");
391 fprintf(out, " -t, --show-top show the top of the list.\n");
392 fprintf(out, " -c <cols>, --nb-columns <cols>\n");
393 fprintf(out, " specificy the number of columns to display. The\n");
394 fprintf(out, " value -1 corresponds to no constraint. By default\n");
395 fprintf(out, " the command uses the tty width, or no constraint\n");
396 fprintf(out, " if the stdout is not a tty.\n");
397 fprintf(out, " -l <lines>, --nb-lines <lines>\n");
398 fprintf(out, " same as -c for number of lines.\n");
399 fprintf(out, " -m <size>, --size-min <size>\n");
400 fprintf(out, " set the listed entries minimum size. The size\n");
401 fprintf(out, " can be specified using the G, M, K, and B units.\n");
403 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
406 /**********************************************************************/
408 static struct option long_options[] = {
409 { "version", no_argument, 0, 'v' },
410 { "ignore-dots", no_argument, 0, 'd' },
411 { "reverse-order", no_argument, 0, 'r' },
412 { "show-top", no_argument, 0, 't' },
413 { "help", no_argument, 0, 'h' },
414 { "fancy", no_argument, 0, 'f' },
415 { "nb-columns", 1, 0, 'c' },
416 { "nb-lines", 1, 0, 'l' },
417 { "size-min", 1, 0, 'm' },
421 int main(int argc, char **argv) {
423 struct entry_node *root;
428 setlocale (LC_ALL, "");
430 while ((c = getopt_long(argc, argv, "vdfrtl:c:m:hd",
431 long_options, NULL)) != -1) {
435 printf("dus version %s (%s)\n", VERSION_NUMBER, UNAME);
444 fancy_size_display = 1;
456 forced_height = atoi(optarg);
460 forced_width = atoi(optarg);
464 size_min = atoss(optarg);
480 while (optind < argc) {
481 l = strlen(argv[optind]);
482 if(l > 0 && argv[optind][l - 1] == '/') {
483 argv[optind][l - 1] = '\0';
484 root = push_dir_content(argv[optind++], root);
486 root = push_entry(argv[optind++], root);
490 root = push_dir_content(".", root);
493 if(isatty(STDOUT_FILENO) &&
494 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
495 print_sorted(root, win.ws_col, win.ws_row - 2);
497 print_sorted(root, -1, -1);
500 destroy_entry_list(root);