src/scanner.c

changeset 44
9574a181ec26
parent 43
104e75d18ede
child 48
0d2c13c24fd0
--- a/src/scanner.c	Wed May 22 10:57:17 2013 +0200
+++ b/src/scanner.c	Wed May 22 13:00:36 2013 +0200
@@ -117,11 +117,13 @@
   return list;
 }
 
-int scanDirectory(scanner_t scanner, settings_t* settings) {
+int scanDirectory(scanner_t scanner, settings_t* settings,
+    string_list_t* output) {
 
   int lines, a;
   int lineSum = 0;
   bool bfile;
+  char *outbuf;
 
   filelist_t *filelist = buildFileList(scanner, settings, NULL);
 
@@ -129,11 +131,28 @@
 
     /* Scan subdirectories */
     if (!S_ISREG(filelist->st_mode)) {
-      printf("%*s\n", filelist->displayname_len+scanner.spaces,
+      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
+        string_list_t *recoutput = new_string_list_t();
+        lines = scanDirectory(
+            (scanner_t) {filelist->filename, scanner.spaces+1},
+            settings, recoutput);
+        lineSum += lines;
+        if (!settings->matchesOnly || recoutput->count > 0) {
+          outbuf = (char*) malloc(81);
+          snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
+              filelist->displayname_len+scanner.spaces, filelist->displayname,
+              60-filelist->displayname_len-scanner.spaces-1, "", lines);
+          add_string(output, outbuf);
+          for (int i = 0 ; i < recoutput->count ; i++) {
+            add_string(output, recoutput->items[i]);
+          }
+        }
+        destroy_string_list_t(recoutput);
+      } else {
+        outbuf = (char*) malloc(81);
+        snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
           filelist->displayname);
-      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
-        lineSum += scanDirectory(
-            (scanner_t) {filelist->filename, scanner.spaces+1}, settings);
+        add_string(output, outbuf);
       }
     } else {
       if ((settings->includeSuffixes->count == 0
@@ -149,8 +168,10 @@
 
         FILE *file = fopen(filelist->filename, "r");
         if (file == NULL) {
-          printf("%*s", filelist->displayname_len+scanner.spaces,
+          outbuf = (char*) malloc(81);
+          snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
               filelist->displayname);
+          add_string(output, outbuf);
           perror("  File acces failed");
         } else {
           do {
@@ -184,23 +205,30 @@
           /* Print and sum line count */
           if (bfile) {
             if (!settings->matchesOnly) {
-              printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
+              outbuf = (char*) malloc(81);
+              snprintf(outbuf, 81,
+                  "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
                   filelist->displayname,
                   60-filelist->displayname_len-scanner.spaces, "", "binary");
+              add_string(output, outbuf);
             }
           } else {
             lineSum += lines;
-            printf("%*s%*s%13d lines\n",
+            outbuf = (char*) malloc(81);
+            snprintf(outbuf, 81, "%*s%*s%13d lines\n",
                 filelist->displayname_len+scanner.spaces, filelist->displayname,
                 60-filelist->displayname_len-scanner.spaces, "", lines);
+            add_string(output, outbuf);
           }
         }
       } else {
         if (!settings->matchesOnly) {
           /* Print hint */
-          printf("%*s%*s%19s\n",
+          outbuf = (char*) malloc(81);
+          snprintf(outbuf, 81, "%*s%*s%19s\n",
               filelist->displayname_len+scanner.spaces, filelist->displayname,
               60-filelist->displayname_len-scanner.spaces, "", "no match");
+          add_string(output, outbuf);
         }
       }
     }

mercurial