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>
20 # 20061027: Initial release
21 # 20110314: Compile with -fPIC
23 if [ "$#" -eq 0 ]; then
24 echo "Usage: $0 COMMAND [ARGS...]" >&2
29 [ -z "$TEMPDIR" ] && exit 1
30 trap "rm -fr $TEMPDIR" EXIT
32 cat > "$TEMPDIR/getenv.c" <<'EOF' || exit 1
37 char *getenv (const char *name)
39 static char *(*real_getenv)(const char *name) = NULL;
42 if (real_getenv == NULL) {
43 void *handle = dlopen("/lib64/libc.so.6", RTLD_LAZY);
45 fprintf(stderr, "dlopen: %s\n", dlerror());
49 real_getenv = dlsym(handle, "getenv");
50 if (real_getenv == NULL) {
51 fprintf(stderr, "dlsym: %s\n", dlerror());
56 ret = real_getenv(name);
58 printf("getenv(\"%s\") = NULL\n", name);
60 printf("getenv(\"%s\") = \"%s\"\n", name, ret);
66 gcc
-fPIC -O0 -ldl -shared "$TEMPDIR/getenv.c" -o "$TEMPDIR/libgetenv.so" ||
exit 1
67 LD_PRELOAD
="$TEMPDIR/libgetenv.so:$LD_PRELOAD" "$@"