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/>.
27 #include <sys/types.h>
35 #include <sys/ioctl.h>
39 #define BUFFER_SIZE 4096
41 typedef int64_t size_sum_t;
43 /* Yeah, global variables! */
45 int ignore_dotfiles = 0; /* 1 means ignore files and directories
46 starting with a dot */
48 int forced_width = 0; /* -1 means no width limit, strictly positive
49 means limit, 0 means not active */
51 int forced_height = 0; /* -1 means no height limit, strictly positive
52 means limit, 0 means not active */
54 int fancy_size_display = 0; /* 1 means to use floating values with K, M and G
57 int reverse_sorting = 0; /* 1 means to show the large ones first */
59 int show_top = 0; /* 1 means to show the top of the sorted list
60 instead of the bottom */
62 /********************************************************************/
64 /* malloc with error checking. */
66 void *safe_malloc(size_t n) {
69 fprintf(stderr, "Can not allocate memory: %s\n", strerror(errno));
75 /********************************************************************/
77 int ignore_entry(const char *name) {
79 strcmp(name, ".") == 0 ||
80 strcmp(name, "..") == 0 ||
81 (ignore_dotfiles && name[0] == '.');
84 void print_size_sum(size_sum_t s) {
86 char *a = tmp + sizeof(tmp)/sizeof(char);
99 size_sum_t file_or_dir_size(const char *name) {
101 struct dirent *dir_e;
104 char subname[BUFFER_SIZE];
108 if(lstat(name, &dummy) != 0) {
109 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
113 if(S_ISLNK(dummy.st_mode)) {
120 while((dir_e = readdir(dir))) {
121 if(!ignore_entry(dir_e->d_name)) {
122 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
123 result += file_or_dir_size(subname);
128 if(S_ISREG(dummy.st_mode)) {
129 result += dummy.st_size;
136 /**********************************************************************/
138 struct file_with_size {
141 struct file_with_size *next;
144 struct file_with_size *create(char *name, struct file_with_size *current) {
145 struct file_with_size *result;
146 result = safe_malloc(sizeof(struct file_with_size));
147 result->filename = strdup(name);
148 result->size = file_or_dir_size(name);
149 result->next = current;
153 void destroy(struct file_with_size *node) {
154 struct file_with_size *next;
157 free(node->filename);
163 /**********************************************************************/
165 int compare_files(const void *x1, const void *x2) {
166 const struct file_with_size **f1, **f2;
168 f1 = (const struct file_with_size **) x1;
169 f2 = (const struct file_with_size **) x2;
171 if(reverse_sorting) {
172 if((*f1)->size < (*f2)->size) {
174 } else if((*f1)->size > (*f2)->size) {
180 if((*f1)->size < (*f2)->size) {
182 } else if((*f1)->size > (*f2)->size) {
190 void raw_print(char *buffer, char *filename, size_sum_t size) {
196 *(b++) = size%10 + '0';
213 sprintf(b, " %s\n", filename);
216 void fancy_print(char *buffer, char *filename, size_sum_t size) {
222 } else if(size < 1024 * 1024) {
225 ((double) (size))/(1024.0),
227 } else if(size < 1024 * 1024 * 1024) {
230 ((double) (size))/(1024.0 * 1024),
235 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
240 void print_sorted(struct file_with_size *root, int width, int height) {
241 char line[BUFFER_SIZE];
242 struct file_with_size *node;
243 struct file_with_size **nodes;
244 int nb, n, first, last;
247 for(node = root; node; node = node->next) {
251 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
254 for(node = root; node; node = node->next) {
258 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
264 height = forced_height;
267 if(height > 0 && height < nb) {
277 for(n = first; n < last; n++) {
278 if(fancy_size_display) {
279 fancy_print(line, nodes[n]->filename, nodes[n]->size);
281 raw_print(line, nodes[n]->filename, nodes[n]->size);
283 if(width >= 0 && width < BUFFER_SIZE) {
292 /**********************************************************************/
294 int main(int argc, char **argv) {
296 struct file_with_size *root;
300 setlocale (LC_ALL, "");
303 c = getopt(argc, argv, "dfrtl:c:hdu");
314 fancy_size_display = 1;
326 forced_height = atoi(optarg);
330 forced_width = atoi(optarg);
334 printf("Usage: dus [OPTION]... [FILE]...\n");
335 printf("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");
337 printf(" -d ignore files and directories starting with a '.'\n");
338 printf(" -f display size with float values and K, M and G units.\n");
339 printf(" -r reverse the sorting order.\n");
340 printf(" -t show the top of the list.\n");
341 printf(" -c <cols> specificy the number of columns to display. The value -1\n");
342 printf(" corresponds to no constraint. By default the command\n");
343 printf(" uses the tty width, or no constraint if the stdout is\n");
344 printf(" not a tty.\n");
345 printf(" -l <lines> same as -c for number of lines.\n");
346 printf(" -h show this help.\n");
348 printf("Report bugs and comments to <francois@fleuret.org>\n");
359 while (optind < argc) {
360 root = create(argv[optind++], root);
364 struct dirent *dir_e;
367 while((dir_e = readdir(dir))) {
368 if(!ignore_entry(dir_e->d_name)) {
369 root = create(dir_e->d_name, root);
374 fprintf(stderr, "Can not open ./: %s\n", strerror(errno));
379 if(isatty(STDOUT_FILENO)) {
381 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
382 fprintf(stderr, "Can not get the tty size: %s\n", strerror(errno));
385 print_sorted(root, win.ws_col, win.ws_row - 2);
387 print_sorted(root, -1, -1);