xref: /original-bsd/lib/libc/stdlib/getenv.c (revision 241757c4)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #if defined(LIBC_SCCS) && !defined(lint)
14 static char sccsid[] = "@(#)getenv.c	5.4 (Berkeley) 12/24/87";
15 #endif /* LIBC_SCCS and not lint */
16 
17 #include <stdio.h>
18 
19 /*
20  * getenv --
21  *	Returns ptr to value associated with name, if any, else NULL.
22  */
23 char *
24 getenv(name)
25 	char *name;
26 {
27 	int offset;
28 	char *_findenv();
29 
30 	return(_findenv(name, &offset));
31 }
32 
33 /*
34  * _findenv --
35  *	Returns pointer to value associated with name, if any, else NULL.
36  *	Sets offset to be the offset of the name/value combination in the
37  *	environmental array, for use by setenv(3) and unsetenv(3).
38  *	Explicitly removes '=' in argument name.
39  *
40  *	This routine *should* be a static; don't use it.
41  */
42 char *
43 _findenv(name, offset)
44 	register char *name;
45 	int *offset;
46 {
47 	extern char **environ;
48 	register int len;
49 	register char **P, *C;
50 
51 	for (C = name, len = 0; *C && *C != '='; ++C, ++len);
52 	for (P = environ; *P; ++P)
53 		if (!strncmp(*P, name, len))
54 			if (*(C = *P + len) == '=') {
55 				*offset = P - environ;
56 				return(++C);
57 			}
58 	return(NULL);
59 }
60