src/scanner.c

changeset 44
9574a181ec26
parent 43
104e75d18ede
child 48
0d2c13c24fd0
     1.1 --- a/src/scanner.c	Wed May 22 10:57:17 2013 +0200
     1.2 +++ b/src/scanner.c	Wed May 22 13:00:36 2013 +0200
     1.3 @@ -117,11 +117,13 @@
     1.4    return list;
     1.5  }
     1.6  
     1.7 -int scanDirectory(scanner_t scanner, settings_t* settings) {
     1.8 +int scanDirectory(scanner_t scanner, settings_t* settings,
     1.9 +    string_list_t* output) {
    1.10  
    1.11    int lines, a;
    1.12    int lineSum = 0;
    1.13    bool bfile;
    1.14 +  char *outbuf;
    1.15  
    1.16    filelist_t *filelist = buildFileList(scanner, settings, NULL);
    1.17  
    1.18 @@ -129,11 +131,28 @@
    1.19  
    1.20      /* Scan subdirectories */
    1.21      if (!S_ISREG(filelist->st_mode)) {
    1.22 -      printf("%*s\n", filelist->displayname_len+scanner.spaces,
    1.23 +      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
    1.24 +        string_list_t *recoutput = new_string_list_t();
    1.25 +        lines = scanDirectory(
    1.26 +            (scanner_t) {filelist->filename, scanner.spaces+1},
    1.27 +            settings, recoutput);
    1.28 +        lineSum += lines;
    1.29 +        if (!settings->matchesOnly || recoutput->count > 0) {
    1.30 +          outbuf = (char*) malloc(81);
    1.31 +          snprintf(outbuf, 81, "%*s/%*s%13d lines\n",
    1.32 +              filelist->displayname_len+scanner.spaces, filelist->displayname,
    1.33 +              60-filelist->displayname_len-scanner.spaces-1, "", lines);
    1.34 +          add_string(output, outbuf);
    1.35 +          for (int i = 0 ; i < recoutput->count ; i++) {
    1.36 +            add_string(output, recoutput->items[i]);
    1.37 +          }
    1.38 +        }
    1.39 +        destroy_string_list_t(recoutput);
    1.40 +      } else {
    1.41 +        outbuf = (char*) malloc(81);
    1.42 +        snprintf(outbuf, 81, "%*s\n", filelist->displayname_len+scanner.spaces,
    1.43            filelist->displayname);
    1.44 -      if (settings->recursive && S_ISDIR(filelist->st_mode)) {
    1.45 -        lineSum += scanDirectory(
    1.46 -            (scanner_t) {filelist->filename, scanner.spaces+1}, settings);
    1.47 +        add_string(output, outbuf);
    1.48        }
    1.49      } else {
    1.50        if ((settings->includeSuffixes->count == 0
    1.51 @@ -149,8 +168,10 @@
    1.52  
    1.53          FILE *file = fopen(filelist->filename, "r");
    1.54          if (file == NULL) {
    1.55 -          printf("%*s", filelist->displayname_len+scanner.spaces,
    1.56 +          outbuf = (char*) malloc(81);
    1.57 +          snprintf(outbuf, 81, "%*s", filelist->displayname_len+scanner.spaces,
    1.58                filelist->displayname);
    1.59 +          add_string(output, outbuf);
    1.60            perror("  File acces failed");
    1.61          } else {
    1.62            do {
    1.63 @@ -184,23 +205,30 @@
    1.64            /* Print and sum line count */
    1.65            if (bfile) {
    1.66              if (!settings->matchesOnly) {
    1.67 -              printf("%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
    1.68 +              outbuf = (char*) malloc(81);
    1.69 +              snprintf(outbuf, 81,
    1.70 +                  "%*s%*s%19s\n", filelist->displayname_len+scanner.spaces,
    1.71                    filelist->displayname,
    1.72                    60-filelist->displayname_len-scanner.spaces, "", "binary");
    1.73 +              add_string(output, outbuf);
    1.74              }
    1.75            } else {
    1.76              lineSum += lines;
    1.77 -            printf("%*s%*s%13d lines\n",
    1.78 +            outbuf = (char*) malloc(81);
    1.79 +            snprintf(outbuf, 81, "%*s%*s%13d lines\n",
    1.80                  filelist->displayname_len+scanner.spaces, filelist->displayname,
    1.81                  60-filelist->displayname_len-scanner.spaces, "", lines);
    1.82 +            add_string(output, outbuf);
    1.83            }
    1.84          }
    1.85        } else {
    1.86          if (!settings->matchesOnly) {
    1.87            /* Print hint */
    1.88 -          printf("%*s%*s%19s\n",
    1.89 +          outbuf = (char*) malloc(81);
    1.90 +          snprintf(outbuf, 81, "%*s%*s%19s\n",
    1.91                filelist->displayname_len+scanner.spaces, filelist->displayname,
    1.92                60-filelist->displayname_len-scanner.spaces, "", "no match");
    1.93 +          add_string(output, outbuf);
    1.94          }
    1.95        }
    1.96      }

mercurial