From 1f03dd9f8455147d907449a61fc46984fd7799dd Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Fri, 25 Mar 2022 21:38:08 -0500 Subject: [PATCH] Bugfix: When using --at with --total for --content, the returned number is always 1 and should not be. The code need to count all newlines rather than always returning 1. This is likely a bug from copying the code from fss_basic_read where the non-zero count would indeed always be 1. --- level_3/fss_basic_list_read/c/private-read.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/level_3/fss_basic_list_read/c/private-read.c b/level_3/fss_basic_list_read/c/private-read.c index 0778eaf..8720f41 100644 --- a/level_3/fss_basic_list_read/c/private-read.c +++ b/level_3/fss_basic_list_read/c/private-read.c @@ -419,16 +419,27 @@ extern "C" { fll_print_format("%ul%r", main->output.to.stream, data->contents.array[i].used, f_string_eol_s); } else if (data->option & fss_basic_list_read_data_option_total_d) { - flockfile(main->output.to.stream); + f_array_length_t total = 0; + f_array_length_t j = 0; + f_array_length_t k = 0; - if (data->contents.array[i].used) { - fss_basic_list_read_print_one(main); - } - else { - fss_basic_list_read_print_zero(main); + // Count each new line. + for (; j < data->contents.array[i].used; ++j) { + + if (data->contents.array[i].array[j].start > data->contents.array[i].array[j].stop) continue; + if (data->contents.array[i].array[j].start > data->buffer.used) continue; + + for (k = data->contents.array[i].array[j].start; k <= data->contents.array[i].array[j].stop && k < data->buffer.used; ++k) { + if (data->buffer.string[k] == f_string_eol_s.string[0]) ++total; + } // for + } // for + + // If there are no newline characters but there is data, then this represents a single line. + if (data->contents.array[i].used && !total) { + total = 1; } - funlockfile(main->output.to.stream); + fll_print_format("%ul%r", main->output.to.stream, total, f_string_eol_s); } else { fss_basic_list_read_print_at(main, i, *delimits_object, *delimits_content, data); -- 1.8.3.1