diff options
author | dg <dg@FreeBSD.org> | 1995-10-17 21:37:41 +0000 |
---|---|---|
committer | dg <dg@FreeBSD.org> | 1995-10-17 21:37:41 +0000 |
commit | 43c7f23817808b32fa67162c25ed389f07d46b3e (patch) | |
tree | 7185dfa7bcb362d68b172d02199ec3c21aa3e246 /lib/libc/stdlib/getenv.c | |
parent | 73c1a1fccb4b8ea01172f33f00609d80ee244ee2 (diff) | |
download | FreeBSD-src-43c7f23817808b32fa67162c25ed389f07d46b3e.zip FreeBSD-src-43c7f23817808b32fa67162c25ed389f07d46b3e.tar.gz |
Doubled the performance of getenv()/__findenv() by rewriting it to not
use strncmp()..
Diffstat (limited to 'lib/libc/stdlib/getenv.c')
-rw-r--r-- | lib/libc/stdlib/getenv.c | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/lib/libc/stdlib/getenv.c b/lib/libc/stdlib/getenv.c index 7407e0b..a6bbd35 100644 --- a/lib/libc/stdlib/getenv.c +++ b/lib/libc/stdlib/getenv.c @@ -39,20 +39,7 @@ static char sccsid[] = "@(#)getenv.c 8.1 (Berkeley) 6/4/93"; #include <stddef.h> #include <string.h> -char *__findenv __P((const char *, int *)); - -/* - * getenv -- - * Returns ptr to value associated with name, if any, else NULL. - */ -char * -getenv(name) - const char *name; -{ - int offset; - - return (__findenv(name, &offset)); -} +inline char *__findenv __P((const char *, int *)); /* * __findenv -- @@ -63,25 +50,42 @@ getenv(name) * * This routine *should* be a static; don't use it. */ -char * +inline char * __findenv(name, offset) register const char *name; int *offset; { extern char **environ; - register int len; + register int len, i; register const char *np; - register char **p, *c; + register char **p, *cp; 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] == '=') { + 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 (c + len + 1); + return (cp); } + } return (NULL); } + +/* + * getenv -- + * Returns ptr to value associated with name, if any, else NULL. + */ +char * +getenv(name) + const char *name; +{ + int offset; + + return (__findenv(name, &offset)); +} |