3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vdu.cc by Jacques Gelinas
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
30 __extension__ typedef long long longlong;
32 static int vdu_onedir (char const *path, longlong *size)
35 int dirfd = open (path,O_RDONLY); // A handle to speed up
38 fprintf (stderr,"Can't open directory %s (%s)\n",path
46 fprintf (stderr,"Can't open (opendir) directory %s (%s)\n",path
55 while ((ent=readdir(dir))!=NULL){
57 if (lstat(ent->d_name,&st)==-1){
58 fprintf (stderr,"Can't stat %s/%s (%s)\n",path
59 ,ent->d_name,strerror(errno));
62 }else if (S_ISREG(st.st_mode)){
63 if (st.st_nlink == 1){
64 dirsize += st.st_size;
66 }else if (S_ISDIR(st.st_mode) && st.st_dev == dirst.st_dev){
67 if (strcmp(ent->d_name,".")!=0
68 && strcmp(ent->d_name,"..")!=0){
69 char *tmp = malloc(strlen(path) + strlen(ent->d_name) + 2);
74 strcat(tmp, ent->d_name);
75 ret = vdu_onedir(tmp,&dirsize);
90 int main (int argc, char *argv[])
94 fprintf (stderr,"vdu version %s\n",VERSION);
95 fprintf (stderr,"vdu directory ...\n\n");
97 ,"Compute the size of a directory tree, ignoring files\n"
98 "with more than one link.\n");
103 for (i=1; i<argc && ret != -1; i++){
107 ret = vdu_onedir (argv[i],&size);
109 printf ("%s\t%ldK\n",argv[i],ksize);