2 # Verify behavior of env.
4 # Copyright (C) 2009-2016 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
23 # A simple shebang program to call "echo" from symlinks like "./-u" or "./--".
24 echo "#!$abs_top_builddir/src/echo simple_echo" > simple_echo \
26 chmod a
+x simple_echo || framework_failure_
28 # Verify we can run the shebang which is not the case if
29 # there are spaces in $abs_top_builddir.
30 .
/simple_echo || skip_
"Error running simple_echo script"
32 # Verify clearing the environment
36 compare
/dev
/null out || fail
=1
37 env
-i > out || fail
=1
38 compare
/dev
/null out || fail
=1
39 env
-u a
-i -u a
-- > out || fail
=1
40 compare
/dev
/null out || fail
=1
41 env
-i -- a
=b
> out || fail
=1
42 echo a
=b
> exp || framework_failure_
43 compare exp out || fail
=1
45 # These tests verify exact status of internal failure.
46 env
--- # unknown option
47 test $?
= 125 || fail
=1
48 env
-u # missing option argument
49 test $?
= 125 || fail
=1
50 env sh
-c 'exit 2' # exit status propagation
52 env .
# invalid command
53 test $?
= 126 || fail
=1
54 env no_such
# no such command
55 test $?
= 127 || fail
=1
57 # POSIX is clear that environ may, but need not be, sorted.
58 # Environment variable values may contain newlines, which cannot be
59 # observed by merely inspecting output from env.
60 # Cygwin requires a minimal environment to launch new processes: execve
61 # adds missing variables SYSTEMROOT and WINDIR, which show up in a
62 # subsequent env. Cygwin also requires /bin to always be part of PATH,
63 # and attempts to unset or reduce PATH may cause execve to fail.
65 # For these reasons, it is more portable to grep that our desired changes
66 # took place, rather than comparing output of env over an entire environment.
67 if env |
grep '^ENV_TEST' >/dev
/null
; then
68 skip_
"environment has potential interference from ENV_TEST*"
73 >out || framework_failure_
74 env ENV_TEST2
= > all || fail
=1
75 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
76 env
-u ENV_TEST1 ENV_TEST3
=c
> all || fail
=1
77 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
78 env ENV_TEST1
=b
> all || fail
=1
79 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
80 env ENV_TEST2
= env
> all || fail
=1
81 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
82 env
-u ENV_TEST1 ENV_TEST3
=c env
> all || fail
=1
83 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
84 env ENV_TEST1
=b env
> all || fail
=1
85 grep '^ENV_TEST' all | LC_ALL
=C
sort >> out || framework_failure_
86 cat <<EOF >exp || framework_failure_
96 compare exp out || fail
=1
98 # PATH modifications affect exec.
99 mkdir unlikely_name || framework_failure_
100 cat <<EOF > unlikely_name/also_unlikely || framework_failure_
104 chmod +x unlikely_name
/also_unlikely || framework_failure_
105 returns_
127 env also_unlikely || fail
=1
106 test x$
(PATH
=$PATH:unlikely_name env also_unlikely
) = xpass || fail
=1
107 test x$
(env PATH
="$PATH":unlikely_name also_unlikely
) = xpass || fail
=1
109 # Explicitly put . on the PATH for the rest of this test.
113 # Use -- to end options (but not variable assignments).
114 # On some systems, execve("-i") invokes a shebang script ./-i on PATH as
115 # '/bin/sh -i', rather than '/bin/sh -- -i', which doesn't do what we want.
116 # Avoid the issue by using a shebang to 'echo' passing a second parameter
117 # before the '-i'. See the definition of simple_echo before.
118 # Test -u, rather than -i, to minimize PATH problems.
119 ln -s "simple_echo" .
/-u || framework_failure_
120 case $
(env
-u echo echo good
) in
124 case $
(env
-u echo -- echo good
) in
128 case $
(env
-- -u pass
) in
133 # After options have ended, the first argument not containing = is a program.
135 test $?
= 127 || fail
=1
136 ln -s "simple_echo" .
/-- || framework_failure_
137 case $
(env a
=b
-- true ||
echo fail
) in
142 # No way to directly invoke program name containing =.
143 cat <<EOF >./c=d || framework_failure_
147 chmod +x c
=d || framework_failure_
148 test "x$(env c=d echo fail)" = xfail || fail
=1
149 test "x$(env -- c=d echo fail)" = xfail || fail
=1
150 test "x$(env ./c=d echo fail)" = xfail || fail
=1
151 test "x$(env sh -c 'exec "$@
"' sh c=d echo fail)" = xpass || fail
=1
152 test "x$(sh -c '\c=d echo fail')" = xpass
&& #dash 0.5.4 fails so check first
153 { test "x$(env sh -c '\c=d echo fail')" = xpass || fail
=1; }
155 # catch unsetenv failure, broken through coreutils 8.0
156 returns_
125 env
-u a
=b true || fail
=1
157 returns_
125 env
-u '' true || fail
=1