xref: /original-bsd/lib/libc/stdlib/getenv.c (revision 05598ce3)
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.8 (Berkeley) 02/23/91";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <stdlib.h>
13 #include <stddef.h>
14 #include <string.h>
15 
16 /*
17  * getenv --
18  *	Returns ptr to value associated with name, if any, else NULL.
19  */
20 char *
21 getenv(name)
22 	const char *name;
23 {
24 	int offset;
25 	char *_findenv();
26 
27 	return(_findenv(name, &offset));
28 }
29 
30 /*
31  * _findenv --
32  *	Returns pointer to value associated with name, if any, else NULL.
33  *	Sets offset to be the offset of the name/value combination in the
34  *	environmental array, for use by setenv(3) and unsetenv(3).
35  *	Explicitly removes '=' in argument name.
36  *
37  *	This routine *should* be a static; don't use it.
38  */
39 char *
40 _findenv(name, offset)
41 	register char *name;
42 	int *offset;
43 {
44 	extern char **environ;
45 	register int len;
46 	register char **P, *C;
47 
48 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
49 	for (P = environ; *P; ++P)
50 		if (!strncmp(*P, name, len))
51 			if (*(C = *P + len) == '=') {
52 				*offset = P - environ;
53 				return(++C);
54 			}
55 	return(NULL);
56 }
57