aboutsummaryrefslogtreecommitdiffstats
path: root/src/word.c
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-07-03 21:20:32 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2024-07-03 21:20:32 -0400
commit7b3cb7088d59fca697b04832737e6a6d06d00ea6 (patch)
tree731b959726dac8f2cc51ff38e96c0909d233d46f /src/word.c
downloadcli-7b3cb7088d59fca697b04832737e6a6d06d00ea6.zip
Initial Commit
Diffstat (limited to 'src/word.c')
-rw-r--r--src/word.c29
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;
+}
+