diff options
Diffstat (limited to 'util/files.c')
-rw-r--r-- | util/files.c | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/util/files.c b/util/files.c new file mode 100644 index 0000000..0afa843 --- /dev/null +++ b/util/files.c @@ -0,0 +1,28 @@ +static int fpeekc(FILE *fp) { + int c = getc(fp); + if (c == EOF) + return c; + ungetc(c, fp); + return c; +} + +#define fnextc getc /* advance to the next character */ + +/* NOTE: Advances and returns # of characters advanced iff prefix is found. */ +static int fhasprefix(FILE *fp, const char *prefix) { + assert(*prefix); + long start = ftell(fp); + if (start == -1) + return 0; + const char *p = prefix; + while (*p) { + int c = getc(fp); + if (c != *p) { + /* wrong character / EOF */ + fseek(fp, start, SEEK_SET); + return 0; + } + p++; + } + return (int)(p - prefix); /* length of prefix */ +} |