xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 61180)
133129Sbostic /*
2*61180Sbostic  * Copyright (c) 1987, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
433129Sbostic  *
542634Sbostic  * %sccs.include.redist.c%
633129Sbostic  */
733129Sbostic 
826554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*61180Sbostic static char sccsid[] = "@(#)getenv.c	8.1 (Berkeley) 06/04/93";
1033129Sbostic #endif /* LIBC_SCCS and not lint */
1122090Smckusick 
1242120Sbostic #include <stdlib.h>
1342120Sbostic #include <stddef.h>
1446599Sdonn #include <string.h>
1530607Sbostic 
1654427Sbostic char *__findenv __P((const char *, int *));
1754427Sbostic 
181967Swnj /*
1933129Sbostic  * getenv --
2030607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
211967Swnj  */
221967Swnj char *
getenv(name)231967Swnj getenv(name)
2446599Sdonn 	const char *name;
251967Swnj {
2633129Sbostic 	int offset;
271967Swnj 
2854427Sbostic 	return (__findenv(name, &offset));
291967Swnj }
301967Swnj 
311967Swnj /*
3254427Sbostic  * __findenv --
3330607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
3430607Sbostic  *	Sets offset to be the offset of the name/value combination in the
3530607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
3630607Sbostic  *	Explicitly removes '=' in argument name.
3730607Sbostic  *
3830607Sbostic  *	This routine *should* be a static; don't use it.
391967Swnj  */
4030607Sbostic char *
__findenv(name,offset)4154427Sbostic __findenv(name, offset)
4254427Sbostic 	register const char *name;
4333129Sbostic 	int *offset;
441967Swnj {
4533129Sbostic 	extern char **environ;
4633129Sbostic 	register int len;
4754427Sbostic 	register const char *np;
4854427Sbostic 	register char **p, *c;
491967Swnj 
5054427Sbostic 	if (name == NULL || environ == NULL)
5154427Sbostic 		return (NULL);
5254427Sbostic 	for (np = name; *np && *np != '='; ++np)
5354427Sbostic 		continue;
5454427Sbostic 	len = np - name;
5554427Sbostic 	for (p = environ; (c = *p) != NULL; ++p)
5654427Sbostic 		if (strncmp(c, name, len) == 0 && c[len] == '=') {
5754427Sbostic 			*offset = p - environ;
5854443Sbostic 			return (c + len + 1);
5954427Sbostic 		}
6054427Sbostic 	return (NULL);
611967Swnj }
62