# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include "getword.h"
int getword (FILE *fp, char *buf, size_t size,
int first (char), int rest (char))
{
int c;
size_t i, max;
assert (fp && buf && size && first && rest);
max = size - 1;
/* search for beginning of word */
while ((c = getc (fp)) != EOF && first (c) == 0)
;
if (c == EOF)
return EOF;
/* read the word */
i = 0;
do {
/* store characters as long as this is possible */
if (i < max)
buf [i++] = c;
} while ((c = getc (fp)) != EOF && rest (c));
/* terminate string */
buf [i] = '\0';
/* push last character back */
if (c != EOF)
ungetc (c, fp);
return i;
}