3 * dus is a simple utility designed to display the list of files and
4 * directories with disk occupancy, sorted according to it.
6 * Copyright (c) 2009 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 1024
39 typedef int64_t size_sum_t;
41 size_sum_t file_or_dir_size(char *name) {
46 char subname[BUFFER_SIZE];
50 if(lstat(name, &dummy) != 0) {
51 printf("Can not stat %s: %s\n", name, strerror(errno));
55 if(S_ISLNK(dummy.st_mode)) {
62 while((dir_e = readdir(dir))) {
63 if(strcmp(dir_e->d_name, ".") &&
64 strcmp(dir_e->d_name, "..")) {
65 snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
66 result += file_or_dir_size(subname);
71 if(S_ISREG(dummy.st_mode)) {
72 /* printf("%d %s\n", dummy.st_size, name); */
73 result += dummy.st_size;
80 /**********************************************************************/
82 struct file_with_size {
85 struct file_with_size *next;
88 struct file_with_size *create(char *name, struct file_with_size *current) {
89 struct file_with_size *result;
90 result = malloc(sizeof(struct file_with_size));
91 result->filename = strdup(name);
92 result->size = file_or_dir_size(name);
93 result->next = current;
97 void destroy(struct file_with_size *node) {
98 struct file_with_size *next;
101 free(node->filename);
107 /**********************************************************************/
109 int compare_files(const void *x1, const void *x2) {
110 const struct file_with_size **f1, **f2;
112 f1 = (const struct file_with_size **) x1;
113 f2 = (const struct file_with_size **) x2;
115 if((*f1)->size < (*f2)->size) {
117 } else if((*f1)->size > (*f2)->size) {
125 void print_sorted(struct file_with_size *root, int height) {
126 struct file_with_size *node;
127 struct file_with_size **nodes;
131 for(node = root; node; node = node->next) {
135 nodes = malloc(nb * sizeof(struct file_with_size *));
138 for(node = root; node; node = node->next) {
142 qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
145 for(n = 0; n < nb; n++) {
153 if(height > 0 && height < nb) {
156 for(n = first; n < nb; n++) {
157 if(nodes[n]->size < 1024) {
159 ((int) nodes[n]->size),
161 } else if(nodes[n]->size < 1024 * 1024) {
162 printf("% 6.1fK %s\n",
163 ((double) (nodes[n]->size))/(1024.0),
165 } else if(nodes[n]->size < 1024 * 1024 * 1024) {
166 printf("% 6.1fM %s\n",
167 ((double) (nodes[n]->size))/(1024.0 * 1024),
170 printf("% 6.1fG %s\n",
171 ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
179 /**********************************************************************/
181 int main(int argc, char **argv) {
183 struct file_with_size *root;
188 for(k = 1; k < argc; k++) {
189 root = create(argv[k], root);
193 struct dirent *dir_e;
196 while((dir_e = readdir(dir))) {
197 if(strcmp(dir_e->d_name, ".") &&
198 strcmp(dir_e->d_name, "..")) {
199 root = create(dir_e->d_name, root);
206 if(isatty(STDOUT_FILENO)) {
208 if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
209 printf("Can not get the tty size: %s\n", strerror(errno));
212 print_sorted(root, win.ws_row - 2);
214 print_sorted(root, -1);