util-vserver-0.30.208
[util-vserver.git] / src / vdu.c
1 // $Id: vdu.c,v 1.2 2003/09/30 20:16:53 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 #ifdef HAVE_CONFIG_H
21 #  include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include <errno.h>
32 #include <string.h>
33
34 __extension__ typedef long long         longlong;
35
36 static int vdu_onedir (char const *path, longlong *size)
37 {
38         int ret = -1;
39         int dirfd = open (path,O_RDONLY);       // A handle to speed up
40                                                                                                 // chdir
41         if (dirfd == -1){
42                 fprintf (stderr,"Can't open directory %s (%s)\n",path
43                         ,strerror(errno));
44         }else{
45                 DIR *dir;
46
47                 fchdir (dirfd);
48                 dir = opendir (".");
49                 if (dir == NULL){
50                         fprintf (stderr,"Can't open (opendir) directory %s (%s)\n",path
51                                 ,strerror(errno));
52                 }else{
53                         struct stat dirst;
54                         struct dirent *ent;
55                         longlong dirsize = 0;
56
57                         ret = 0;
58                         lstat (".",&dirst);
59                         while ((ent=readdir(dir))!=NULL){
60                                 struct stat st;
61                                 if (lstat(ent->d_name,&st)==-1){
62                                         fprintf (stderr,"Can't stat %s/%s (%s)\n",path
63                                                 ,ent->d_name,strerror(errno));
64                                         ret = -1;
65                                         break;
66                                 }else if (S_ISREG(st.st_mode)){
67                                         if (st.st_nlink == 1){
68                                                 dirsize += st.st_size;
69                                         }
70                                 }else if (S_ISDIR(st.st_mode) && st.st_dev == dirst.st_dev){
71                                         if (strcmp(ent->d_name,".")!=0
72                                                 && strcmp(ent->d_name,"..")!=0){
73                                                 char    *tmp = malloc(strlen(path) + strlen(ent->d_name) + 2);
74                                                 if (tmp==0) ret=-1;
75                                                 else {
76                                                   strcpy(tmp, path);
77                                                   strcat(tmp, "/");
78                                                   strcat(tmp, ent->d_name);
79                                                   ret = vdu_onedir(tmp,&dirsize);
80                                                   free(tmp);
81                                                   fchdir (dirfd);
82                                                 }
83                                         }
84                                 }
85                         }
86                         closedir (dir);
87                         *size += dirsize;
88                 }
89                 close (dirfd);
90         }
91         return ret;
92 }
93
94 int main (int argc, char *argv[])
95 {
96         int ret = -1;
97         if (argc < 2){
98                 fprintf (stderr,"vdu version %s\n",VERSION);
99                 fprintf (stderr,"vdu directory ...\n\n");
100                 fprintf (stderr
101                         ,"Compute the size of a directory tree, ignoring files\n"
102                          "with more than one link.\n");
103         }else{
104                 int i;
105
106                 ret = 0;
107                 for (i=1; i<argc && ret != -1; i++){
108                         longlong size = 0;
109                         long ksize;
110                         
111                         ret = vdu_onedir (argv[i],&size);
112                         ksize = size >> 10;
113                         printf ("%s\t%ldK\n",argv[i],ksize);
114                 }
115         }
116         return ret;
117 }
118