From c88f36f55623124d09f48631974ca38aaec00057 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 4 Sep 2011 00:08:32 -0400 Subject: optimize seek function for memory streams --- src/stdio/open_memstream.c | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'src/stdio/open_memstream.c') diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c index 57737098..7fc16204 100644 --- a/src/stdio/open_memstream.c +++ b/src/stdio/open_memstream.c @@ -13,21 +13,12 @@ static off_t ms_seek(FILE *f, off_t off, int whence) { ssize_t base; struct cookie *c = f->cookie; - switch (whence) { - case SEEK_SET: - base = 0; - break; - case SEEK_CUR: - base = c->pos; - break; - case SEEK_END: - base = c->len; - break; - default: - fail: + if (whence>2U) { +fail: errno = EINVAL; return -1; } + base = (size_t [3]){0, c->pos, c->len}[whence]; if (off < -base || off > SSIZE_MAX-base) goto fail; return c->pos = base+off; } -- cgit v1.2.1