modified: spffq.py
[GalaxyCodeBases.git] / etc / getenv32.sh
blob50f1444e5bce753f40dff8a8f14026aae2ce8596
1 #!/bin/sh
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU Library General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@gmail.com>
19 # History:
20 # 20061027: Initial release
21 # 20110314: Compile with -fPIC
23 if [ "$#" -eq 0 ]; then
24 echo "Usage: $0 COMMAND [ARGS...]" >&2
25 exit 1
28 TEMPDIR=$(mktemp -d)
29 [ -z "$TEMPDIR" ] && exit 1
30 trap "rm -fr $TEMPDIR" EXIT
32 cat > "$TEMPDIR/getenv.c" <<'EOF' || exit 1
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <dlfcn.h>
37 char *getenv (const char *name)
39 static char *(*real_getenv)(const char *name) = NULL;
40 char *ret;
42 if (real_getenv == NULL) {
43 void *handle = dlopen("/lib/libc.so.6", RTLD_LAZY);
44 if (handle == NULL) {
45 fprintf(stderr, "dlopen: %s\n", dlerror());
46 exit(1);
49 real_getenv = dlsym(handle, "getenv");
50 if (real_getenv == NULL) {
51 fprintf(stderr, "dlsym: %s\n", dlerror());
52 exit(1);
56 ret = real_getenv(name);
57 if (ret == NULL)
58 printf("getenv(\"%s\") = NULL\n", name);
59 else
60 printf("getenv(\"%s\") = \"%s\"\n", name, ret);
62 return ret;
64 EOF
66 gcc -fPIC -O0 -ldl -shared "$TEMPDIR/getenv.c" -o "$TEMPDIR/libgetenv.so" || exit 1
67 LD_PRELOAD="$TEMPDIR/libgetenv.so:$LD_PRELOAD" "$@"