This commit was generated by cvs2svn to compensate for changes in r120,
[util-vserver.git] / src / vdu.c
1 // $Id: vdu.c,v 1.1 2003/09/29 22:01:57 ensc Exp $
2
3 // Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 // based on vdu.cc by Jacques Gelinas
5 //  
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)
9 // any later version.
10 //  
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.
15 //  
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.
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <string.h>
29
30 __extension__ typedef long long         longlong;
31
32 static int vdu_onedir (char const *path, longlong *size)
33 {
34         int ret = -1;
35         int dirfd = open (path,O_RDONLY);       // A handle to speed up
36                                                                                                 // chdir
37         if (dirfd == -1){
38                 fprintf (stderr,"Can't open directory %s (%s)\n",path
39                         ,strerror(errno));
40         }else{
41                 DIR *dir;
42
43                 fchdir (dirfd);
44                 dir = opendir (".");
45                 if (dir == NULL){
46                         fprintf (stderr,"Can't open (opendir) directory %s (%s)\n",path
47                                 ,strerror(errno));
48                 }else{
49                         struct stat dirst;
50                         struct dirent *ent;
51                         longlong dirsize = 0;
52
53                         ret = 0;
54                         lstat (".",&dirst);
55                         while ((ent=readdir(dir))!=NULL){
56                                 struct stat st;
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));
60                                         ret = -1;
61                                         break;
62                                 }else if (S_ISREG(st.st_mode)){
63                                         if (st.st_nlink == 1){
64                                                 dirsize += st.st_size;
65                                         }
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);
70                                                 if (tmp==0) ret=-1;
71                                                 else {
72                                                   strcpy(tmp, path);
73                                                   strcat(tmp, "/");
74                                                   strcat(tmp, ent->d_name);
75                                                   ret = vdu_onedir(tmp,&dirsize);
76                                                   free(tmp);
77                                                   fchdir (dirfd);
78                                                 }
79                                         }
80                                 }
81                         }
82                         closedir (dir);
83                         *size += dirsize;
84                 }
85                 close (dirfd);
86         }
87         return ret;
88 }
89
90 int main (int argc, char *argv[])
91 {
92         int ret = -1;
93         if (argc < 2){
94                 fprintf (stderr,"vdu version %s\n",VERSION);
95                 fprintf (stderr,"vdu directory ...\n\n");
96                 fprintf (stderr
97                         ,"Compute the size of a directory tree, ignoring files\n"
98                          "with more than one link.\n");
99         }else{
100                 int i;
101
102                 ret = 0;
103                 for (i=1; i<argc && ret != -1; i++){
104                         longlong size = 0;
105                         long ksize;
106                         
107                         ret = vdu_onedir (argv[i],&size);
108                         ksize = size >> 10;
109                         printf ("%s\t%ldK\n",argv[i],ksize);
110                 }
111         }
112         return ret;
113 }
114