[PATCH] Speed up gets
Corinna Vinschen
vinschen@redhat.com
Mon Nov 24 19:08:00 GMT 2008
Hi,
I'd like to propose the below patch. The current code implements
gets/_gets_r in terms of _getchar_r calls. That means, for every single
character of input the following sequence of functions is called:
for each char
_getchar_r
_getc_r
_flockfile
_sgetc_r
_funlockfile
It should be much quicker to move the locking up into gets itself and to
call __sgetc_r right there, as FreeBSD does as well:
_flockfile
for each char
_sgetc_r
_funlockfile
That doesn't change the fact that gets is inherently unsecure, of
course :)
Ok to apply?
Thanks,
Corinna
* libc/stdio/gets.c (_gets_r): Lock stdin here and call
__sgetc_r instead of _getchar_r.
Index: libc/stdio/gets.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/stdio/gets.c,v
retrieving revision 1.2
diff -u -p -r1.2 gets.c
--- libc/stdio/gets.c 23 Apr 2004 20:01:55 -0000 1.2
+++ libc/stdio/gets.c 24 Nov 2008 17:25:27 -0000
@@ -79,15 +79,20 @@ _DEFUN(_gets_r, (ptr, buf),
register int c;
register char *s = buf;
- while ((c = _getchar_r (ptr)) != '\n')
+ _flockfile (stdin);
+ while ((c = __sgetc_r (ptr, stdin)) != '\n')
if (c == EOF)
if (s == buf)
- return NULL;
+ {
+ _funlockfile (stdin);
+ return NULL;
+ }
else
break;
else
*s++ = c;
*s = 0;
+ _funlockfile (stdin);
return buf;
}
--
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat
More information about the Newlib
mailing list