freq

count frequencies of keys in text
git clone git://bvnf.space/freq.git
Log | Files | Refs

commit 7c83e66679339067eb36124cd613203d5c0e11f2
parent d5a2646377301d4a00a76bd46f3b0da09183a452
Author: phoebos <ben@bvnf.space>
Date:   Mon, 21 Mar 2022 19:46:19 +0000

properly check for empty fields

Diffstat:
Mfreq.c | 13++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/freq.c b/freq.c @@ -100,6 +100,10 @@ freq(FILE *f, int field) { linenum = 0; while ((n = getline(&line, &linelen, f)) != -1) { + if (line[n-1] == '\n') { + line[n-1] = '\0'; /* remove the '\n' */ + n--; + } linenum++; i = 0; field_start = line; @@ -115,9 +119,8 @@ freq(FILE *f, int field) { line[i] = '\0'; goto field_found; } - if (i < n - 1) - /* the last byte is probably '\n' and there is no next field */ - cur_field++; + field_start = (char *)-1; + cur_field++; in_word = 0; } } else { @@ -131,6 +134,10 @@ freq(FILE *f, int field) { fprintf(stderr, "freq: warning: field %d not found on line %d\n", field, linenum); continue; } + if (field_start == (char *)-1) { + fprintf(stderr, "freq: warning: empty field %d on line %d\n", field, linenum); + continue; + } field_found: //fprintf(stderr, "found '%s'\n", field_start); update_record(field_start);