sh: Use 126 and 127 exit status for failures opening a script

This affects scripts named on the command line, named with a '.' special
builtin and found via the PATH %func autoloading mechanism.

PR:		231986
This commit is contained in:
Jilles Tjoelker 2018-11-27 21:49:59 +00:00
parent 32b083531f
commit 77da4a95e8
4 changed files with 18 additions and 4 deletions

View File

@ -359,12 +359,16 @@ popstring(void)
void
setinputfile(const char *fname, int push)
{
int e;
int fd;
int fd2;
INTOFF;
if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0)
error("cannot open %s: %s", fname, strerror(errno));
if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0) {
e = errno;
errorwithstatus(e == ENOENT || e == ENOTDIR ? 127 : 126,
"cannot open %s: %s", fname, strerror(e));
}
if (fd < 10) {
fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
close(fd);

View File

@ -32,7 +32,7 @@
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $FreeBSD$
.\"
.Dd July 19, 2018
.Dd November 27, 2018
.Dt SH 1
.Os
.Sh NAME
@ -2819,7 +2819,11 @@ Shell database.
Privileged shell profile.
.El
.Sh EXIT STATUS
Errors that are detected by the shell, such as a syntax error, will
If the
.Ar script
cannot be found, the exit status will be 127;
if it cannot be opened for another reason, the exit status will be 126.
Other errors that are detected by the shell, such as a syntax error, will
cause the shell to exit with a non-zero exit status.
If the shell is not an interactive shell, the execution of the shell
file will be aborted.

View File

@ -30,6 +30,7 @@ ${PACKAGE}FILES+= redirection-error5.0
${PACKAGE}FILES+= redirection-error6.0
${PACKAGE}FILES+= redirection-error7.0
${PACKAGE}FILES+= redirection-error8.0
${PACKAGE}FILES+= script-error1.0
${PACKAGE}FILES+= write-error1.0
.include <bsd.test.mk>

View File

@ -0,0 +1,5 @@
# $FreeBSD$
{ stderr=$(${SH} /var/empty/nosuchscript 2>&1 >&3); } 3>&1
r=$?
[ -n "$stderr" ] && [ "$r" = 127 ]