summaryrefslogtreecommitdiff
path: root/src/lib/libast/misc/getenv.c
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
committerIgor Pashev <pashev.igor@gmail.com>2012-06-24 22:28:35 +0000
commit3950ffe2a485479f6561c27364d3d7df5a21d124 (patch)
tree468c6e14449d1b1e279222ec32f676b0311917d2 /src/lib/libast/misc/getenv.c
downloadksh-upstream.tar.gz
Imported Upstream version 93u+upstream
Diffstat (limited to 'src/lib/libast/misc/getenv.c')
-rw-r--r--src/lib/libast/misc/getenv.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/lib/libast/misc/getenv.c b/src/lib/libast/misc/getenv.c
new file mode 100644
index 0000000..7e24817
--- /dev/null
+++ b/src/lib/libast/misc/getenv.c
@@ -0,0 +1,113 @@
+/***********************************************************************
+* *
+* This software is part of the ast package *
+* Copyright (c) 1985-2011 AT&T Intellectual Property *
+* and is licensed under the *
+* Eclipse Public License, Version 1.0 *
+* by AT&T Intellectual Property *
+* *
+* A copy of the License is available at *
+* http://www.eclipse.org/org/documents/epl-v10.html *
+* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+* *
+* Information and Software Systems Research *
+* AT&T Research *
+* Florham Park NJ *
+* *
+* Glenn Fowler <gsf@research.att.com> *
+* David Korn <dgk@research.att.com> *
+* Phong Vo <kpv@research.att.com> *
+* *
+***********************************************************************/
+#pragma prototyped
+
+#if _UWIN && __STDPP__
+__STDPP__directive pragma pp:hide getenv
+#endif
+
+#include "intercepts.h"
+
+#if _UWIN && __STDPP__
+__STDPP__directive pragma pp:nohide getenv
+#endif
+
+/*
+ * NOTE: the "intercepts" definition is here instead of astintercept.c because some
+ * static linkers miss lone references to "intercepts" without "astintercept()"
+ * ALSO: { 0 } definition required by some dynamic linkers averse to common symbols
+ * UWIN: no _ast_getenv macro map to maintain ast54 compatibility
+ */
+
+Intercepts_t intercepts
+#if _BLD_3d
+ ;
+#else
+ = { 0 };
+#endif
+
+#if _UWIN && !defined(getenv)
+
+#include <windows.h>
+
+extern char** environ;
+
+static char*
+default_getenv(const char* name)
+{
+ register char** av;
+ register const char* cp;
+ register const char* sp;
+ register char c0;
+ register char c1;
+
+ av = environ;
+ if (!av || !name || !(c0 = *name))
+ return 0;
+ if (!(c1 = *++name))
+ c1 = '=';
+ while (cp = *av++)
+ {
+ if (cp[0] != c0 || cp[1] != c1)
+ continue;
+ sp = name;
+ cp++;
+ while (*sp && *sp++ == *cp++);
+ if (*(sp-1) != *(cp-1))
+ continue;
+ if (*sp == 0 && *cp == '=')
+ return (char*)(cp+1);
+ }
+ return 0;
+}
+
+#endif
+
+/*
+ * get name from the environment
+ */
+
+#if defined(__EXPORT__) && defined(getenv)
+#define extern __EXPORT__
+#endif
+
+extern char*
+getenv(const char* name)
+{
+#if _UWIN && !defined(getenv) /* for ast54 compatibility */
+ HANDLE dll;
+
+ static char* (*posix_getenv)(const char*);
+
+ if (!posix_getenv)
+ {
+ if (dll = GetModuleHandle("posix.dll"))
+ posix_getenv = (char*(*)(const char*))GetProcAddress(dll, "getenv");
+ if (!posix_getenv)
+ posix_getenv = default_getenv;
+ }
+ return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : (*posix_getenv)(name);
+#else
+#undef getenv
+ return intercepts.intercept_getenv ? (*intercepts.intercept_getenv)(name) : getenv(name);
+#endif
+}