xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 42120)
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
634821Sbostic  * provided that the above copyright notice and this paragraph are
734821Sbostic  * duplicated in all such forms and that any documentation,
834821Sbostic  * advertising materials, and other materials related to such
934821Sbostic  * distribution and use acknowledge that the software was developed
1034821Sbostic  * by the University of California, Berkeley.  The name of the
1134821Sbostic  * University may not be used to endorse or promote products derived
1234821Sbostic  * from this software without specific prior written permission.
1334821Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434821Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534821Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1633129Sbostic  */
1733129Sbostic 
1826554Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*42120Sbostic static char sccsid[] = "@(#)getenv.c	5.6 (Berkeley) 05/16/90";
2033129Sbostic #endif /* LIBC_SCCS and not lint */
2122090Smckusick 
22*42120Sbostic #include <stdlib.h>
23*42120Sbostic #include <stddef.h>
2430607Sbostic 
251967Swnj /*
2633129Sbostic  * getenv --
2730607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
281967Swnj  */
291967Swnj char *
301967Swnj getenv(name)
3130607Sbostic 	char *name;
321967Swnj {
3333129Sbostic 	int offset;
3433129Sbostic 	char *_findenv();
351967Swnj 
3633129Sbostic 	return(_findenv(name, &offset));
371967Swnj }
381967Swnj 
391967Swnj /*
4033129Sbostic  * _findenv --
4130607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
4230607Sbostic  *	Sets offset to be the offset of the name/value combination in the
4330607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
4430607Sbostic  *	Explicitly removes '=' in argument name.
4530607Sbostic  *
4630607Sbostic  *	This routine *should* be a static; don't use it.
471967Swnj  */
4830607Sbostic char *
4933129Sbostic _findenv(name, offset)
5030607Sbostic 	register char *name;
5133129Sbostic 	int *offset;
521967Swnj {
5333129Sbostic 	extern char **environ;
5433129Sbostic 	register int len;
5533129Sbostic 	register char **P, *C;
561967Swnj 
5733129Sbostic 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
5833129Sbostic 	for (P = environ; *P; ++P)
5933129Sbostic 		if (!strncmp(*P, name, len))
6030607Sbostic 			if (*(C = *P + len) == '=') {
6130607Sbostic 				*offset = P - environ;
6230607Sbostic 				return(++C);
6330607Sbostic 			}
641967Swnj 	return(NULL);
651967Swnj }
66