xref: /original-bsd/lib/libc/stdlib/getenv.c (revision e22fe102)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getenv.c	5.7 (Berkeley) 06/01/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <stdlib.h>
13 #include <stddef.h>
14 
15 /*
16  * getenv --
17  *	Returns ptr to value associated with name, if any, else NULL.
18  */
19 char *
20 getenv(name)
21 	char *name;
22 {
23 	int offset;
24 	char *_findenv();
25 
26 	return(_findenv(name, &offset));
27 }
28 
29 /*
30  * _findenv --
31  *	Returns pointer to value associated with name, if any, else NULL.
32  *	Sets offset to be the offset of the name/value combination in the
33  *	environmental array, for use by setenv(3) and unsetenv(3).
34  *	Explicitly removes '=' in argument name.
35  *
36  *	This routine *should* be a static; don't use it.
37  */
38 char *
39 _findenv(name, offset)
40 	register char *name;
41 	int *offset;
42 {
43 	extern char **environ;
44 	register int len;
45 	register char **P, *C;
46 
47 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
48 	for (P = environ; *P; ++P)
49 		if (!strncmp(*P, name, len))
50 			if (*(C = *P + len) == '=') {
51 				*offset = P - environ;
52 				return(++C);
53 			}
54 	return(NULL);
55 }
56