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