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.0alpha"
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 /********************************************************************/
66 /* malloc with error checking. */
68 void *safe_malloc(size_t n) {
71 fprintf(stderr, "Can not allocate memory: %s\n", strerror(errno));
77 /********************************************************************/
79 int ignore_entry(const char *name) {
81 strcmp(name, ".") == 0 ||
82 strcmp(name, "..") == 0 ||
83 (ignore_dotfiles && name[0] == '.');
86 void print_size_sum(size_sum_t s) {
88 char *a = tmp + sizeof(tmp)/sizeof(char);
101 size_sum_t file_or_dir_size(const char *name) {
103 struct dirent *dir_e;
106 char subname[BUFFER_SIZE];
110 if(lstat(name, &dummy) != 0) {
111 fprintf(stderr, "Can not stat %s: %s\n", name, strerror(errno));
115 if(S_ISLNK(dummy.st_mode)) {
122 while((dir_e = readdir(dir))) {
123 if(!ignore_entry(dir_e->d_name)) {
124 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
125 result += file_or_dir_size(subname);
130 if(S_ISREG(dummy.st_mode)) {
131 result += dummy.st_size;
138 /**********************************************************************/
140 struct file_with_size {
143 struct file_with_size *next;
146 struct file_with_size *create(char *name, struct file_with_size *current) {
147 struct file_with_size *result;
148 result = safe_malloc(sizeof(struct file_with_size));
149 result->filename = strdup(name);
150 result->size = file_or_dir_size(name);
151 result->next = current;
155 void destroy(struct file_with_size *node) {
156 struct file_with_size *next;
159 free(node->filename);
165 /**********************************************************************/
167 int compare_files(const void *x1, const void *x2) {
168 const struct file_with_size **f1, **f2;
170 f1 = (const struct file_with_size **) x1;
171 f2 = (const struct file_with_size **) x2;
173 if(reverse_sorting) {
174 if((*f1)->size < (*f2)->size) {
176 } else if((*f1)->size > (*f2)->size) {
182 if((*f1)->size < (*f2)->size) {
184 } else if((*f1)->size > (*f2)->size) {
192 void raw_print(char *buffer, char *filename, size_sum_t size) {
198 *(b++) = size%10 + '0';
215 sprintf(b, " %s\n", filename);
218 void fancy_print(char *buffer, char *filename, size_sum_t size) {
224 } else if(size < 1024 * 1024) {
227 ((double) (size))/(1024.0),
229 } else if(size < 1024 * 1024 * 1024) {
232 ((double) (size))/(1024.0 * 1024),
237 ((double) (size))/(1024.0 * 1024.0 * 1024.0),
242 void print_sorted(struct file_with_size *root, int width, int height) {
243 char line[BUFFER_SIZE];
244 struct file_with_size *node;
245 struct file_with_size **nodes;
246 int nb, n, first, last;
249 for(node = root; node; node = node->next) {
253 nodes = safe_malloc(nb * sizeof(struct file_with_size *));
256 for(node = root; node; node = node->next) {
260 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
266 height = forced_height;
269 if(height > 0 && height < nb) {
279 for(n = first; n < last; n++) {
280 if(fancy_size_display) {
281 fancy_print(line, nodes[n]->filename, nodes[n]->size);
283 raw_print(line, nodes[n]->filename, nodes[n]->size);
285 if(width >= 0 && width < BUFFER_SIZE) {
293 /**********************************************************************/
294 void print_help(FILE *out) {
295 fprintf(out, "Usage: dus [OPTION]... [FILE]...\n");
296 fprintf(out, "Version %s (%s)\n", VERSION_NUMBER, UNAME);
297 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");
299 fprintf(out, " -d ignore files and directories starting with a '.'\n");
300 fprintf(out, " -f display size with float values and K, M and G units.\n");
301 fprintf(out, " -r reverse the sorting order.\n");
302 fprintf(out, " -t show the top of the list.\n");
303 fprintf(out, " -c <cols> specificy the number of columns to display. The value -1\n");
304 fprintf(out, " corresponds to no constraint. By default the command\n");
305 fprintf(out, " uses the tty width, or no constraint if the stdout is\n");
306 fprintf(out, " not a tty.\n");
307 fprintf(out, " -l <lines> same as -c for number of lines.\n");
308 fprintf(out, " -h show this help.\n");
310 fprintf(out, "Report bugs and comments to <francois@fleuret.org>\n");
313 /**********************************************************************/
315 int main(int argc, char **argv) {
317 struct file_with_size *root;
321 setlocale (LC_ALL, "");
324 c = getopt(argc, argv, "dfrtl:c:hdu");
335 fancy_size_display = 1;
347 forced_height = atoi(optarg);
351 forced_width = atoi(optarg);
367 while (optind < argc) {
368 root = create(argv[optind++], root);
372 struct dirent *dir_e;
375 while((dir_e = readdir(dir))) {
376 if(!ignore_entry(dir_e->d_name)) {
377 root = create(dir_e->d_name, root);
382 fprintf(stderr, "Can not open ./: %s\n", strerror(errno));
387 if(isatty(STDOUT_FILENO)) {
389 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
390 fprintf(stderr, "Can not get the tty size: %s\n", strerror(errno));
393 print_sorted(root, win.ws_col, win.ws_row - 2);
395 print_sorted(root, -1, -1);