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>
38 #define BUFFER_SIZE 4096
40 typedef int64_t size_sum_t;
42 int ignore_dotfiles = 1;
44 /********************************************************************/
46 int ignore_entry(const char *name) {
48 strcmp(name, ".") == 0 ||
49 strcmp(name, "..") == 0 ||
50 (ignore_dotfiles && name[0] == '.');
53 size_sum_t file_or_dir_size(const char *name) {
58 char subname[BUFFER_SIZE];
62 if(lstat(name, &dummy) != 0) {
63 printf("Can not stat %s: %s\n", name, strerror(errno));
67 if(S_ISLNK(dummy.st_mode)) {
74 while((dir_e = readdir(dir))) {
75 if(!ignore_entry(dir_e->d_name)) {
76 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
77 result += file_or_dir_size(subname);
82 if(S_ISREG(dummy.st_mode)) {
83 /* printf("%d %s\n", dummy.st_size, name); */
84 result += dummy.st_size;
91 /**********************************************************************/
93 struct file_with_size {
96 struct file_with_size *next;
99 struct file_with_size *create(char *name, struct file_with_size *current) {
100 struct file_with_size *result;
101 result = malloc(sizeof(struct file_with_size));
102 result->filename = strdup(name);
103 result->size = file_or_dir_size(name);
104 result->next = current;
108 void destroy(struct file_with_size *node) {
109 struct file_with_size *next;
112 free(node->filename);
118 /**********************************************************************/
120 int compare_files(const void *x1, const void *x2) {
121 const struct file_with_size **f1, **f2;
123 f1 = (const struct file_with_size **) x1;
124 f2 = (const struct file_with_size **) x2;
126 if((*f1)->size < (*f2)->size) {
128 } else if((*f1)->size > (*f2)->size) {
135 void print_sorted(struct file_with_size *root, int height) {
136 struct file_with_size *node;
137 struct file_with_size **nodes;
141 for(node = root; node; node = node->next) {
145 nodes = malloc(nb * sizeof(struct file_with_size *));
148 for(node = root; node; node = node->next) {
152 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
155 for(n = 0; n < nb; n++) {
163 if(height > 0 && height < nb) {
166 for(n = first; n < nb; n++) {
167 if(nodes[n]->size < 1024) {
169 ((int) nodes[n]->size),
171 } else if(nodes[n]->size < 1024 * 1024) {
172 printf("% 6.1fK %s\n",
173 ((double) (nodes[n]->size))/(1024.0),
175 } else if(nodes[n]->size < 1024 * 1024 * 1024) {
176 printf("% 6.1fM %s\n",
177 ((double) (nodes[n]->size))/(1024.0 * 1024),
180 printf("% 6.1fG %s\n",
181 ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
189 /**********************************************************************/
191 int main(int argc, char **argv) {
193 struct file_with_size *root;
197 setlocale (LC_ALL, "");
200 for(k = 1; k < argc; k++) {
201 root = create(argv[k], root);
205 struct dirent *dir_e;
208 while((dir_e = readdir(dir))) {
209 if(!ignore_entry(dir_e->d_name)) {
210 root = create(dir_e->d_name, root);
217 if(isatty(STDOUT_FILENO)) {
219 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
220 printf("Can not get the tty size: %s\n", strerror(errno));
223 print_sorted(root, win.ws_row - 2);
225 print_sorted(root, -1);