summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2012-01-22 14:00:33 +0000
committerjilles <jilles@FreeBSD.org>2012-01-22 14:00:33 +0000
commitae59680813890a93e9eaebbf69c2d63b852e512f (patch)
tree2e29d787749cfc02654715cbca4bcb06b3c510ec
parentf317ff0f6559bac3c7a0ee926ba002b29cdf2d53 (diff)
downloadFreeBSD-src-ae59680813890a93e9eaebbf69c2d63b852e512f.zip
FreeBSD-src-ae59680813890a93e9eaebbf69c2d63b852e512f.tar.gz
sh: Fix $? in the first command of a 'for'.
In the first command of a 'for', $? should be the exit status of the last pipeline (command substitution in the word list or command before 'for'), not always 0.
-rw-r--r--bin/sh/eval.c5
-rw-r--r--tools/regression/bin/sh/builtins/for2.09
-rw-r--r--tools/regression/bin/sh/builtins/for3.08
3 files changed, 21 insertions, 1 deletions
diff --git a/bin/sh/eval.c b/bin/sh/eval.c
index f4f69d9..a5f0aff 100644
--- a/bin/sh/eval.c
+++ b/bin/sh/eval.c
@@ -348,6 +348,7 @@ evalfor(union node *n, int flags)
union node *argp;
struct strlist *sp;
struct stackmark smark;
+ int status;
setstackmark(&smark);
arglist.lastp = &arglist.list;
@@ -357,11 +358,12 @@ evalfor(union node *n, int flags)
}
*arglist.lastp = NULL;
- exitstatus = 0;
loopnest++;
+ status = 0;
for (sp = arglist.list ; sp ; sp = sp->next) {
setvar(n->nfor.var, sp->text, 0);
evaltree(n->nfor.body, flags);
+ status = exitstatus;
if (evalskip) {
if (evalskip == SKIPCONT && --skipcount <= 0) {
evalskip = 0;
@@ -374,6 +376,7 @@ evalfor(union node *n, int flags)
}
loopnest--;
popstackmark(&smark);
+ exitstatus = status;
}
diff --git a/tools/regression/bin/sh/builtins/for2.0 b/tools/regression/bin/sh/builtins/for2.0
new file mode 100644
index 0000000..48c22ce
--- /dev/null
+++ b/tools/regression/bin/sh/builtins/for2.0
@@ -0,0 +1,9 @@
+# $FreeBSD$
+
+r=x
+f() { return 42; }
+f
+for i in x; do
+ r=$?
+done
+[ "$r" = 42 ]
diff --git a/tools/regression/bin/sh/builtins/for3.0 b/tools/regression/bin/sh/builtins/for3.0
new file mode 100644
index 0000000..cc37238
--- /dev/null
+++ b/tools/regression/bin/sh/builtins/for3.0
@@ -0,0 +1,8 @@
+# $FreeBSD$
+
+r=x
+f() { return 42; }
+for i in x`f`; do
+ r=$?
+done
+[ "$r" = 42 ]
OpenPOWER on IntegriCloud