1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1996, by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 /*
28  * MKS interface extension.
29  * A version of getenv() that doesn't overwrite it's return value
30  * on each call.
31  *
32  * Copyright 1995 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #ifdef M_RCSID
37 #ifndef lint
38 static char const rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_getenv.c 1.2 1995/07/11 16:53:01 ross Exp $";
39 #endif /*lint*/
40 #endif /*M_RCSID*/
41 
42 #include <mks.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #ifdef M_NON_STATIC_GETENV
47 
48 #undef __m_getenv
49 
50 /*f
51  *  Assume getenv() works the way we expect it to on PC systems.
52  */
53 char *
__m_getenv(char const * name)54 __m_getenv(char const *name) {
55 	return getenv(name);
56 }
57 
58 #else /* M_NON_STATIC_GETENV */
59 
60 extern char **environ;
61 
62 /*f
63  *  A version of getenv safe to use in library functions.  According to
64  *  ANSI C and XPG 4 no library function shall behave as if it called
65  *  getenv.  This is a problem on systems that have getenv functions
66  *  that overwrite their return value on each call.
67  */
68 
69 char *
__m_getenv(char const * name)70 __m_getenv(char const *name) {
71 	if (m_setenv() != NULL) {
72 		int len = strlen(name);
73 		char **envp = environ;
74 		char *s = *envp++;
75 
76 		while(s != NULL) {
77 			if (strncmp(name, s, len) == 0 && s[len] == '=') {
78 				return s + len + 1;
79 			}
80 			s = *envp++;
81 		}
82 	}
83 	return NULL;
84 }
85 
86 #endif /* M_NON_STATIC_GETENV */
87