xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/mks/m_getenv.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * MKS interface extension.
31  * A version of getenv() that doesn't overwrite it's return value
32  * on each call.
33  *
34  * Copyright 1995 by Mortice Kern Systems Inc.  All rights reserved.
35  *
36  */
37 
38 #ifdef M_RCSID
39 #ifndef lint
40 static char const rcsID[] = "$Header: /rd/src/libc/mks/rcs/m_getenv.c 1.2 1995/07/11 16:53:01 ross Exp $";
41 #endif /*lint*/
42 #endif /*M_RCSID*/
43 
44 #include <mks.h>
45 #include <stdlib.h>
46 #include <string.h>
47 
48 #ifdef M_NON_STATIC_GETENV
49 
50 #undef __m_getenv
51 
52 /*f
53  *  Assume getenv() works the way we expect it to on PC systems.
54  */
55 char *
56 __m_getenv(char const *name) {
57 	return getenv(name);
58 }
59 
60 #else /* M_NON_STATIC_GETENV */
61 
62 extern char **environ;
63 
64 /*f
65  *  A version of getenv safe to use in library functions.  According to
66  *  ANSI C and XPG 4 no library function shall behave as if it called
67  *  getenv.  This is a problem on systems that have getenv functions
68  *  that overwrite their return value on each call.
69  */
70 
71 char *
72 __m_getenv(char const *name) {
73 	if (m_setenv() != NULL) {
74 		int len = strlen(name);
75 		char **envp = environ;
76 		char *s = *envp++;
77 
78 		while(s != NULL) {
79 			if (strncmp(name, s, len) == 0 && s[len] == '=') {
80 				return s + len + 1;
81 			}
82 			s = *envp++;
83 		}
84 	}
85 	return NULL;
86 }
87 
88 #endif /* M_NON_STATIC_GETENV */
89