Changed the buffer size, added the setlocale.
[dus.git] / dus.c
1
2 /*
3  *  dus is a simple utility to display the files and directories
4  *  according to their total disk occupancy.
5  *
6  *  Copyright (c) 2010 Francois Fleuret
7  *  Written by Francois Fleuret <francois@fleuret.org>
8  *
9  *  This file is part of dus.
10  *
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.
14  *
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.
19  *
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/>.
22  *
23  */
24
25 #define _BSD_SOURCE
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36
37 #define BUFFER_SIZE 4096
38
39 typedef int64_t size_sum_t;
40
41 /********************************************************************/
42
43 size_sum_t file_or_dir_size(char *name) {
44   DIR *dir;
45   struct dirent *dir_e;
46   struct stat dummy;
47   size_sum_t result;
48   char subname[BUFFER_SIZE];
49
50   result = 0;
51
52   if(lstat(name, &dummy) != 0) {
53     printf("Can not stat %s: %s\n", name, strerror(errno));
54     exit (1);
55   }
56
57   if(S_ISLNK(dummy.st_mode)) {
58     return 0;
59   }
60
61   dir = opendir(name);
62
63   if(dir) {
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);
69       }
70     }
71     closedir(dir);
72   } else {
73     if(S_ISREG(dummy.st_mode)) {
74       /* printf("%d %s\n", dummy.st_size, name); */
75       result += dummy.st_size;
76     }
77   }
78
79   return result;
80 }
81
82 /**********************************************************************/
83
84 struct file_with_size {
85   char *filename;
86   size_sum_t size;
87   struct file_with_size *next;
88 };
89
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;
96   return result;
97 }
98
99 void destroy(struct file_with_size *node) {
100   struct file_with_size *next;
101   while(node) {
102     next = node->next;
103     free(node->filename);
104     free(node);
105     node = next;
106   }
107 }
108
109 /**********************************************************************/
110
111 int compare_files(const void *x1, const void *x2) {
112   const struct file_with_size **f1, **f2;
113
114   f1 = (const struct file_with_size **) x1;
115   f2 = (const struct file_with_size **) x2;
116
117   if((*f1)->size < (*f2)->size) {
118     return -1;
119   } else if((*f1)->size > (*f2)->size) {
120     return 1;
121   } else {
122     return 0;
123   }
124 }
125
126 void print_sorted(struct file_with_size *root, int height) {
127   struct file_with_size *node;
128   struct file_with_size **nodes;
129   int nb, n, first;
130
131   nb = 0;
132   for(node = root; node; node = node->next) {
133     nb++;
134   }
135
136   nodes = malloc(nb * sizeof(struct file_with_size *));
137
138   n = 0;
139   for(node = root; node; node = node->next) {
140     nodes[n++] = node;
141   }
142
143   qsort(nodes, nb, sizeof(struct file_with_size *), compare_files);
144
145   /*
146   for(n = 0; n < nb; n++) {
147     printf("%lld %s\n",
148            nodes[n]->size,
149            nodes[n]->filename);
150   }
151   */
152
153   first = 0;
154   if(height > 0 && height < nb) {
155     first = nb - height;
156   }
157   for(n = first; n < nb; n++) {
158     if(nodes[n]->size < 1024) {
159       printf("% 7d %s\n",
160              ((int) nodes[n]->size),
161              nodes[n]->filename);
162     } else if(nodes[n]->size < 1024 * 1024) {
163       printf("% 6.1fK %s\n",
164              ((double) (nodes[n]->size))/(1024.0),
165              nodes[n]->filename);
166     } else if(nodes[n]->size < 1024 * 1024 * 1024) {
167       printf("% 6.1fM %s\n",
168              ((double) (nodes[n]->size))/(1024.0 * 1024),
169              nodes[n]->filename);
170     } else {
171       printf("% 6.1fG %s\n",
172              ((double) (nodes[n]->size))/(1024.0 * 1024.0 * 1024.0),
173              nodes[n]->filename);
174     }
175   }
176
177   free(nodes);
178 }
179
180 /**********************************************************************/
181
182 int main(int argc, char **argv) {
183   int k;
184   struct file_with_size *root;
185
186   root = 0;
187
188   setlocale (LC_ALL, "");
189
190   if(argc > 1) {
191     for(k = 1; k < argc; k++) {
192       root = create(argv[k], root);
193     }
194   } else {
195     DIR *dir;
196     struct dirent *dir_e;
197     dir = opendir(".");
198     if(dir) {
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);
203         }
204       }
205       closedir(dir);
206     }
207   }
208
209   if(isatty(STDOUT_FILENO)) {
210     struct winsize win;
211     if(ioctl (STDOUT_FILENO, TIOCGWINSZ, (char *) &win)) {
212       printf("Can not get the tty size: %s\n", strerror(errno));
213       exit (1);
214     }
215     print_sorted(root, win.ws_row - 2);
216   } else {
217     print_sorted(root, -1);
218   }
219
220   destroy(root);
221
222   exit(0);
223 }