diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-07-03 21:20:32 -0400 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-07-03 21:20:32 -0400 |
| commit | 7b3cb7088d59fca697b04832737e6a6d06d00ea6 (patch) | |
| tree | 731b959726dac8f2cc51ff38e96c0909d233d46f /src/word.c | |
| download | cli-7b3cb7088d59fca697b04832737e6a6d06d00ea6.zip | |
Initial Commit
Diffstat (limited to 'src/word.c')
| -rw-r--r-- | src/word.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/word.c b/src/word.c new file mode 100644 index 0000000..f10a648 --- /dev/null +++ b/src/word.c @@ -0,0 +1,29 @@ +#include "word.h" + +static int read_char(FILE *fp); + +int read_word(FILE *fp, char *word, size_t length) { + int ch; + size_t position = 0; + + while ((ch = read_char(fp)) == ' ') + ; + + while (ch != ' ' && ch != EOF) { + if (position < length) { + word[position++] = ch; + } + ch = read_char(fp); + } + word[position] = '\0'; + return ch; +} + +static inline int read_char(FILE *fp) { + int ch = getc(fp); + if (ch == '\n' || ch == '\t') { + return ' '; + } + return ch; +} + |