xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 34821)
133129Sbostic /*
233129Sbostic  * Copyright (c) 1987 Regents of the University of California.
333129Sbostic  * All rights reserved.
433129Sbostic  *
533129Sbostic  * Redistribution and use in source and binary forms are permitted
6*34821Sbostic  * provided that the above copyright notice and this paragraph are
7*34821Sbostic  * duplicated in all such forms and that any documentation,
8*34821Sbostic  * advertising materials, and other materials related to such
9*34821Sbostic  * distribution and use acknowledge that the software was developed
10*34821Sbostic  * by the University of California, Berkeley.  The name of the
11*34821Sbostic  * University may not be used to endorse or promote products derived
12*34821Sbostic  * from this software without specific prior written permission.
13*34821Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34821Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34821Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633129Sbostic  */
1733129Sbostic 
1826554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*34821Sbostic static char sccsid[] = "@(#)getenv.c	5.5 (Berkeley) 06/27/88";
2033129Sbostic #endif /* LIBC_SCCS and not lint */
2122090Smckusick 
2230607Sbostic #include <stdio.h>
2330607Sbostic 
241967Swnj /*
2533129Sbostic  * getenv --
2630607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
271967Swnj  */
281967Swnj char *
291967Swnj getenv(name)
3030607Sbostic 	char *name;
311967Swnj {
3233129Sbostic 	int offset;
3333129Sbostic 	char *_findenv();
341967Swnj 
3533129Sbostic 	return(_findenv(name, &offset));
361967Swnj }
371967Swnj 
381967Swnj /*
3933129Sbostic  * _findenv --
4030607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
4130607Sbostic  *	Sets offset to be the offset of the name/value combination in the
4230607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
4330607Sbostic  *	Explicitly removes '=' in argument name.
4430607Sbostic  *
4530607Sbostic  *	This routine *should* be a static; don't use it.
461967Swnj  */
4730607Sbostic char *
4833129Sbostic _findenv(name, offset)
4930607Sbostic 	register char *name;
5033129Sbostic 	int *offset;
511967Swnj {
5233129Sbostic 	extern char **environ;
5333129Sbostic 	register int len;
5433129Sbostic 	register char **P, *C;
551967Swnj 
5633129Sbostic 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
5733129Sbostic 	for (P = environ; *P; ++P)
5833129Sbostic 		if (!strncmp(*P, name, len))
5930607Sbostic 			if (*(C = *P + len) == '=') {
6030607Sbostic 				*offset = P - environ;
6130607Sbostic 				return(++C);
6230607Sbostic 			}
631967Swnj 	return(NULL);
641967Swnj }
65