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>
37 #define BUFFER_SIZE 4096
39 typedef int64_t size_sum_t;
41 /********************************************************************/
43 size_sum_t file_or_dir_size(char *name) {
48 char subname[BUFFER_SIZE];
52 if(lstat(name, &dummy) != 0) {
53 printf("Can not stat %s: %s\n", name, strerror(errno));
57 if(S_ISLNK(dummy.st_mode)) {
64 while((dir_e = readdir(dir))) {
65 if(strcmp(dir_e->d_name, ".") &&
66 strcmp(dir_e->d_name, "..")) {
67 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
68 result += file_or_dir_size(subname);
73 if(S_ISREG(dummy.st_mode)) {
74 /* printf("%d %s\n", dummy.st_size, name); */
75 result += dummy.st_size;
82 /**********************************************************************/
84 struct file_with_size {
87 struct file_with_size *next;
90 struct file_with_size *create(char *name, struct file_with_size *current) {
91 struct file_with_size *result;
92 result = malloc(sizeof(struct file_with_size));
93 result->filename = strdup(name);
94 result->size = file_or_dir_size(name);
95 result->next = current;
99 void destroy(struct file_with_size *node) {
100 struct file_with_size *next;
103 free(node->filename);
109 /**********************************************************************/
111 int compare_files(const void *x1, const void *x2) {
112 const struct file_with_size **f1, **f2;
114 f1 = (const struct file_with_size **) x1;
115 f2 = (const struct file_with_size **) x2;
117 if((*f1)->size < (*f2)->size) {
119 } else if((*f1)->size > (*f2)->size) {
126 void print_sorted(struct file_with_size *root, int height) {
127 struct file_with_size *node;
128 struct file_with_size **nodes;
132 for(node = root; node; node = node->next) {
136 nodes = malloc(nb * sizeof(struct file_with_size *));
139 for(node = root; node; node = node->next) {
143 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
146 for(n = 0; n < nb; n++) {
154 if(height > 0 && height < nb) {
157 for(n = first; n < nb; n++) {
158 if(nodes[n]->size < 1024) {
160 ((int) nodes[n]->size),
162 } else if(nodes[n]->size < 1024 * 1024) {
163 printf("% 6.1fK %s\n",
164 ((double) (nodes[n]->size))/(1024.0),
166 } else if(nodes[n]->size < 1024 * 1024 * 1024) {
167 printf("% 6.1fM %s\n",
168 ((double) (nodes[n]->size))/(1024.0 * 1024),
171 printf("% 6.1fG %s\n",
172 ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
180 /**********************************************************************/
182 int main(int argc, char **argv) {
184 struct file_with_size *root;
188 setlocale (LC_ALL, "");
191 for(k = 1; k < argc; k++) {
192 root = create(argv[k], root);
196 struct dirent *dir_e;
199 while((dir_e = readdir(dir))) {
200 if(strcmp(dir_e->d_name, ".") &&
201 strcmp(dir_e->d_name, "..")) {
202 root = create(dir_e->d_name, root);
209 if(isatty(STDOUT_FILENO)) {
211 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
212 printf("Can not get the tty size: %s\n", strerror(errno));
215 print_sorted(root, win.ws_row - 2);
217 print_sorted(root, -1);