summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@gmail.com>2009-10-27 17:08:31 -0400
committerTavian Barnes <tavianator@gmail.com>2009-10-27 17:08:31 -0400
commit9d093988213e7471a5d8ba6042e16c8d2ab5f053 (patch)
tree0d1e893ea8f3e3e0ee5e7a2cc9ebe67cd137fff4
parentf118c11876d6b6501d11763a839a20cf1a5cb1a7 (diff)
downloaddimension-9d093988213e7471a5d8ba6042e16c8d2ab5f053.tar.xz
Make tokenizer a bit more robust.
-rw-r--r--dimension/tokenize.c22
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;