--- a/src/cline.c Wed May 22 10:57:17 2013 +0200 +++ b/src/cline.c Wed May 22 13:00:36 2013 +0200 @@ -33,7 +33,6 @@ #include "scanner.h" #include "settings.h" #include "arguments.h" -#include "stream.h" #include "regex_parser.h" void printHelpText() { @@ -219,48 +218,65 @@ } } - /* Configure output */ - if (!settings->verbose) { - close_stdout(); - } - /* Find tokens */ parseCSL(includeSuffix, settings->includeSuffixes); parseCSL(excludeSuffix, settings->excludeSuffixes); /* Scan directories */ if (regex_compile_all(settings->regex)) { - int lines = 0; + /* Don't waste memory when only the total sum is needed */ + string_list_t *output = settings->verbose ? new_string_list_t() : NULL; + char *outbuf; + + int lineSum = 0, lines; if (directories->count == 0) { add_string(directories, "."); } for (int t = 0 ; t < directories->count ; t++) { - if (t > 0) { - for (int u = 0 ; u < 79 ; u++) { - printf("-"); - } - printf("\n"); + lines = scanDirectory((scanner_t){directories->items[t], 0}, settings, + output); + lineSum += lines; + if (directories->count > 1 ) { + outbuf = (char*) malloc(81); + memset(outbuf, '-', 79); + outbuf[79] = '\n'; + outbuf[80] = 0; + add_string(output, outbuf); + outbuf = (char*) malloc(81); + snprintf(outbuf, 81, "%-63s%10d lines\n", directories->items[t], lines); + add_string(output, outbuf); + outbuf = (char*) malloc(81); + memset(outbuf, '-', 79); + outbuf[79] = '\n'; + outbuf[80] = 0; + add_string(output, outbuf); } - lines += scanDirectory((scanner_t){directories->items[t], 0}, settings); } destroy_string_list_t(directories); - /* Print double line and line count */ - for (int t = 0 ; t < 79 ; t++) { - printf("="); - } - printf("\n%73d lines\n", lines); + /* Print result */ + if (settings->verbose) { + for (int i = 0 ; i < output->count ; i++) { + printf("%s", output->items[i]); + free(output->items[i]); + } + + for (int t = 0 ; t < 79 ; t++) { + printf("="); + } + printf("\n%73d lines\n", lineSum); - if (settings->confusing_lnlen && settings->regex->pattern_list->count > 0) { - printf("\nSome files contain too long lines.\n" - "The regex parser currently supports a maximum line length of %d." - "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); + if (settings->confusing_lnlen && + settings->regex->pattern_list->count > 0) { + + printf("\nSome files contain too long lines.\n" + "The regex parser currently supports a maximum line length of %d." + "\nThe result might be wrong.\n", REGEX_MAX_LINELENGTH); + } + } else { + printf("%d", lineSum); } - - if (!settings->verbose) { - reopen_stdout(); - printf("%d", lines); - } + destroy_string_list_t(output); destroy_settings_t(settings); }