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 /********************************************************************/
56 int ignore_entry(const char *name) {
58 strcmp(name, ".") == 0 ||
59 strcmp(name, "..") == 0 ||
60 (ignore_dotfiles && name[0] == '.');
63 void print_size_sum(size_sum_t s) {
65 char *a = tmp + sizeof(tmp)/sizeof(char);
78 size_sum_t file_or_dir_size(const char *name) {
83 char subname[BUFFER_SIZE];
87 if(lstat(name, &dummy) != 0) {
88 printf("Can not stat %s: %s\n", name, strerror(errno));
92 if(S_ISLNK(dummy.st_mode)) {
99 while((dir_e = readdir(dir))) {
100 if(!ignore_entry(dir_e->d_name)) {
101 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
102 result += file_or_dir_size(subname);
107 if(S_ISREG(dummy.st_mode)) {
108 result += dummy.st_size;
115 /**********************************************************************/
117 struct file_with_size {
120 struct file_with_size *next;
123 struct file_with_size *create(char *name, struct file_with_size *current) {
124 struct file_with_size *result;
125 result = malloc(sizeof(struct file_with_size));
126 result->filename = strdup(name);
127 result->size = file_or_dir_size(name);
128 result->next = current;
132 void destroy(struct file_with_size *node) {
133 struct file_with_size *next;
136 free(node->filename);
142 /**********************************************************************/
144 int compare_files(const void *x1, const void *x2) {
145 const struct file_with_size **f1, **f2;
147 f1 = (const struct file_with_size **) x1;
148 f2 = (const struct file_with_size **) x2;
150 if((*f1)->size < (*f2)->size) {
152 } else if((*f1)->size > (*f2)->size) {
159 void print_sorted(struct file_with_size *root, int height) {
160 struct file_with_size *node;
161 struct file_with_size **nodes;
165 for(node = root; node; node = node->next) {
169 nodes = malloc(nb * sizeof(struct file_with_size *));
172 for(node = root; node; node = node->next) {
176 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
181 height = forced_height;
184 if(height > 0 && height < nb) {
188 if(fancy_size_display) {
189 for(n = first; n < nb; n++) {
190 if(nodes[n]->size < 1024) {
192 ((int) nodes[n]->size),
194 } else if(nodes[n]->size < 1024 * 1024) {
195 printf("% 6.1fK %s\n",
196 ((double) (nodes[n]->size))/(1024.0),
198 } else if(nodes[n]->size < 1024 * 1024 * 1024) {
199 printf("% 6.1fM %s\n",
200 ((double) (nodes[n]->size))/(1024.0 * 1024),
203 printf("% 6.1fG %s\n",
204 ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
209 for(n = first; n < nb; n++) {
210 print_size_sum(nodes[n]->size);
211 printf(" %s\n", nodes[n]->filename);
218 /**********************************************************************/
220 int main(int argc, char **argv) {
222 struct file_with_size *root;
226 setlocale (LC_ALL, "");
229 c = getopt(argc, argv, "dfl:hdu");
240 fancy_size_display = 1;
244 forced_height = atoi(optarg);
248 printf("Usage: dus [OPTION]... [FILE]...\n");
249 printf("List files and directories sorted according to their size or content \n");
252 printf(" -d do not take into account files starting with a '.'\n");
253 printf(" -f display size with float values and K, M and G units.\n");
254 printf(" -l <lines> specificy the number of lines to display. The value -1\n");
255 printf(" corresponds to all the lines. By default the command\n");
256 printf(" shows one line less than the number of lines of the TTY,\n");
257 printf(" or all the lines if standard output is not a tty\n");
258 printf(" -h show this help.\n");
260 printf("Report bugs and comments to <francois@fleuret.org>\n");
271 while (optind < argc) {
272 root = create(argv[optind++], root);
276 struct dirent *dir_e;
279 while((dir_e = readdir(dir))) {
280 if(!ignore_entry(dir_e->d_name)) {
281 root = create(dir_e->d_name, root);
288 if(isatty(STDOUT_FILENO)) {
290 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
291 printf("Can not get the tty size: %s\n", strerror(errno));
294 print_sorted(root, win.ws_row - 2);
296 print_sorted(root, -1);