xref: /titanic_50/usr/src/lib/libast/common/misc/getenv.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
17c2fbfb3SApril Chin /***********************************************************************
27c2fbfb3SApril Chin *                                                                      *
37c2fbfb3SApril Chin *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
57c2fbfb3SApril Chin *                      and is licensed under the                       *
67c2fbfb3SApril Chin *                  Common Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
87c2fbfb3SApril Chin *                                                                      *
97c2fbfb3SApril Chin *                A copy of the License is available at                 *
107c2fbfb3SApril Chin *            http://www.opensource.org/licenses/cpl1.0.txt             *
117c2fbfb3SApril Chin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
127c2fbfb3SApril Chin *                                                                      *
137c2fbfb3SApril Chin *              Information and Software Systems Research               *
147c2fbfb3SApril Chin *                            AT&T Research                             *
157c2fbfb3SApril Chin *                           Florham Park NJ                            *
167c2fbfb3SApril Chin *                                                                      *
177c2fbfb3SApril Chin *                 Glenn Fowler <gsf@research.att.com>                  *
187c2fbfb3SApril Chin *                  David Korn <dgk@research.att.com>                   *
197c2fbfb3SApril Chin *                   Phong Vo <kpv@research.att.com>                    *
207c2fbfb3SApril Chin *                                                                      *
217c2fbfb3SApril Chin ***********************************************************************/
227c2fbfb3SApril Chin #pragma prototyped
237c2fbfb3SApril Chin 
2434f9b3eeSRoland Mainz #if _UWIN && __STDPP__
2534f9b3eeSRoland Mainz __STDPP__directive pragma pp:hide getenv
2634f9b3eeSRoland Mainz #endif
2734f9b3eeSRoland Mainz 
287c2fbfb3SApril Chin #include "intercepts.h"
297c2fbfb3SApril Chin 
3034f9b3eeSRoland Mainz #if _UWIN && __STDPP__
3134f9b3eeSRoland Mainz __STDPP__directive pragma pp:nohide getenv
3234f9b3eeSRoland Mainz #endif
3334f9b3eeSRoland Mainz 
347c2fbfb3SApril Chin /*
357c2fbfb3SApril Chin  * NOTE: the "intercepts" definition is here instead of astintercept.c because some
367c2fbfb3SApril Chin  *	 static linkers miss lone references to "intercepts" without "astintercept()"
3734f9b3eeSRoland Mainz  * ALSO: { 0 } definition required by some dynamic linkers averse to common symbols
3834f9b3eeSRoland Mainz  * UWIN: no _ast_getenv macro map to maintain ast54 compatibility
397c2fbfb3SApril Chin  */
407c2fbfb3SApril Chin 
41*3e14f97fSRoger A. Faulkner Intercepts_t	intercepts
42*3e14f97fSRoger A. Faulkner #if _BLD_3d
43*3e14f97fSRoger A. Faulkner 		;
44*3e14f97fSRoger A. Faulkner #else
45*3e14f97fSRoger A. Faulkner 		= { 0 };
46*3e14f97fSRoger A. Faulkner #endif
477c2fbfb3SApril Chin 
4834f9b3eeSRoland Mainz #if _UWIN && !defined(getenv)
4934f9b3eeSRoland Mainz 
5034f9b3eeSRoland Mainz #include <windows.h>
5134f9b3eeSRoland Mainz 
5234f9b3eeSRoland Mainz extern char**	environ;
5334f9b3eeSRoland Mainz 
5434f9b3eeSRoland Mainz static char*
default_getenv(const char * name)5534f9b3eeSRoland Mainz default_getenv(const char* name)
5634f9b3eeSRoland Mainz {
5734f9b3eeSRoland Mainz 	register char**		av;
5834f9b3eeSRoland Mainz 	register const char*	cp;
5934f9b3eeSRoland Mainz 	register const char*	sp;
6034f9b3eeSRoland Mainz 	register char		c0;
6134f9b3eeSRoland Mainz 	register char		c1;
6234f9b3eeSRoland Mainz 
6334f9b3eeSRoland Mainz 	av = environ;
6434f9b3eeSRoland Mainz 	if (!av || !name || !(c0 = *name))
6534f9b3eeSRoland Mainz 		return 0;
6634f9b3eeSRoland Mainz 	if (!(c1 = *++name))
6734f9b3eeSRoland Mainz 		c1 = '=';
6834f9b3eeSRoland Mainz 	while (cp = *av++)
6934f9b3eeSRoland Mainz 	{
7034f9b3eeSRoland Mainz 		if (cp[0] != c0 || cp[1] != c1)
7134f9b3eeSRoland Mainz 			continue;
7234f9b3eeSRoland Mainz 		sp = name;
7334f9b3eeSRoland Mainz 		cp++;
7434f9b3eeSRoland Mainz 		while (*sp && *sp++ == *cp++);
7534f9b3eeSRoland Mainz 		if (*(sp-1) != *(cp-1))
7634f9b3eeSRoland Mainz 			continue;
7734f9b3eeSRoland Mainz 		if (*sp == 0 && *cp == '=')
7834f9b3eeSRoland Mainz 			return (char*)(cp+1);
7934f9b3eeSRoland Mainz 	}
8034f9b3eeSRoland Mainz 	return 0;
8134f9b3eeSRoland Mainz }
8234f9b3eeSRoland Mainz 
8334f9b3eeSRoland Mainz #endif
8434f9b3eeSRoland Mainz 
857c2fbfb3SApril Chin /*
867c2fbfb3SApril Chin  * get name from the environment
877c2fbfb3SApril Chin  */
887c2fbfb3SApril Chin 
8934f9b3eeSRoland Mainz #if defined(__EXPORT__) && defined(getenv)
9034f9b3eeSRoland Mainz #define extern	__EXPORT__
9134f9b3eeSRoland Mainz #endif
9234f9b3eeSRoland Mainz 
9334f9b3eeSRoland Mainz extern char*
getenv(const char * name)947c2fbfb3SApril Chin getenv(const char* name)
957c2fbfb3SApril Chin {
9634f9b3eeSRoland Mainz #if _UWIN && !defined(getenv) /* for ast54 compatibility */
9734f9b3eeSRoland Mainz 	HANDLE		dll;
9834f9b3eeSRoland Mainz 
9934f9b3eeSRoland Mainz 	static char*	(*posix_getenv)(const char*);
10034f9b3eeSRoland Mainz 
10134f9b3eeSRoland Mainz 	if (!posix_getenv)
10234f9b3eeSRoland Mainz 	{
10334f9b3eeSRoland Mainz 		if (dll = GetModuleHandle("posix.dll"))
10434f9b3eeSRoland Mainz 			posix_getenv = (char*(*)(const char*))GetProcAddress(dll, "getenv");
10534f9b3eeSRoland Mainz 		if (!posix_getenv)
10634f9b3eeSRoland Mainz 			posix_getenv = default_getenv;
10734f9b3eeSRoland Mainz 	}
10834f9b3eeSRoland Mainz 	return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : (*posix_getenv)(name);
10934f9b3eeSRoland Mainz #else
1107c2fbfb3SApril Chin #undef	getenv
1117c2fbfb3SApril Chin 	return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : getenv(name);
11234f9b3eeSRoland Mainz #endif
1137c2fbfb3SApril Chin }
114