00001
00002
00003
00004
00005
00006 #include <stdio.h>
00007 #include <sys/types.h>
00008 #include <sys/stat.h>
00009 #include <dirent.h>
00010 #include <string.h>
00011
00012 void fsize(char *);
00013 void dirwalk(char *, void (*fcn)(char *));
00014
00015 static double total;
00016
00017
00018 double du_dir(char *dirname)
00019 {
00020 total = 0.0;
00021 fsize(dirname);
00022 return(total);
00023 }
00024
00025
00026
00027 void fsize(char *name)
00028 {
00029 struct stat stbuf;
00030
00031 if(lstat(name, &stbuf) == -1) {
00032 fprintf(stderr, "du_dir can't access %s\n", name);
00033 return;
00034 }
00035 if((stbuf.st_mode & S_IFMT) == S_IFDIR)
00036 dirwalk(name, fsize);
00037 total = total + (double)stbuf.st_size;
00038 }
00039
00040
00041 void dirwalk(char *dir, void (*fcn)(char *))
00042 {
00043 char name[196];
00044 struct dirent *dp;
00045 DIR *dfd;
00046
00047 if((dfd=opendir(dir)) == NULL) {
00048 fprintf(stderr, "du_dir can't open dir %s\n", dir);
00049 return;
00050 }
00051 while((dp=readdir(dfd)) != NULL) {
00052 if(strcmp(dp->d_name, ".") == 0
00053 || strcmp(dp->d_name, "..") == 0)
00054 continue;
00055 sprintf(name, "%s/%s", dir, dp->d_name);
00056 (*fcn)(name);
00057 }
00058 closedir(dfd);
00059 }