xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 30607)
126554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
2*30607Sbostic static char sccsid[] = "@(#)getenv.c	5.3 (Berkeley) 03/11/87";
326554Sdonn #endif LIBC_SCCS and not lint
422090Smckusick 
5*30607Sbostic #include <stdio.h>
6*30607Sbostic 
71967Swnj /*
8*30607Sbostic  * getenv(name) --
9*30607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
101967Swnj  */
111967Swnj char *
121967Swnj getenv(name)
13*30607Sbostic 	char *name;
141967Swnj {
15*30607Sbostic 	int	offset;
16*30607Sbostic 	char	*_findenv();
171967Swnj 
18*30607Sbostic 	return(_findenv(name,&offset));
191967Swnj }
201967Swnj 
211967Swnj /*
22*30607Sbostic  * _findenv(name,offset) --
23*30607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
24*30607Sbostic  *	Sets offset to be the offset of the name/value combination in the
25*30607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
26*30607Sbostic  *	Explicitly removes '=' in argument name.
27*30607Sbostic  *
28*30607Sbostic  *	This routine *should* be a static; don't use it.
291967Swnj  */
30*30607Sbostic char *
31*30607Sbostic _findenv(name,offset)
32*30607Sbostic 	register char *name;
33*30607Sbostic 	int	*offset;
341967Swnj {
35*30607Sbostic 	extern char	**environ;
36*30607Sbostic 	register int	len;
37*30607Sbostic 	register char	**P,
38*30607Sbostic 			*C;
391967Swnj 
40*30607Sbostic 	for (C = name,len = 0;*C && *C != '=';++C,++len);
41*30607Sbostic 	for (P = environ;*P;++P)
42*30607Sbostic 		if (!strncmp(*P,name,len))
43*30607Sbostic 			if (*(C = *P + len) == '=') {
44*30607Sbostic 				*offset = P - environ;
45*30607Sbostic 				return(++C);
46*30607Sbostic 			}
471967Swnj 	return(NULL);
481967Swnj }
49