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>
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 file_or_dir_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 += file_or_dir_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 /**********************************************************************/
170 struct file_with_size {
173 struct file_with_size *next;
176 struct file_with_size *create(char *name, struct file_with_size *current) {
177 struct file_with_size *result;
178 result = safe_malloc(sizeof(struct file_with_size));
179 result->filename = strdup(name);
180 result->size = file_or_dir_size(name);
181 result->next = current;
185 void destroy(struct file_with_size *node) {
186 struct file_with_size *next;
189 free(node->filename);
195 /**********************************************************************/
197 int compare_files(const void *x1, const void *x2) {
198 const struct file_with_size **f1, **f2;
200 f1 = (const struct file_with_size **) x1;
201 f2 = (const struct file_with_size **) 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 file_with_size *root, int width, int height) {
273 char line[BUFFER_SIZE];
274 struct file_with_size *node;
275 struct file_with_size **nodes;
276 int nb, n, first, last;
279 for(node = root; node; node = node->next) {
283 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
286 for(node = root; node; node = node->next) {
290 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
296 height = forced_height;
299 if(height > 0 && height < nb) {
309 for(n = first; n < last; n++) {
310 if(size_min < 0 || nodes[n]->size >= size_min) {
311 if(fancy_size_display) {
312 fancy_print(line, nodes[n]->filename, nodes[n]->size);
314 raw_print(line, nodes[n]->filename, nodes[n]->size);
316 if(width >= 0 && width < BUFFER_SIZE) {
325 /**********************************************************************/
326 void print_help(FILE *out) {
327 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
328 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
329 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");
331 fprintf(out, " -d ignore files and directories starting with a '.'\n");
332 fprintf(out, " -f display size with float values and K, M and G units.\n");
333 fprintf(out, " -r reverse the sorting order.\n");
334 fprintf(out, " -t show the top of the list.\n");
335 fprintf(out, " -c <cols> specificy the number of columns to display. The value -1\n");
336 fprintf(out, " corresponds to no constraint. By default the command\n");
337 fprintf(out, " uses the tty width, or no constraint if the stdout is\n");
338 fprintf(out, " not a tty.\n");
339 fprintf(out, " -l <lines> same as -c for number of lines.\n");
340 fprintf(out, " -h show this help.\n");
341 fprintf(out, " -m <size> size min.\n");
343 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
346 /**********************************************************************/
348 int main(int argc, char **argv) {
350 struct file_with_size *root;
355 setlocale (LC_ALL, "");
358 c = getopt(argc, argv, "dfrtl:c:m:hdu");
369 fancy_size_display = 1;
381 forced_height = atoi(optarg);
385 forced_width = atoi(optarg);
389 size_min = atoss(optarg);
405 while (optind < argc) {
406 root = create(argv[optind++], root);
410 struct dirent *dir_e;
413 while((dir_e = readdir(dir))) {
414 if(!ignore_entry(dir_e->d_name)) {
415 root = create(dir_e->d_name, root);
420 fprintf(stderr, "Can not open ./: %s\n", strerror(errno));
425 if(isatty(STDOUT_FILENO) &&
426 !ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
427 print_sorted(root, win.ws_col, win.ws_row - 2);
429 print_sorted(root, -1, -1);