xref: /titanic_41/usr/src/lib/libast/common/misc/getenv.c (revision edafac1f8203a7de1490688213c118b03215c89d)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2009 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 = { 0 };
42 
43 #if _UWIN && !defined(getenv)
44 
45 #include <windows.h>
46 
47 extern char**	environ;
48 
49 static char*
50 default_getenv(const char* name)
51 {
52 	register char**		av;
53 	register const char*	cp;
54 	register const char*	sp;
55 	register char		c0;
56 	register char		c1;
57 
58 	av = environ;
59 	if (!av || !name || !(c0 = *name))
60 		return 0;
61 	if (!(c1 = *++name))
62 		c1 = '=';
63 	while (cp = *av++)
64 	{
65 		if (cp[0] != c0 || cp[1] != c1)
66 			continue;
67 		sp = name;
68 		cp++;
69 		while (*sp && *sp++ == *cp++);
70 		if (*(sp-1) != *(cp-1))
71 			continue;
72 		if (*sp == 0 && *cp == '=')
73 			return (char*)(cp+1);
74 	}
75 	return 0;
76 }
77 
78 #endif
79 
80 /*
81  * get name from the environment
82  */
83 
84 #if defined(__EXPORT__) && defined(getenv)
85 #define extern	__EXPORT__
86 #endif
87 
88 extern char*
89 getenv(const char* name)
90 {
91 #if _UWIN && !defined(getenv) /* for ast54 compatibility */
92 	HANDLE		dll;
93 
94 	static char*	(*posix_getenv)(const char*);
95 
96 	if (!posix_getenv)
97 	{
98 		if (dll = GetModuleHandle("posix.dll"))
99 			posix_getenv = (char*(*)(const char*))GetProcAddress(dll, "getenv");
100 		if (!posix_getenv)
101 			posix_getenv = default_getenv;
102 	}
103 	return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : (*posix_getenv)(name);
104 #else
105 #undef	getenv
106 	return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : getenv(name);
107 #endif
108 }
109