--- a/src/cline.c Wed Apr 09 21:47:07 2025 +0200 +++ b/src/cline.c Thu Apr 10 21:29:56 2025 +0200 @@ -261,77 +261,78 @@ parseCSL(includeSuffix, settings->includeSuffixes); parseCSL(excludeSuffix, settings->excludeSuffixes); + /* Compiler regular expressions, if any specified */ + if (!regex_compile_all(settings->regex)) { + return 1; + } + /* Scan directories */ - if (regex_compile_all(settings->regex)) { - scanresult_t* result = new_scanresult_t(settings); + scanresult_t* result = new_scanresult_t(settings); + const char* result_type = settings->count_chars ? "chars" : "lines"; + bool has_output = false; + unsigned total = 0; + if (directories->count == 0) { + add_string(directories, "./"); + } + for (unsigned t = 0 ; t < directories->count ; t++) { /* Don't waste memory when only the total sum is needed */ string_list_t *output = settings->verbose ? new_string_list_t() : NULL; - char *outbuf; - const char* result_type = settings->count_chars ? "chars" : "lines"; - - unsigned total = 0; - if (directories->count == 0) { - add_string(directories, "./"); - } - for (unsigned t = 0 ; t < directories->count ; t++) { - scanDirectory((scanner_t){directories->items[t], 0}, settings, - output, result); - total += result->result; - if (directories->count > 1) { - add_string(output, strdup(sepline_single)); - outbuf = (char*) malloc(81); - snprintf(outbuf, 81, "%-63s%10u %s\n", directories->items[t], - result->result, result_type); - add_string(output, outbuf); - add_string(output, strdup(sepline_single)); - } - } - destroy_string_list_t(directories); - - /* Print result */ - if (output != NULL) { + scanDirectory((scanner_t){directories->items[t], 0}, settings, output, result); + total += result->result; + if (settings->verbose) { + has_output |= output->count > 0; for (int i = 0 ; i < output->count ; i++) { printf("%s", output->items[i]); free(output->items[i]); } - - if (result->ext) { - if (result->ext->count > 0) { - fwrite(sepline_double, 1, 80, stdout); - printf("\nIndividual sums:\n"); - for (unsigned t = 0 ; t < result->ext->count ; t++) { - printf(" %-62s%10u %s\n", - result->ext->extensions[t], - result->ext->result[t], - result_type); - } + destroy_string_list_t(output); + if (directories->count > 1) { + has_output = true; + fputs(sepline_single, stdout); + printf("%-63s%10u %s\n", directories->items[t], + result->result, result_type); + fputs(sepline_single, stdout); + } + } + } + destroy_string_list_t(directories); + + /* Print result */ + if (settings->verbose) { + if (result->ext) { + if (result->ext->count > 0) { + has_output = true; + fwrite(sepline_double, 1, 80, stdout); + printf("\nIndividual sums:\n"); + for (unsigned t = 0 ; t < result->ext->count ; t++) { + printf(" %-62s%10u %s\n", + result->ext->extensions[t], + result->ext->result[t], + result_type); } } + } - if (output->count > 0) { - fwrite(sepline_double, 1, 80, stdout); - printf("%73d %s\n", total, result_type); - } else { - /* If we only need to output a total, output it differently */ - printf("%d %s\n", total, result_type); - } - - if (settings->confusing_lnlen && - settings->regex->pattern_list->count > 0) { + if (has_output) { + fwrite(sepline_double, 1, 80, stdout); + printf("%73d %s\n", total, result_type); + } else { + /* If there was not any output so far, skip formatting */ + printf("%d %s\n", total, result_type); + } - printf("\nSome files contain too long lines.\n" - "The parser currently supports a maximum line length of %u." - "\nThe result might be wrong.\n", MAX_LINELENGTH); - } - } else { - printf("%u", total); + if (settings->confusing_lnlen && + settings->regex->pattern_list->count > 0) { + + printf("\nSome files contain too long lines.\n" + "The parser currently supports a maximum line length of %u." + "\nThe result might be wrong.\n", MAX_LINELENGTH); } - destroy_scanresult_t(result); - destroy_string_list_t(output); - destroy_settings_t(settings); + } else { + printf("%u", total); } + destroy_scanresult_t(result); + destroy_settings_t(settings); - fflush(stdout); - fflush(stderr); return 0; }