diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-07-04 14:26:41 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-07-04 14:26:41 -0400 |
| commit | 4a7c1b3aa8345d1c99186b6a3410f249d49c5223 (patch) | |
| tree | 9d8c7f6dabc4b459fc338edbd9addbabb4a3a97b /src/main.c | |
| parent | Tweak tests and read_word impl (diff) | |
| download | cli-4a7c1b3aa8345d1c99186b6a3410f249d49c5223.zip | |
clear word after appending
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 22 |
1 files changed, 13 insertions, 9 deletions
@@ -6,31 +6,35 @@ #include "word.h" #define MAX_WORD_LENGTH 20 +#define LINEBUFFER_CAPACITY 60 int main(void) { char word[MAX_WORD_LENGTH + 2]; size_t word_length; - int err; - LineBuffer *lb = linebuffer_create(40); + int read_word_result; + LineBuffer *lb = linebuffer_create(LINEBUFFER_CAPACITY); - while (true) { - err = read_word(stdin, word, MAX_WORD_LENGTH + 1); - if (err == EOF) { - printf("Error reading word\n"); - return 1; - } + while ((read_word_result = read_word(stdin, word, MAX_WORD_LENGTH + 1)) == + READ_WORD_SUCCESS) { word_length = strlen(word); if (word_length == 0) { linebuffer_write(lb, stdout); + linebuffer_destroy(lb); return 0; } if (word_length > MAX_WORD_LENGTH) { word[MAX_WORD_LENGTH] = '*'; } - if (word_length + 1 > linebuffer_space_remaining(lb)) { + if (word_length >= linebuffer_space_remaining(lb)) { linebuffer_write_justified(lb, stdout); linebuffer_clear(lb); } linebuffer_append_word(lb, word); + memset(word, 0, MAX_WORD_LENGTH + 2); + } + if (read_word_result == READ_WORD_ERR_READ) { + printf("Error reading word\n"); + return 1; } + return 0; } |