diff options
author | Tavian Barnes <tavianator@gmail.com> | 2009-10-27 17:08:31 -0400 |
---|---|---|
committer | Tavian Barnes <tavianator@gmail.com> | 2009-10-27 17:08:31 -0400 |
commit | 9d093988213e7471a5d8ba6042e16c8d2ab5f053 (patch) | |
tree | 0d1e893ea8f3e3e0ee5e7a2cc9ebe67cd137fff4 /dimension | |
parent | f118c11876d6b6501d11763a839a20cf1a5cb1a7 (diff) | |
download | dimension-9d093988213e7471a5d8ba6042e16c8d2ab5f053.tar.xz |
Make tokenizer a bit more robust.
Diffstat (limited to 'dimension')
-rw-r--r-- | dimension/tokenize.c | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/dimension/tokenize.c b/dimension/tokenize.c index fc715b8..e1c2fce 100644 --- a/dimension/tokenize.c +++ b/dimension/tokenize.c @@ -27,11 +27,26 @@ dmnsn_array * dmnsn_tokenize(FILE *file) { + if (fseeko(file, 0, SEEK_END) != 0) { + fprintf(stderr, "Couldn't seek on input stream.\n"); + return NULL; + } + + off_t size = ftello(file); + int fd = fileno(file); - off_t size = lseek(fd, 0, SEEK_END); - lseek(fd, 0, SEEK_SET); + if (fd == -1) { + fprintf(stderr, "Couldn't get file descriptor to input stream.\n"); + return NULL; + } + char *map = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0), *next = map; + if (map == MAP_FAILED) { + fprintf(stderr, "Couldn't mmap() input stream.\n"); + return NULL; + } + dmnsn_token token; dmnsn_array *tokens = dmnsn_new_array(sizeof(dmnsn_token)); @@ -60,7 +75,8 @@ dmnsn_tokenize(FILE *file) default: /* Unrecognised character */ - fprintf(stderr, "Unrecognized character 0x%X in input.\n", (unsigned int)*next); + fprintf(stderr, "Unrecognized character 0x%X in input.\n", + (unsigned int)*next); dmnsn_delete_tokens(tokens); munmap(map, size); return NULL; |