xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 33129)
1*33129Sbostic /*
2*33129Sbostic  * Copyright (c) 1987 Regents of the University of California.
3*33129Sbostic  * All rights reserved.
4*33129Sbostic  *
5*33129Sbostic  * Redistribution and use in source and binary forms are permitted
6*33129Sbostic  * provided that this notice is preserved and that due credit is given
7*33129Sbostic  * to the University of California at Berkeley. The name of the University
8*33129Sbostic  * may not be used to endorse or promote products derived from this
9*33129Sbostic  * software without specific prior written permission. This software
10*33129Sbostic  * is provided ``as is'' without express or implied warranty.
11*33129Sbostic  */
12*33129Sbostic 
1326554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
14*33129Sbostic static char sccsid[] = "@(#)getenv.c	5.4 (Berkeley) 12/24/87";
15*33129Sbostic #endif /* LIBC_SCCS and not lint */
1622090Smckusick 
1730607Sbostic #include <stdio.h>
1830607Sbostic 
191967Swnj /*
20*33129Sbostic  * getenv --
2130607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
221967Swnj  */
231967Swnj char *
241967Swnj getenv(name)
2530607Sbostic 	char *name;
261967Swnj {
27*33129Sbostic 	int offset;
28*33129Sbostic 	char *_findenv();
291967Swnj 
30*33129Sbostic 	return(_findenv(name, &offset));
311967Swnj }
321967Swnj 
331967Swnj /*
34*33129Sbostic  * _findenv --
3530607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
3630607Sbostic  *	Sets offset to be the offset of the name/value combination in the
3730607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
3830607Sbostic  *	Explicitly removes '=' in argument name.
3930607Sbostic  *
4030607Sbostic  *	This routine *should* be a static; don't use it.
411967Swnj  */
4230607Sbostic char *
43*33129Sbostic _findenv(name, offset)
4430607Sbostic 	register char *name;
45*33129Sbostic 	int *offset;
461967Swnj {
47*33129Sbostic 	extern char **environ;
48*33129Sbostic 	register int len;
49*33129Sbostic 	register char **P, *C;
501967Swnj 
51*33129Sbostic 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
52*33129Sbostic 	for (P = environ; *P; ++P)
53*33129Sbostic 		if (!strncmp(*P, name, len))
5430607Sbostic 			if (*(C = *P + len) == '=') {
5530607Sbostic 				*offset = P - environ;
5630607Sbostic 				return(++C);
5730607Sbostic 			}
581967Swnj 	return(NULL);
591967Swnj }
60