blob: e46aedd9118066ce751ae5d02b1d5318c0951287 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
#include "word.h"
#include <ctype.h>
int read_word(FILE *fp, char *word, size_t length) {
if (fp == NULL || word == NULL) {
return READ_WORD_ERR_READ;
}
int ch;
size_t position = 0;
while (isspace(ch = getc(fp)))
;
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. 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;
position++;
ch = getc(fp);
}
if (length >= 0) {
word[position] = '\0';
}
if (ferror(fp)) {
return READ_WORD_ERR_READ;
}
if (position == 0 && ch == EOF) {
return READ_WORD_ERR_EOF;
}
return READ_WORD_SUCCESS;
}
|