aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-07-04 14:26:41 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2024-07-04 14:26:41 -0400
commit4a7c1b3aa8345d1c99186b6a3410f249d49c5223 (patch)
tree9d8c7f6dabc4b459fc338edbd9addbabb4a3a97b /src/main.c
parentTweak tests and read_word impl (diff)
downloadcli-4a7c1b3aa8345d1c99186b6a3410f249d49c5223.zip
clear word after appending
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index 5fdc535..7b30345 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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;
}