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>
36 struct file_with_size {
39 struct file_with_size *next;
42 size_t file_or_dir_size(char *name) {
51 if(lstat(name, &dummy) != 0) {
52 printf("Can not stat %s (%s).\n", name, strerror(errno));
56 if(S_ISLNK(dummy.st_mode)) {
63 while((dir_e = readdir(dir))) {
64 if(strcmp(dir_e->d_name, ".") &&
65 strcmp(dir_e->d_name, "..")) {
66 sprintf(subname, "%s/%s", name, dir_e->d_name);
67 result += file_or_dir_size(subname);
72 if(S_ISREG(dummy.st_mode)) {
73 result += dummy.st_size;
80 struct file_with_size *create(char *name, struct file_with_size *current) {
81 struct file_with_size *result;
82 result = malloc(sizeof(struct file_with_size));
83 result->filename = strdup(name);
84 result->size = file_or_dir_size(name);
85 result->next = current;
89 int main(int argc, char **argv) {
91 struct file_with_size *root;
94 for(k = 1; k < argc; k++) {
95 root = create(argv[k], root);