xref: /titanic_41/usr/src/lib/libast/common/misc/getenv.c (revision 32c22d57860198538fb6b8f261cb76ab26318d34)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #pragma prototyped
23 
24 #if _UWIN && __STDPP__
25 __STDPP__directive pragma pp:hide getenv
26 #endif
27 
28 #include "intercepts.h"
29 
30 #if _UWIN && __STDPP__
31 __STDPP__directive pragma pp:nohide getenv
32 #endif
33 
34 /*
35  * NOTE: the "intercepts" definition is here instead of astintercept.c because some
36  *	 static linkers miss lone references to "intercepts" without "astintercept()"
37  * ALSO: { 0 } definition required by some dynamic linkers averse to common symbols
38  * UWIN: no _ast_getenv macro map to maintain ast54 compatibility
39  */
40 
41 Intercepts_t	intercepts
42 #if _BLD_3d
43 		;
44 #else
45 		= { 0 };
46 #endif
47 
48 #if _UWIN && !defined(getenv)
49 
50 #include <windows.h>
51 
52 extern char**	environ;
53 
54 static char*
55 default_getenv(const char* name)
56 {
57 	register char**		av;
58 	register const char*	cp;
59 	register const char*	sp;
60 	register char		c0;
61 	register char		c1;
62 
63 	av = environ;
64 	if (!av || !name || !(c0 = *name))
65 		return 0;
66 	if (!(c1 = *++name))
67 		c1 = '=';
68 	while (cp = *av++)
69 	{
70 		if (cp[0] != c0 || cp[1] != c1)
71 			continue;
72 		sp = name;
73 		cp++;
74 		while (*sp && *sp++ == *cp++);
75 		if (*(sp-1) != *(cp-1))
76 			continue;
77 		if (*sp == 0 && *cp == '=')
78 			return (char*)(cp+1);
79 	}
80 	return 0;
81 }
82 
83 #endif
84 
85 /*
86  * get name from the environment
87  */
88 
89 #if defined(__EXPORT__) && defined(getenv)
90 #define extern	__EXPORT__
91 #endif
92 
93 extern char*
94 getenv(const char* name)
95 {
96 #if _UWIN && !defined(getenv) /* for ast54 compatibility */
97 	HANDLE		dll;
98 
99 	static char*	(*posix_getenv)(const char*);
100 
101 	if (!posix_getenv)
102 	{
103 		if (dll = GetModuleHandle("posix.dll"))
104 			posix_getenv = (char*(*)(const char*))GetProcAddress(dll, "getenv");
105 		if (!posix_getenv)
106 			posix_getenv = default_getenv;
107 	}
108 	return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : (*posix_getenv)(name);
109 #else
110 #undef	getenv
111 	return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : getenv(name);
112 #endif
113 }
114