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.1"
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] == '.');
91 void print_size_sum(size_sum_t s) {
93 char *a = tmp + sizeof(tmp)/sizeof(char);
106 size_sum_t entry_size(const char *name) {
108 struct dirent *dir_e;
111 char subname[PATH_MAX];
115 if(lstat(name, &dummy) != 0) {
116 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
120 if(S_ISLNK(dummy.st_mode)) {
127 while((dir_e = readdir(dir))) {
128 if(!ignore_entry(dir_e->d_name)) {
129 snprintf(subname, PATH_MAX, "%s/%s", name, dir_e->d_name);
130 result += entry_size(subname);
135 if(S_ISREG(dummy.st_mode)) {
136 result += dummy.st_size;
143 size_sum_t atoss(const char *string) {
144 size_sum_t total, partial_total;
149 for(c = string; *c; c++) {
150 if(*c >= '0' && *c <= '9') {
151 partial_total = 10 * partial_total + ((int) (*c - '0'));
152 } else if(*c == 'K' || *c == 'k') {
153 total += partial_total * 1024;
155 } else if(*c == 'M' || *c == 'm') {
156 total += partial_total * 1024 * 1024;
158 } else if(*c == 'G' || *c == 'g') {
159 total += partial_total * 1024 * 1024 * 1024;
162 fprintf(stderr, "Syntax error in %s\n", string);
168 /**********************************************************************/
171 struct entry_node *next;
176 struct entry_node *push_entry(char *name, struct entry_node *head) {
177 struct entry_node *result;
178 result = safe_malloc(sizeof(struct entry_node));
179 result->name = strdup(name);
180 result->size = entry_size(name);
185 void destroy_entry_list(struct entry_node *node) {
186 struct entry_node *next;
195 /**********************************************************************/
197 int compare_files(const void *x1, const void *x2) {
198 const struct entry_node **f1, **f2;
200 f1 = (const struct entry_node **) x1;
201 f2 = (const struct entry_node **) x2;
203 if(reverse_sorting) {
204 if((*f1)->size < (*f2)->size) {
206 } else if((*f1)->size > (*f2)->size) {
212 if((*f1)->size < (*f2)->size) {
214 } else if((*f1)->size > (*f2)->size) {
222 void raw_print(char *buffer, char *filename, size_sum_t size) {
228 *(b++) = size%10 + '0';
245 sprintf(b, " %s\n", filename);
248 void fancy_print(char *buffer, char *filename, size_sum_t size) {
254 } else if(size < 1024 * 1024) {
257 ((double) (size))/(1024.0),
259 } else if(size < 1024 * 1024 * 1024) {
262 ((double) (size))/(1024.0 * 1024),
267 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
272 void print_sorted(struct entry_node *root, int width, int height) {
273 char line[BUFFER_SIZE];
274 struct entry_node *node;
275 struct entry_node **nodes;
276 int nb_nodes, n, first, last;
279 for(node = root; node; node = node->next) {
280 if(size_min < 0 || node->size >= size_min) {
285 nodes = safe_malloc(nb_nodes * sizeof(struct entry_node *));
288 for(node = root; node; node = node->next) {
289 if(size_min < 0 || node->size >= size_min) {
294 qsort(nodes, nb_nodes, sizeof(struct entry_node *), compare_files);
300 height = forced_height;
303 if(height >= 0 && nb_nodes > height && !show_top && !forced_height) {
307 if(height > 0 && height < nb_nodes) {
308 first = nb_nodes - height;
313 last = nb_nodes - first;
314 first = nb_nodes - n;
317 for(n = first; n < last; n++) {
318 if(fancy_size_display) {
319 fancy_print(line, nodes[n]->name, nodes[n]->size);
321 raw_print(line, nodes[n]->name, nodes[n]->size);
323 if(width >= 0 && width < BUFFER_SIZE) {
329 if(height >= 0 && nb_nodes > height && show_top && !forced_height) {
336 /**********************************************************************/
338 void usage(FILE *out) {
339 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
340 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
341 fprintf(out, "List files and directories sorted according to their size or content size. Take the content of the current directory as argument if none is provided.\n");
343 /* 01234567890123456789012345678901234567890123456789012345678901234567890123456789*/
344 fprintf(out, " -d, --ignore-dots ignore files and directories starting with a '.'\n");
345 fprintf(out, " -f, --fancy display size with float values and K, M and G\n");
346 fprintf(out, " units.\n");
347 fprintf(out, " -r, --reverse-order reverse the sorting order.\n");
348 fprintf(out, " -t, --show-top show the top of the list.\n");
349 fprintf(out, " -c <cols>, --nb-columns <cols>\n");
350 fprintf(out, " specificy the number of columns to display. The\n");
351 fprintf(out, " value -1 corresponds to no constraint. By default\n");
352 fprintf(out, " the command uses the tty width, or no constraint\n");
353 fprintf(out, " if the stdout is not a tty.\n");
354 fprintf(out, " -l <lines>, --nb-lines <lines>\n");
355 fprintf(out, " same as -c for number of lines.\n");
356 fprintf(out, " -h, --help show this help.\n");
357 fprintf(out, " -m <size>, --size-min <size>\n");
358 fprintf(out, " set the listed entries minimum size.\n");
360 fprintf(out, "Report bugs and comments to <francois@fleuret.org>.\n");
363 /**********************************************************************/
365 static struct option long_options[] = {
366 { "ignore-dots", no_argument, 0, 'd' },
367 { "reverse-order", no_argument, 0, 'r' },
368 { "show-top", no_argument, 0, 't' },
369 { "help", no_argument, 0, 'h' },
370 { "fancy", no_argument, 0, 'f' },
371 { "nb-columns", 1, 0, 'c' },
372 { "nb-lines", 1, 0, 'l' },
373 { "size-min", 1, 0, 'm' },
377 int main(int argc, char **argv) {
379 struct entry_node *root;
384 setlocale (LC_ALL, "");
386 while ((c = getopt_long(argc, argv, "dfrtl:c:m:hd",
387 long_options, NULL)) != -1) {
395 fancy_size_display = 1;
407 forced_height = atoi(optarg);
411 forced_width = atoi(optarg);
415 size_min = atoss(optarg);
431 while (optind < argc) {
432 root = push_entry(argv[optind++], root);
436 struct dirent *dir_e;
439 while((dir_e = readdir(dir))) {
440 if(!ignore_entry(dir_e->d_name)) {
441 root = push_entry(dir_e->d_name, root);
446 fprintf(stderr, "Can not open ./: %s\n", strerror(errno));
451 if(isatty(STDOUT_FILENO) &&
452 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
453 print_sorted(root, win.ws_col, win.ws_row - 2);
455 print_sorted(root, -1, -1);
458 destroy_entry_list(root);