2 /*************************************************************************/
5 /* This program is free software: you can redistribute it and/or modify */
6 /* it under the terms of the version 3 of the GNU General Public License */
7 /* as published by the Free Software Foundation. */
9 /* This program is distributed in the hope that it will be useful, but */
10 /* WITHOUT ANY WARRANTY; without even the implied warranty of */
11 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
12 /* General Public License for more details. */
14 /* You should have received a copy of the GNU General Public License */
15 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by and Copyright (C) Francois Fleuret */
18 /* Contact <francois.fleuret@idiap.ch> for comments & bug reports */
21 /*************************************************************************/
25 #include <sys/types.h>
34 struct file_with_size {
37 struct file_with_size *next;
40 size_t file_or_dir_size(char *name) {
49 if(lstat(name, &dummy) != 0) {
50 printf("Can not stat %s (%s).\n", name, strerror(errno));
54 if(S_ISLNK(dummy.st_mode)) {
61 while((dir_e = readdir(dir))) {
62 if(strcmp(dir_e->d_name, ".") &&
63 strcmp(dir_e->d_name, "..")) {
64 sprintf(subname, "%s/%s", name, dir_e->d_name);
65 result += file_or_dir_size(subname);
70 if(S_ISREG(dummy.st_mode)) {
71 result += dummy.st_size;
78 struct file_with_size *create(char *name, struct file_with_size *current) {
79 struct file_with_size *result;
80 result = malloc(sizeof(struct file_with_size));
81 result->filename = strdup(name);
82 result->size = file_or_dir_size(name);
83 result->next = current;
87 int main(int argc, char **argv) {
89 struct file_with_size *root;
92 for(k = 1; k < argc; k++) {
93 root = create(argv[k], root);