1 // $Id: vdu.c,v 1.1 2003/09/29 22:01:57 ensc Exp $
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.
28 #include <sys/types.h>
35 #include <sys/ioctl.h>
38 // Patch to help compile this utility on unpatched kernel source
39 #ifndef EXT2_IMMUTABLE_FILE_FL
40 #define EXT2_IMMUTABLE_FILE_FL 0x00000010
41 #define EXT2_IMMUTABLE_LINK_FL 0x00008000
45 //__extension__ typedef long long longlong;
46 __extension__ typedef long longlong;
48 static longlong inodes;
49 static longlong blocks;
52 static short verbose = 0;
54 static inline void warning(char *s) {
55 fprintf(stderr,"%s (%s)\n",s,strerror(errno));
63 static void vdu_onedir (char const *path)
65 struct stat dirst, st;
70 longlong dirsize, dirinodes, dirblocks;
72 dirsize = dirinodes = dirblocks = 0;
74 // A handle to speed up chdir
75 if ((dirfd = open (path,O_RDONLY)) == -1) {
76 fprintf (stderr,"Can't open directory %s\n",path);
80 if (fchdir (dirfd) == -1) {
81 fprintf (stderr,"Can't fchdir directory %s\n",path);
82 panic("fchdir failed");
85 if (fstat (dirfd,&dirst) != 0) {
86 fprintf (stderr,"Can't lstat directory %s\n",path);
87 panic("lstat failed");
90 if ((dir = opendir (".")) == NULL) {
91 fprintf (stderr,"Can't open (opendir) directory %s\n",path);
92 panic("opendir failed");
96 /* Walk the directory entries and compute the sum of inodes,
97 blocks, and disk space used. This code will recursively descend
98 down the directory structure. */
100 while ((ent=readdir(dir))!=NULL){
101 if (lstat(ent->d_name,&st)==-1){
102 fprintf (stderr,"Can't stat %s/%s\n",path,ent->d_name);
103 warning("lstat failed");
109 if (S_ISREG(st.st_mode)){
110 if (st.st_nlink > 1){
114 if ((fd = open(ent->d_name,O_RDONLY))==-1) {
115 fprintf (stderr,"Can't open file %s/%s\n",path,ent->d_name);
116 warning ("open failed");
121 res = ioctl(fd, EXT2_IOC_GETFLAGS, &flags);
124 if ((res == 0) && (flags & EXT2_IMMUTABLE_LINK_FL)){
126 printf ("Skipping %s\n",ent->d_name);
130 dirsize += st.st_size;
131 dirblocks += st.st_blocks;
133 } else if (S_ISDIR(st.st_mode)) {
134 if ((st.st_dev == dirst.st_dev) &&
135 (strcmp(ent->d_name,".")!=0) &&
136 (strcmp(ent->d_name,"..")!=0)) {
138 dirsize += st.st_size;
139 dirblocks += st.st_blocks;
141 name = strdup(ent->d_name);
143 panic("Out of memory\n");
150 dirsize += st.st_size;
151 dirblocks += st.st_blocks;
157 printf("%8ld %8ld %8ld %s\n",dirinodes, dirblocks, dirsize>>10,path);
165 int main (int argc, char *argv[])
170 fprintf (stderr,"vdu version %s\n",VERSION);
171 fprintf (stderr,"vdu directory ...\n\n");
172 fprintf (stderr,"Compute the size of a directory tree.");
174 if ((startdir = open (".",O_RDONLY)) == -1) {
175 fprintf (stderr,"Can't open current working directory\n");
176 panic("open failed");
179 for (i=1; i<argc; i++){
180 inodes = blocks = size = 0;
181 vdu_onedir (argv[i]);
182 printf("%8ld %8ld %8ld %s TOTAL\n",inodes, blocks, size>>10,argv[i]);
183 if (fchdir (startdir) == -1) {
184 panic("fchdir failed");