xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 46599)
133129Sbostic /*
233129Sbostic  * Copyright (c) 1987 Regents of the University of California.
333129Sbostic  * All rights reserved.
433129Sbostic  *
542634Sbostic  * %sccs.include.redist.c%
633129Sbostic  */
733129Sbostic 
826554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*46599Sdonn static char sccsid[] = "@(#)getenv.c	5.8 (Berkeley) 02/23/91";
1033129Sbostic #endif /* LIBC_SCCS and not lint */
1122090Smckusick 
1242120Sbostic #include <stdlib.h>
1342120Sbostic #include <stddef.h>
14*46599Sdonn #include <string.h>
1530607Sbostic 
161967Swnj /*
1733129Sbostic  * getenv --
1830607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
191967Swnj  */
201967Swnj char *
211967Swnj getenv(name)
22*46599Sdonn 	const char *name;
231967Swnj {
2433129Sbostic 	int offset;
2533129Sbostic 	char *_findenv();
261967Swnj 
2733129Sbostic 	return(_findenv(name, &offset));
281967Swnj }
291967Swnj 
301967Swnj /*
3133129Sbostic  * _findenv --
3230607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
3330607Sbostic  *	Sets offset to be the offset of the name/value combination in the
3430607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
3530607Sbostic  *	Explicitly removes '=' in argument name.
3630607Sbostic  *
3730607Sbostic  *	This routine *should* be a static; don't use it.
381967Swnj  */
3930607Sbostic char *
4033129Sbostic _findenv(name, offset)
4130607Sbostic 	register char *name;
4233129Sbostic 	int *offset;
431967Swnj {
4433129Sbostic 	extern char **environ;
4533129Sbostic 	register int len;
4633129Sbostic 	register char **P, *C;
471967Swnj 
4833129Sbostic 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
4933129Sbostic 	for (P = environ; *P; ++P)
5033129Sbostic 		if (!strncmp(*P, name, len))
5130607Sbostic 			if (*(C = *P + len) == '=') {
5230607Sbostic 				*offset = P - environ;
5330607Sbostic 				return(++C);
5430607Sbostic 			}
551967Swnj 	return(NULL);
561967Swnj }
57