xref: /csrg-svn/lib/libc/stdlib/getenv.c (revision 54427)
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*54427Sbostic static char sccsid[] = "@(#)getenv.c	5.10 (Berkeley) 06/25/92";
1033129Sbostic #endif /* LIBC_SCCS and not lint */
1122090Smckusick 
1242120Sbostic #include <stdlib.h>
1342120Sbostic #include <stddef.h>
1446599Sdonn #include <string.h>
1530607Sbostic 
16*54427Sbostic char *__findenv __P((const char *, int *));
17*54427Sbostic 
181967Swnj /*
1933129Sbostic  * getenv --
2030607Sbostic  *	Returns ptr to value associated with name, if any, else NULL.
211967Swnj  */
221967Swnj char *
231967Swnj getenv(name)
2446599Sdonn 	const char *name;
251967Swnj {
2633129Sbostic 	int offset;
271967Swnj 
28*54427Sbostic 	return (__findenv(name, &offset));
291967Swnj }
301967Swnj 
311967Swnj /*
32*54427Sbostic  * __findenv --
3330607Sbostic  *	Returns pointer to value associated with name, if any, else NULL.
3430607Sbostic  *	Sets offset to be the offset of the name/value combination in the
3530607Sbostic  *	environmental array, for use by setenv(3) and unsetenv(3).
3630607Sbostic  *	Explicitly removes '=' in argument name.
3730607Sbostic  *
3830607Sbostic  *	This routine *should* be a static; don't use it.
391967Swnj  */
4030607Sbostic char *
41*54427Sbostic __findenv(name, offset)
42*54427Sbostic 	register const char *name;
4333129Sbostic 	int *offset;
441967Swnj {
4533129Sbostic 	extern char **environ;
4633129Sbostic 	register int len;
47*54427Sbostic 	register const char *np;
48*54427Sbostic 	register char **p, *c;
491967Swnj 
50*54427Sbostic 	if (name == NULL || environ == NULL)
51*54427Sbostic 		return (NULL);
52*54427Sbostic 	for (np = name; *np && *np != '='; ++np)
53*54427Sbostic 		continue;
54*54427Sbostic 	len = np - name;
55*54427Sbostic 	for (p = environ; (c = *p) != NULL; ++p)
56*54427Sbostic 		if (strncmp(c, name, len) == 0 && c[len] == '=') {
57*54427Sbostic 			*offset = p - environ;
58*54427Sbostic 			return (c + 1);
59*54427Sbostic 		}
60*54427Sbostic 	return (NULL);
611967Swnj }
62