xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 42634)
133129Sbostic /*
233129Sbostic  * Copyright (c) 1987 Regents of the University of California.
333129Sbostic  * All rights reserved.
433129Sbostic  *
5*42634Sbostic  * %sccs.include.redist.c%
633129Sbostic  */
733129Sbostic 
826554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42634Sbostic static char sccsid[] = "@(#)getenv.c	5.7 (Berkeley) 06/01/90";
1033129Sbostic #endif /* LIBC_SCCS and not lint */
1122090Smckusick 
1242120Sbostic #include <stdlib.h>
1342120Sbostic #include <stddef.h>
1430607Sbostic 
151967Swnj /*
1633129Sbostic  * getenv --
1730607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
181967Swnj  */
191967Swnj char *
201967Swnj getenv(name)
2130607Sbostic 	char *name;
221967Swnj {
2333129Sbostic 	int offset;
2433129Sbostic 	char *_findenv();
251967Swnj 
2633129Sbostic 	return(_findenv(name, &offset));
271967Swnj }
281967Swnj 
291967Swnj /*
3033129Sbostic  * _findenv --
3130607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
3230607Sbostic  *	Sets offset to be the offset of the name/value combination in the
3330607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
3430607Sbostic  *	Explicitly removes '=' in argument name.
3530607Sbostic  *
3630607Sbostic  *	This routine *should* be a static; don't use it.
371967Swnj  */
3830607Sbostic char *
3933129Sbostic _findenv(name, offset)
4030607Sbostic 	register char *name;
4133129Sbostic 	int *offset;
421967Swnj {
4333129Sbostic 	extern char **environ;
4433129Sbostic 	register int len;
4533129Sbostic 	register char **P, *C;
461967Swnj 
4733129Sbostic 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
4833129Sbostic 	for (P = environ; *P; ++P)
4933129Sbostic 		if (!strncmp(*P, name, len))
5030607Sbostic 			if (*(C = *P + len) == '=') {
5130607Sbostic 				*offset = P - environ;
5230607Sbostic 				return(++C);
5330607Sbostic 			}
541967Swnj 	return(NULL);
551967Swnj }
56