diff options
author | 2014-12-08 20:40:53 +0000 | |
---|---|---|
committer | 2014-12-08 20:40:53 +0000 | |
commit | c9847b05e23e4c141f7dd401a3db932d779189e7 (patch) | |
tree | bcd0e46411b12d699d55e3af2210d75df514d992 /lib/libc/stdio/fread.c | |
parent | header changes for recent libc changes: (diff) | |
download | wireguard-openbsd-c9847b05e23e4c141f7dd401a3db932d779189e7.tar.xz wireguard-openbsd-c9847b05e23e4c141f7dd401a3db932d779189e7.zip |
don't do silly (and slow) one byte reads in unbuffered mode.
from enh at google
Diffstat (limited to 'lib/libc/stdio/fread.c')
-rw-r--r-- | lib/libc/stdio/fread.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/lib/libc/stdio/fread.c b/lib/libc/stdio/fread.c index 8a592f6d3f1..6e957623e1f 100644 --- a/lib/libc/stdio/fread.c +++ b/lib/libc/stdio/fread.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fread.c,v 1.12 2014/05/01 16:40:36 deraadt Exp $ */ +/* $OpenBSD: fread.c,v 1.13 2014/12/08 20:40:53 tedu Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. @@ -68,6 +68,21 @@ fread(void *buf, size_t size, size_t count, FILE *fp) fp->_r = 0; total = resid; p = buf; + + if ((fp->_flags & __SNBF) != 0) { + /* + * We know if we're unbuffered that our buffer is empty, so + * we can just read directly. This is much faster than the + * loop below which will perform a series of one byte reads. + */ + while (resid > 0 && (r = (*fp->_read)(fp->_cookie, p, resid)) > 0) { + p += r; + resid -= r; + } + FUNLOCKFILE(fp); + return ((total - resid) / size); + } + while (resid > (r = fp->_r)) { (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); fp->_p += r; |