1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# Test whether CR #6763594 ('ksh93 executes command after "command"
28# builtin twice on failure') has been fixed.
29#
30# Quote from CR #6763594:
31# ---- snip ----
32# ksh93 has a bug which causes shell to execute the command after the
33# "command" builtin to be executed twice if "command" fails:
34# -- snip --
35# $ ksh93 -x -c 'print "true" >myfoo ; chmod a+x,a-r myfoo ; command ./myfoo ;
36# print $?'
37# + print true
38# + 1> myfoo
39# + chmod a+x,a-r myfoo
40# + command ./myfoo
41# ksh93[1]: ./myfoo: ./myfoo: cannot open [Permission denied]
42# + print 1
43# 1
44# + print 0
45# 0
46# -- snip --
47# The "print" command at the end is executed twice in this case since
48# the shell jumps to the wrong position in the execution sequence.
49#
50# The correct output should be:
51# -- snip --
52# $ ksh93 -x -c 'print "true" >myfoo ; chmod a+x,a-r myfoo ; command ./myfoo ;
53# print $?'
54# + print true
55# + 1> myfoo
56# + chmod a+x,a-r myfoo
57# + command ./myfoo
58# ksh93[1]: ./myfoo: ./myfoo: cannot open [Permission denied]
59# + print 1
60# 1
61# -- snip --
62# ---- snip ----
63
64
65# test setup
66function err_exit
67{
68	print -u2 -n "\t"
69	print -u2 -r ${Command}[$1]: "${@:2}"
70	(( Errors < 127 && Errors++ ))
71}
72alias err_exit='err_exit $LINENO'
73
74set -o nounset
75Command=${0##*/}
76integer Errors=0
77
78
79typeset testtmpdir=/tmp/ksh93_test_cr_6763594_${PPID}_$$
80mkdir "${testtmpdir}" || { err_exit "Could not create temporary directory ${testtmpdir}." ; exit ${Errors} ; }
81
82cd "${testtmpdir}" || { err_exit "Cannot cd to temporary directory ${testtmpdir}." ; exit ${Errors} ; }
83
84typeset s
85
86${SHELL} -c 'print "true" >myfoo ; chmod a+x,a-r myfoo ; command ./myfoo ; print $?' 1>out_stdout 2>out_stderr
87(( $? == 0 )) || err_exit "Return code $?, expected 0"
88
89s=$( < out_stdout ) ; [[ "$s" == '126' ]] || err_exit "Expected '126', got $(printf "%q\n" "$s")."
90s=$( < out_stderr ) ; [[ "$s" == ~(Elr)(.*:\ \./myfoo:\ \./myfoo:\ .*\[.*\]) ]] || err_exit "Output $(printf "%q\n" "$s") does not match pattern '~(Elr)(.*:\ \./myfoo:\ \./myfoo:\ .*\[.*\])'."
91
92rm "myfoo" "out_stdout" "out_stderr" || err_exit "rm failed."
93cd ..
94rmdir "${testtmpdir}" || err_exit "Failed to remove temporary directory ${testtmpdir}."
95
96
97# tests done
98exit $((Errors))
99