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 /********************************************************************/
58 int ignore_entry(const char *name) {
60 strcmp(name, ".") == 0 ||
61 strcmp(name, "..") == 0 ||
62 (ignore_dotfiles && name[0] == '.');
65 void print_size_sum(size_sum_t s) {
67 char *a = tmp + sizeof(tmp)/sizeof(char);
80 size_sum_t file_or_dir_size(const char *name) {
85 char subname[BUFFER_SIZE];
89 if(lstat(name, &dummy) != 0) {
90 printf("Can not stat %s: %s\n", name, strerror(errno));
94 if(S_ISLNK(dummy.st_mode)) {
101 while((dir_e = readdir(dir))) {
102 if(!ignore_entry(dir_e->d_name)) {
103 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
104 result += file_or_dir_size(subname);
109 if(S_ISREG(dummy.st_mode)) {
110 result += dummy.st_size;
117 /**********************************************************************/
119 struct file_with_size {
122 struct file_with_size *next;
125 struct file_with_size *create(char *name, struct file_with_size *current) {
126 struct file_with_size *result;
127 result = malloc(sizeof(struct file_with_size));
128 result->filename = strdup(name);
129 result->size = file_or_dir_size(name);
130 result->next = current;
134 void destroy(struct file_with_size *node) {
135 struct file_with_size *next;
138 free(node->filename);
144 /**********************************************************************/
146 int compare_files(const void *x1, const void *x2) {
147 const struct file_with_size **f1, **f2;
149 f1 = (const struct file_with_size **) x1;
150 f2 = (const struct file_with_size **) x2;
152 if(reverse_sorting) {
153 if((*f1)->size < (*f2)->size) {
155 } else if((*f1)->size > (*f2)->size) {
161 if((*f1)->size < (*f2)->size) {
163 } else if((*f1)->size > (*f2)->size) {
171 void print_sorted(struct file_with_size *root, int height) {
172 struct file_with_size *node;
173 struct file_with_size **nodes;
177 for(node = root; node; node = node->next) {
181 nodes = malloc(nb * sizeof(struct file_with_size *));
184 for(node = root; node; node = node->next) {
188 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
193 height = forced_height;
196 if(height > 0 && height < nb) {
200 if(fancy_size_display) {
201 for(n = first; n < nb; n++) {
202 if(nodes[n]->size < 1024) {
204 ((int) nodes[n]->size),
206 } else if(nodes[n]->size < 1024 * 1024) {
207 printf("% 6.1fK %s\n",
208 ((double) (nodes[n]->size))/(1024.0),
210 } else if(nodes[n]->size < 1024 * 1024 * 1024) {
211 printf("% 6.1fM %s\n",
212 ((double) (nodes[n]->size))/(1024.0 * 1024),
215 printf("% 6.1fG %s\n",
216 ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
221 for(n = first; n < nb; n++) {
222 print_size_sum(nodes[n]->size);
223 printf(" %s\n", nodes[n]->filename);
230 /**********************************************************************/
232 int main(int argc, char **argv) {
234 struct file_with_size *root;
238 setlocale (LC_ALL, "");
241 c = getopt(argc, argv, "dfrl:hdu");
252 fancy_size_display = 1;
260 forced_height = atoi(optarg);
264 printf("Usage: dus [OPTION]... [FILE]...\n");
265 printf("List files and directories sorted according to their size or content size (the content of the current directory by default).\n");
267 printf(" -d ignore files and directories starting with a '.'\n");
268 printf(" -f display size with float values and K, M and G units.\n");
269 printf(" -r reverse the sorting order.\n");
270 printf(" -l <lines> specificy the number of lines to display. The value -1\n");
271 printf(" corresponds to all the lines. By default the command\n");
272 printf(" shows one line less than the height the tty, or all the\n");
273 printf(" lines if the standard output is not a tty.\n");
274 printf(" -h show this help.\n");
276 printf("Report bugs and comments to <francois@fleuret.org>\n");
287 while (optind < argc) {
288 root = create(argv[optind++], root);
292 struct dirent *dir_e;
295 while((dir_e = readdir(dir))) {
296 if(!ignore_entry(dir_e->d_name)) {
297 root = create(dir_e->d_name, root);
304 if(isatty(STDOUT_FILENO)) {
306 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
307 printf("Can not get the tty size: %s\n", strerror(errno));
310 print_sorted(root, win.ws_row - 2);
312 print_sorted(root, -1);