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) {
76 fprintf(stderr, "Can not allocate memory: %s\n", strerror(errno));
82 /********************************************************************/
84 int ignore_entry(const char *name) {
86 strcmp(name, ".") == 0 ||
87 strcmp(name, "..") == 0 ||
88 (ignore_dotfiles && name[0] == '.' && name[1] != '/');
91 size_sum_t entry_size(const char *name) {
96 char subname[PATH_MAX];
100 if(lstat(name, &dummy) != 0) {
101 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
105 if(S_ISLNK(dummy.st_mode)) {
109 if(S_ISDIR(dummy.st_mode)) {
112 while((dir_e = readdir(dir))) {
113 if(!ignore_entry(dir_e->d_name)) {
114 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
115 result += entry_size(subname);
120 fprintf(stderr, "Can not open directory %s: %s\n", name, strerror(errno));
123 } else if(S_ISREG(dummy.st_mode)) {
124 result += dummy.st_size;
130 size_sum_t atoss(const char *string) {
131 size_sum_t total, partial_total;
136 for(c = string; *c; c++) {
137 if(*c >= '0' && *c <= '9') {
138 partial_total = 10 * partial_total + ((int) (*c - '0'));
139 } else if(*c == 'K' || *c == 'k') {
140 total += partial_total * 1024;
142 } else if(*c == 'M' || *c == 'm') {
143 total += partial_total * 1024 * 1024;
145 } else if(*c == 'G' || *c == 'g') {
146 total += partial_total * 1024 * 1024 * 1024;
149 fprintf(stderr, "Syntax error in %s\n", string);
153 total += partial_total;
158 /**********************************************************************/
161 struct entry_node *next;
166 struct entry_node *push_entry(char *name, struct entry_node *head) {
167 struct entry_node *result;
168 result = safe_malloc(sizeof(struct entry_node));
169 result->name = strdup(name);
170 result->size = entry_size(name);
175 struct entry_node *push_dir_content(char *name, struct entry_node *head) {
176 char subname[PATH_MAX];
178 struct dirent *dir_e;
181 while((dir_e = readdir(dir))) {
182 if(!ignore_entry(dir_e->d_name)) {
183 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
184 head = push_entry(subname, head);
189 fprintf(stderr, "Can not open directory %s: %s\n", name, strerror(errno));
195 void destroy_entry_list(struct entry_node *node) {
196 struct entry_node *next;
205 /**********************************************************************/
207 int compare_files(const void *x1, const void *x2) {
208 const struct entry_node **f1, **f2;
210 f1 = (const struct entry_node **) x1;
211 f2 = (const struct entry_node **) x2;
213 if(reverse_sorting) {
214 if((*f1)->size < (*f2)->size) {
216 } else if((*f1)->size > (*f2)->size) {
222 if((*f1)->size < (*f2)->size) {
224 } else if((*f1)->size > (*f2)->size) {
232 void raw_print(char *buffer, size_t buffer_size,
233 char *filename, size_sum_t size) {
238 if(b >= buffer + buffer_size) {
239 fprintf(stderr, "Buffer overflow in raw_print (hu?!).\n");
242 *(b++) = size%10 + '0';
256 snprintf(b, buffer_size - (b - buffer), "%s\n", filename);
259 void fancy_print(char *buffer, size_t buffer_size,
260 char *filename, size_sum_t size) {
267 } else if(size < 1024 * 1024) {
271 ((double) (size))/(1024.0),
273 } else if(size < 1024 * 1024 * 1024) {
277 ((double) (size))/(1024.0 * 1024),
283 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
288 void print_sorted(struct entry_node *root, int width, int height) {
289 char line[BUFFER_SIZE];
290 struct entry_node *node;
291 struct entry_node **nodes;
292 int nb_nodes, n, first, last;
295 for(node = root; node; node = node->next) {
296 if(size_min < 0 || node->size >= size_min) {
301 nodes = safe_malloc(nb_nodes * sizeof(struct entry_node *));
304 for(node = root; node; node = node->next) {
305 if(size_min < 0 || node->size >= size_min) {
310 qsort(nodes, nb_nodes, sizeof(struct entry_node *), compare_files);
316 height = forced_height;
320 width = forced_width;
323 if(height >= 0 && nb_nodes > height && !show_top && !forced_height) {
327 if(height > 0 && height < nb_nodes) {
328 first = nb_nodes - height;
333 last = nb_nodes - first;
334 first = nb_nodes - n;
337 /* I do not like valgrind to complain about uninitialized data */
338 if(width < BUFFER_SIZE) {
342 for(n = first; n < last; n++) {
343 if(fancy_size_display) {
344 fancy_print(line, BUFFER_SIZE, nodes[n]->name, nodes[n]->size);
346 raw_print(line, BUFFER_SIZE, nodes[n]->name, nodes[n]->size);
348 if(width >= 1 && width + 1 < BUFFER_SIZE && line[width]) {
350 line[width + 1] = '\0';
355 if(height >= 0 && nb_nodes > height && show_top && !forced_height) {
362 /**********************************************************************/
364 void usage(FILE *out) {
365 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
366 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
367 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");
369 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
370 fprintf(out, " -d, --ignore-dots ignore files and directories starting with a '.'\n");
371 fprintf(out, " -f, --fancy display size with float values and K, M and G\n");
372 fprintf(out, " units.\n");
373 fprintf(out, " -r, --reverse-order reverse the sorting order.\n");
374 fprintf(out, " -t, --show-top show the top of the list.\n");
375 fprintf(out, " -c <cols>, --nb-columns <cols>\n");
376 fprintf(out, " specificy the number of columns to display. The\n");
377 fprintf(out, " value -1 corresponds to no constraint. By default\n");
378 fprintf(out, " the command uses the tty width, or no constraint\n");
379 fprintf(out, " if the stdout is not a tty.\n");
380 fprintf(out, " -l <lines>, --nb-lines <lines>\n");
381 fprintf(out, " same as -c for number of lines.\n");
382 fprintf(out, " -h, --help show this help.\n");
383 fprintf(out, " -m <size>, --size-min <size>\n");
384 fprintf(out, " set the listed entries minimum size.\n");
386 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
389 /**********************************************************************/
391 static struct option long_options[] = {
392 { "ignore-dots", no_argument, 0, 'd' },
393 { "reverse-order", no_argument, 0, 'r' },
394 { "show-top", no_argument, 0, 't' },
395 { "help", no_argument, 0, 'h' },
396 { "fancy", no_argument, 0, 'f' },
397 { "nb-columns", 1, 0, 'c' },
398 { "nb-lines", 1, 0, 'l' },
399 { "size-min", 1, 0, 'm' },
403 int main(int argc, char **argv) {
405 struct entry_node *root;
410 setlocale (LC_ALL, "");
412 while ((c = getopt_long(argc, argv, "dfrtl:c:m:hd",
413 long_options, NULL)) != -1) {
421 fancy_size_display = 1;
433 forced_height = atoi(optarg);
437 forced_width = atoi(optarg);
441 size_min = atoss(optarg);
457 while (optind < argc) {
458 l = strlen(argv[optind]);
459 if(l > 0 && argv[optind][l - 1] == '/') {
460 argv[optind][l - 1] = '\0';
461 root = push_dir_content(argv[optind++], root);
463 root = push_entry(argv[optind++], root);
467 root = push_dir_content(".", root);
470 if(isatty(STDOUT_FILENO) &&
471 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
472 print_sorted(root, win.ws_col, win.ws_row - 2);
474 print_sorted(root, -1, -1);
477 destroy_entry_list(root);