3 * dus is a simple utility to display the files and directories
4 * according to their total disk occupancy.
6 * Copyright (c) 2010, 2011 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.3"
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 int dont_exit_on_protected_files = 0; /* Should we go on when we meet
70 files or directories which
73 /********************************************************************/
75 /* malloc with error checking. */
77 void *safe_malloc(size_t n) {
81 "dus: Can not allocate memory: %s\n",
88 /********************************************************************/
90 int ignore_entry(const char *name) {
92 strcmp(name, ".") == 0 ||
93 strcmp(name, "..") == 0 ||
94 (ignore_dotfiles && name[0] == '.' && name[1] != '/');
97 size_sum_t entry_size(const char *name, int *isdir) {
102 char subname[PATH_MAX];
105 if(isdir) { *isdir = 0; }
107 if(lstat(name, &dummy) != 0) {
109 "dus: Can not stat %s: %s\n",
110 name, strerror(errno));
111 if(!(errno == EACCES && dont_exit_on_protected_files)) {
118 if(S_ISLNK(dummy.st_mode)) {
122 if(S_ISDIR(dummy.st_mode)) {
123 if(isdir) { *isdir = 1; }
126 while((dir_e = readdir(dir))) {
127 if(!ignore_entry(dir_e->d_name)) {
128 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
129 result += entry_size(subname, 0);
135 "dus: Can not open directory %s: %s\n",
136 name, strerror(errno));
137 if(!(errno == EACCES && dont_exit_on_protected_files)) {
141 } else if(S_ISREG(dummy.st_mode)) {
142 result += dummy.st_size;
148 size_sum_t atoss(const char *string) {
149 size_sum_t total, partial_total;
154 for(c = string; *c; c++) {
155 if(*c >= '0' && *c <= '9') {
156 partial_total = 10 * partial_total + ((int) (*c - '0'));
157 } else if(*c == 'B' || *c == 'b') {
158 total += partial_total;
160 } else if(*c == 'K' || *c == 'k') {
161 total += partial_total * 1024;
163 } else if(*c == 'M' || *c == 'm') {
164 total += partial_total * 1024 * 1024;
166 } else if(*c == 'G' || *c == 'g') {
167 total += partial_total * 1024 * 1024 * 1024;
171 "dus: Syntax error in size specification `%s'\n",
177 total += partial_total;
182 /**********************************************************************/
185 struct entry_node *next;
190 struct entry_node *push_entry(char *name, struct entry_node *head) {
191 char tmp_name[PATH_MAX];
192 struct entry_node *result;
194 result = safe_malloc(sizeof(struct entry_node));
195 result->size = entry_size(name, &isdir);
197 snprintf(tmp_name, PATH_MAX, "%s/", name);
198 result->name = strdup(tmp_name);
200 result->name = strdup(name);
206 struct entry_node *push_dir_content(char *name, struct entry_node *head) {
207 char subname[PATH_MAX];
209 struct dirent *dir_e;
212 while((dir_e = readdir(dir))) {
213 if(!ignore_entry(dir_e->d_name)) {
214 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
215 head = push_entry(subname, head);
221 "dus: Can not open directory %s: %s\n",
222 name, strerror(errno));
228 void destroy_entry_list(struct entry_node *node) {
229 struct entry_node *next;
238 /**********************************************************************/
240 int compare_files(const void *x1, const void *x2) {
241 const struct entry_node **f1, **f2;
243 f1 = (const struct entry_node **) x1;
244 f2 = (const struct entry_node **) x2;
246 if(reverse_sorting) {
247 if((*f1)->size < (*f2)->size) {
249 } else if((*f1)->size > (*f2)->size) {
255 if((*f1)->size < (*f2)->size) {
257 } else if((*f1)->size > (*f2)->size) {
265 void raw_print(char *buffer, size_t buffer_size,
266 char *filename, size_sum_t size) {
271 if(b >= buffer + buffer_size) {
273 "dus: Buffer overflow in raw_print (hu?!).\n");
276 *(b++) = size%10 + '0';
290 snprintf(b, buffer_size - (b - buffer), "%s\n", filename);
293 void fancy_print(char *buffer, size_t buffer_size,
294 char *filename, size_sum_t size) {
301 } else if(size < 1024 * 1024) {
305 ((double) (size))/(1024.0),
307 } else if(size < 1024 * 1024 * 1024) {
311 ((double) (size))/(1024.0 * 1024),
317 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
322 void print_sorted(struct entry_node *root, int width, int height) {
323 char line[BUFFER_SIZE];
324 struct entry_node *node;
325 struct entry_node **nodes;
326 int nb_nodes, n, first, last;
329 for(node = root; node; node = node->next) {
330 if(size_min < 0 || node->size >= size_min) {
335 nodes = safe_malloc(nb_nodes * sizeof(struct entry_node *));
338 for(node = root; node; node = node->next) {
339 if(size_min < 0 || node->size >= size_min) {
344 qsort(nodes, nb_nodes, sizeof(struct entry_node *), compare_files);
350 height = forced_height;
354 width = forced_width;
357 if(height >= 0 && nb_nodes > height && !show_top && !forced_height) {
361 if(height > 0 && height < nb_nodes) {
362 first = nb_nodes - height;
367 last = nb_nodes - first;
368 first = nb_nodes - n;
371 /* I do not like valgrind to complain about uninitialized data */
372 if(width < BUFFER_SIZE) {
376 for(n = first; n < last; n++) {
377 if(fancy_size_display) {
378 fancy_print(line, BUFFER_SIZE, nodes[n]->name, nodes[n]->size);
380 raw_print(line, BUFFER_SIZE, nodes[n]->name, nodes[n]->size);
382 if(width >= 1 && width + 1 < BUFFER_SIZE && line[width]) {
384 line[width + 1] = '\0';
389 if(height >= 0 && nb_nodes > height && show_top && !forced_height) {
396 /**********************************************************************/
398 void usage(FILE *out) {
399 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
400 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
401 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");
403 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
404 fprintf(out, " -h, --help show this help.\n");
405 fprintf(out, " -v, --version prints the version number and exit\n");
406 fprintf(out, " -d, --ignore-dots ignore files and directories starting with a '.'\n");
407 fprintf(out, " -i, --ignore-protected do not exit when visiting files and directories\n");
408 fprintf(out, " for which we do not have permission\n");
409 fprintf(out, " -f, --fancy display size with float values and K, M and G\n");
410 fprintf(out, " units.\n");
411 fprintf(out, " -r, --reverse-order reverse the sorting order.\n");
412 fprintf(out, " -t, --show-top show the top of the list.\n");
413 fprintf(out, " -c <cols>, --nb-columns <cols>\n");
414 fprintf(out, " specificy the number of columns to display. The\n");
415 fprintf(out, " value -1 corresponds to no constraint. By default\n");
416 fprintf(out, " the command uses the tty width, or no constraint\n");
417 fprintf(out, " if the stdout is not a tty.\n");
418 fprintf(out, " -l <lines>, --nb-lines <lines>\n");
419 fprintf(out, " same as -c for number of lines.\n");
420 fprintf(out, " -m <size>, --size-min <size>\n");
421 fprintf(out, " set the listed entries minimum size. The size\n");
422 fprintf(out, " can be specified using the G, M, K, and B units.\n");
424 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
427 /**********************************************************************/
429 static struct option long_options[] = {
430 { "version", no_argument, 0, 'v' },
431 { "ignore-dots", no_argument, 0, 'd' },
432 { "ignore-protected", no_argument, 0, 'i' },
433 { "reverse-order", no_argument, 0, 'r' },
434 { "show-top", no_argument, 0, 't' },
435 { "help", no_argument, 0, 'h' },
436 { "fancy", no_argument, 0, 'f' },
437 { "nb-columns", 1, 0, 'c' },
438 { "nb-lines", 1, 0, 'l' },
439 { "size-min", 1, 0, 'm' },
443 int main(int argc, char **argv) {
445 struct entry_node *root;
450 setlocale (LC_ALL, "");
452 while ((c = getopt_long(argc, argv, "ivdfrtl:c:m:hd",
453 long_options, NULL)) != -1) {
457 printf("dus version %s (%s)\n", VERSION_NUMBER, UNAME);
466 dont_exit_on_protected_files = 1;
470 fancy_size_display = 1;
482 forced_height = atoi(optarg);
486 forced_width = atoi(optarg);
490 size_min = atoss(optarg);
506 while (optind < argc) {
507 l = strlen(argv[optind]);
508 if(l > 0 && argv[optind][l - 1] == '/') {
509 argv[optind][l - 1] = '\0';
510 root = push_dir_content(argv[optind++], root);
512 root = push_entry(argv[optind++], root);
516 root = push_dir_content(".", root);
519 if(isatty(STDOUT_FILENO) &&
520 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
521 print_sorted(root, win.ws_col, win.ws_row - 2);
523 print_sorted(root, -1, -1);
526 destroy_entry_list(root);