aboutsummaryrefslogtreecommitdiffstats
path: root/src/word.c
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2025-07-12 12:02:55 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2025-07-12 12:02:55 -0400
commitfc5666c693df9f0ab7a835db680b5bfbe2dffe92 (patch)
tree56895bb6261f37aa0609015b0515bd0ed56db9a6 /src/word.c
parentAdd explanation comment for strlcat (diff)
downloadcli-fc5666c693df9f0ab7a835db680b5bfbe2dffe92.zip
Fix word wrapping to next line when the word is longer than the buffer
Diffstat (limited to 'src/word.c')
-rw-r--r--src/word.c12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/word.c b/src/word.c
index 87643d6..e46aedd 100644
--- a/src/word.c
+++ b/src/word.c
@@ -16,11 +16,19 @@ int read_word(FILE *fp, char *word, size_t length) {
while (!isspace(ch) && ch != EOF) {
if (position >= length - 1) {
// Note: -1 to ensure we have space for the null terminator
+ //
// Once we hit the end of our available space, we stop reading,
- // even if we didn't read the whole word.
+ // even if we didn't read the whole word. However, we need to
+ // ensure we put back the character we just read so that it is
+ // retrieved next time we call read_word.
+ int result = ungetc(ch, fp);
+ if (result == EOF) {
+ return READ_WORD_ERR_READ;
+ }
break;
}
- word[position++] = ch;
+ word[position] = ch;
+ position++;
ch = getc(fp);
}
if (length >= 0) {