From 283e0c0eb74236dc10d0ee0baac0e01b9181756d Mon Sep 17 00:00:00 2001 From: David Greenman Date: Tue, 17 Oct 1995 21:37:41 +0000 Subject: [PATCH] Doubled the performance of getenv()/__findenv() by rewriting it to not use strncmp().. --- lib/libc/stdlib/getenv.c | 70 +++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c index 7407e0b81749..a6bbd355d93f 100644 --- a/lib/libc/stdlib/getenv.c +++ b/lib/libc/stdlib/getenv.c @@ -39,7 +39,43 @@ static char sccsid[] = "@(#)getenv.c 8.1 (Berkeley) 6/4/93"; #include #include -char *__findenv __P((const char *, int *)); +inline char *__findenv __P((const char *, int *)); + +/* + * __findenv -- + * Returns pointer to value associated with name, if any, else NULL. + * Sets offset to be the offset of the name/value combination in the + * environmental array, for use by setenv(3) and unsetenv(3). + * Explicitly removes '=' in argument name. + * + * This routine *should* be a static; don't use it. + */ +inline char * +__findenv(name, offset) + register const char *name; + int *offset; +{ + extern char **environ; + register int len, i; + register const char *np; + register char **p, *cp; + + if (name == NULL || environ == NULL) + return (NULL); + for (np = name; *np && *np != '='; ++np) + continue; + len = np - name; + for (p = environ; (cp = *p) != NULL; ++p) { + for (np = name, i = len; i && *cp; i--) + if (*cp++ != *np++) + break; + if (i == 0 && *cp++ == '=') { + *offset = p - environ; + return (cp); + } + } + return (NULL); +} /* * getenv -- @@ -53,35 +89,3 @@ getenv(name) return (__findenv(name, &offset)); } - -/* - * __findenv -- - * Returns pointer to value associated with name, if any, else NULL. - * Sets offset to be the offset of the name/value combination in the - * environmental array, for use by setenv(3) and unsetenv(3). - * Explicitly removes '=' in argument name. - * - * This routine *should* be a static; don't use it. - */ -char * -__findenv(name, offset) - register const char *name; - int *offset; -{ - extern char **environ; - register int len; - register const char *np; - register char **p, *c; - - if (name == NULL || environ == NULL) - return (NULL); - for (np = name; *np && *np != '='; ++np) - continue; - len = np - name; - for (p = environ; (c = *p) != NULL; ++p) - if (strncmp(c, name, len) == 0 && c[len] == '=') { - *offset = p - environ; - return (c + len + 1); - } - return (NULL); -}