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_height = 0; /* -1 means no height limit, strictly positive
49 means limit, 0 means not active */
51 int fancy_size_display = 0; /* 1 means to use floating values with K, M and G
54 int reverse_sorting = 0; /* 1 means to show the large ones first */
56 int show_top = 0; /* 1 means to show the top of the sorted list
57 instead of the bottom */
59 /********************************************************************/
61 int ignore_entry(const char *name) {
63 strcmp(name, ".") == 0 ||
64 strcmp(name, "..") == 0 ||
65 (ignore_dotfiles && name[0] == '.');
68 void print_size_sum(size_sum_t s) {
70 char *a = tmp + sizeof(tmp)/sizeof(char);
83 size_sum_t file_or_dir_size(const char *name) {
88 char subname[BUFFER_SIZE];
92 if(lstat(name, &dummy) != 0) {
93 printf("Can not stat %s: %s\n", name, strerror(errno));
97 if(S_ISLNK(dummy.st_mode)) {
104 while((dir_e = readdir(dir))) {
105 if(!ignore_entry(dir_e->d_name)) {
106 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
107 result += file_or_dir_size(subname);
112 if(S_ISREG(dummy.st_mode)) {
113 result += dummy.st_size;
120 /**********************************************************************/
122 struct file_with_size {
125 struct file_with_size *next;
128 struct file_with_size *create(char *name, struct file_with_size *current) {
129 struct file_with_size *result;
130 result = malloc(sizeof(struct file_with_size));
131 result->filename = strdup(name);
132 result->size = file_or_dir_size(name);
133 result->next = current;
137 void destroy(struct file_with_size *node) {
138 struct file_with_size *next;
141 free(node->filename);
147 /**********************************************************************/
149 int compare_files(const void *x1, const void *x2) {
150 const struct file_with_size **f1, **f2;
152 f1 = (const struct file_with_size **) x1;
153 f2 = (const struct file_with_size **) x2;
155 if(reverse_sorting) {
156 if((*f1)->size < (*f2)->size) {
158 } else if((*f1)->size > (*f2)->size) {
164 if((*f1)->size < (*f2)->size) {
166 } else if((*f1)->size > (*f2)->size) {
174 void print_sorted(struct file_with_size *root, int height) {
175 struct file_with_size *node;
176 struct file_with_size **nodes;
177 int nb, n, first, last;
180 for(node = root; node; node = node->next) {
184 nodes = malloc(nb * sizeof(struct file_with_size *));
187 for(node = root; node; node = node->next) {
191 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
197 height = forced_height;
200 if(height > 0 && height < nb) {
210 if(fancy_size_display) {
211 for(n = first; n < last; n++) {
212 if(nodes[n]->size < 1024) {
214 ((int) nodes[n]->size),
216 } else if(nodes[n]->size < 1024 * 1024) {
217 printf("% 6.1fK %s\n",
218 ((double) (nodes[n]->size))/(1024.0),
220 } else if(nodes[n]->size < 1024 * 1024 * 1024) {
221 printf("% 6.1fM %s\n",
222 ((double) (nodes[n]->size))/(1024.0 * 1024),
225 printf("% 6.1fG %s\n",
226 ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
231 for(n = first; n < last; n++) {
232 print_size_sum(nodes[n]->size);
233 printf(" %s\n", nodes[n]->filename);
240 /**********************************************************************/
242 int main(int argc, char **argv) {
244 struct file_with_size *root;
248 setlocale (LC_ALL, "");
251 c = getopt(argc, argv, "dfrtl:hdu");
262 fancy_size_display = 1;
274 forced_height = atoi(optarg);
278 printf("Usage: dus [OPTION]... [FILE]...\n");
279 printf("List files and directories sorted according to their size or content size (the content of the current directory by default).\n");
281 printf(" -d ignore files and directories starting with a '.'\n");
282 printf(" -f display size with float values and K, M and G units.\n");
283 printf(" -r reverse the sorting order.\n");
284 printf(" -t show the top of the list.\n");
285 printf(" -l <lines> specificy the number of lines to display. The value -1\n");
286 printf(" corresponds to all the lines. By default the command\n");
287 printf(" shows one line less than the height the tty, or all the\n");
288 printf(" lines if the standard output is not a tty.\n");
289 printf(" -h show this help.\n");
291 printf("Report bugs and comments to <francois@fleuret.org>\n");
302 while (optind < argc) {
303 root = create(argv[optind++], root);
307 struct dirent *dir_e;
310 while((dir_e = readdir(dir))) {
311 if(!ignore_entry(dir_e->d_name)) {
312 root = create(dir_e->d_name, root);
319 if(isatty(STDOUT_FILENO)) {
321 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
322 printf("Can not get the tty size: %s\n", strerror(errno));
325 print_sorted(root, win.ws_row - 2);
327 print_sorted(root, -1);