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.1alpha"
29 #include <sys/types.h>
37 #include <sys/ioctl.h>
41 #define BUFFER_SIZE 4096
43 typedef int64_t size_sum_t;
45 /* Yeah, global variables! */
47 int ignore_dotfiles = 0; /* 1 means ignore files and directories
48 starting with a dot */
50 int forced_width = 0; /* -1 means no width limit, strictly positive
51 means limit, 0 means not active */
53 int forced_height = 0; /* -1 means no height limit, strictly positive
54 means limit, 0 means not active */
56 int fancy_size_display = 0; /* 1 means to use floating values with K, M and G
59 int reverse_sorting = 0; /* 1 means to show the large ones first */
61 int show_top = 0; /* 1 means to show the top of the sorted list
62 instead of the bottom */
64 size_sum_t size_min = -1; /* -1 means no minimum size, otherwise lower
65 bound on the size to display a
68 /********************************************************************/
70 /* malloc with error checking. */
72 void *safe_malloc(size_t n) {
75 fprintf(stderr, "Can not allocate memory: %s\n", strerror(errno));
81 /********************************************************************/
83 int ignore_entry(const char *name) {
85 strcmp(name, ".") == 0 ||
86 strcmp(name, "..") == 0 ||
87 (ignore_dotfiles && name[0] == '.');
90 void print_size_sum(size_sum_t s) {
92 char *a = tmp + sizeof(tmp)/sizeof(char);
105 size_sum_t file_or_dir_size(const char *name) {
107 struct dirent *dir_e;
110 char subname[BUFFER_SIZE];
114 if(lstat(name, &dummy) != 0) {
115 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
119 if(S_ISLNK(dummy.st_mode)) {
126 while((dir_e = readdir(dir))) {
127 if(!ignore_entry(dir_e->d_name)) {
128 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
129 result += file_or_dir_size(subname);
134 if(S_ISREG(dummy.st_mode)) {
135 result += dummy.st_size;
142 size_sum_t atoss(const char *string) {
143 size_sum_t total, partial_total;
148 for(c = string; *c; c++) {
149 if(*c >= '0' && *c <= '9') {
150 partial_total = 10 * partial_total + ((int) (*c - '0'));
151 } else if(*c == 'K' || *c == 'k') {
152 total += partial_total * 1024;
154 } else if(*c == 'M' || *c == 'm') {
155 total += partial_total * 1024 * 1024;
157 } else if(*c == 'G' || *c == 'g') {
158 total += partial_total * 1024 * 1024 * 1024;
161 fprintf(stderr, "Syntax error in %s\n", string);
167 /**********************************************************************/
169 struct file_with_size {
172 struct file_with_size *next;
175 struct file_with_size *create(char *name, struct file_with_size *current) {
176 struct file_with_size *result;
177 result = safe_malloc(sizeof(struct file_with_size));
178 result->filename = strdup(name);
179 result->size = file_or_dir_size(name);
180 result->next = current;
184 void destroy(struct file_with_size *node) {
185 struct file_with_size *next;
188 free(node->filename);
194 /**********************************************************************/
196 int compare_files(const void *x1, const void *x2) {
197 const struct file_with_size **f1, **f2;
199 f1 = (const struct file_with_size **) x1;
200 f2 = (const struct file_with_size **) x2;
202 if(reverse_sorting) {
203 if((*f1)->size < (*f2)->size) {
205 } else if((*f1)->size > (*f2)->size) {
211 if((*f1)->size < (*f2)->size) {
213 } else if((*f1)->size > (*f2)->size) {
221 void raw_print(char *buffer, char *filename, size_sum_t size) {
227 *(b++) = size%10 + '0';
244 sprintf(b, " %s\n", filename);
247 void fancy_print(char *buffer, char *filename, size_sum_t size) {
253 } else if(size < 1024 * 1024) {
256 ((double) (size))/(1024.0),
258 } else if(size < 1024 * 1024 * 1024) {
261 ((double) (size))/(1024.0 * 1024),
266 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
271 void print_sorted(struct file_with_size *root, int width, int height) {
272 char line[BUFFER_SIZE];
273 struct file_with_size *node;
274 struct file_with_size **nodes;
275 int nb, n, first, last;
278 for(node = root; node; node = node->next) {
282 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
285 for(node = root; node; node = node->next) {
289 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
295 height = forced_height;
298 if(height > 0 && height < nb) {
308 for(n = first; n < last; n++) {
309 if(size_min < 0 || nodes[n]->size >= size_min) {
310 if(fancy_size_display) {
311 fancy_print(line, nodes[n]->filename, nodes[n]->size);
313 raw_print(line, nodes[n]->filename, nodes[n]->size);
315 if(width >= 0 && width < BUFFER_SIZE) {
324 /**********************************************************************/
325 void print_help(FILE *out) {
326 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
327 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
328 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");
330 fprintf(out, " -d ignore files and directories starting with a '.'\n");
331 fprintf(out, " -f display size with float values and K, M and G units.\n");
332 fprintf(out, " -r reverse the sorting order.\n");
333 fprintf(out, " -t show the top of the list.\n");
334 fprintf(out, " -c <cols> specificy the number of columns to display. The value -1\n");
335 fprintf(out, " corresponds to no constraint. By default the command\n");
336 fprintf(out, " uses the tty width, or no constraint if the stdout is\n");
337 fprintf(out, " not a tty.\n");
338 fprintf(out, " -l <lines> same as -c for number of lines.\n");
339 fprintf(out, " -h show this help.\n");
340 fprintf(out, " -m <size> size min.\n");
342 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
345 /**********************************************************************/
347 int main(int argc, char **argv) {
349 struct file_with_size *root;
354 setlocale (LC_ALL, "");
357 c = getopt(argc, argv, "dfrtl:c:m:hdu");
368 fancy_size_display = 1;
380 forced_height = atoi(optarg);
384 forced_width = atoi(optarg);
388 size_min = atoss(optarg);
404 while (optind < argc) {
405 root = create(argv[optind++], root);
409 struct dirent *dir_e;
412 while((dir_e = readdir(dir))) {
413 if(!ignore_entry(dir_e->d_name)) {
414 root = create(dir_e->d_name, root);
419 fprintf(stderr, "Can not open ./: %s\n", strerror(errno));
424 if(isatty(STDOUT_FILENO) &&
425 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
426 print_sorted(root, win.ws_col, win.ws_row - 2);
428 print_sorted(root, -1, -1);