Added an option ignore_dotfiles.
authorFrancois Fleuret <francois@fleuret.org>
Fri, 26 Feb 2010 09:57:00 +0000 (10:57 +0100)
committerFrancois Fleuret <francois@fleuret.org>
Fri, 26 Feb 2010 09:57:00 +0000 (10:57 +0100)
dus.c

diff --git a/dus.c b/dus.c
index 338b69b..a4b5d0a 100644 (file)
--- a/dus.c
+++ b/dus.c
 #include <errno.h>
 #include <string.h>
 #include <sys/ioctl.h>
+#include <locale.h>
 
 #define BUFFER_SIZE 4096
 
 typedef int64_t size_sum_t;
 
+int ignore_dotfiles = 1;
+
 /********************************************************************/
 
-size_sum_t file_or_dir_size(char *name) {
+int ignore_entry(const char *name) {
+  return
+    strcmp(name, ".") == 0 ||
+    strcmp(name, "..") == 0 ||
+    (ignore_dotfiles && name[0] == '.');
+}
+
+size_sum_t file_or_dir_size(const char *name) {
   DIR *dir;
   struct dirent *dir_e;
   struct stat dummy;
@@ -62,8 +72,7 @@ size_sum_t file_or_dir_size(char *name) {
 
   if(dir) {
     while((dir_e = readdir(dir))) {
-      if(strcmp(dir_e->d_name, ".") &&
-         strcmp(dir_e->d_name, "..")) {
+      if(!ignore_entry(dir_e->d_name)) {
         snprintf(subname, BUFFER_SIZE, "%s/%s", name, dir_e->d_name);
         result += file_or_dir_size(subname);
       }
@@ -197,8 +206,7 @@ int main(int argc, char **argv) {
     dir = opendir(".");
     if(dir) {
       while((dir_e = readdir(dir))) {
-        if(strcmp(dir_e->d_name, ".") &&
-           strcmp(dir_e->d_name, "..")) {
+        if(!ignore_entry(dir_e->d_name)) {
           root = create(dir_e->d_name, root);
         }
       }