sh: Ignore error when cd writes the directory actually switched to.
If CDPATH is used non-trivially or the operand is "-", cd writes the directory actually switched to. (We currently do this only in interactive shells, but POSIX requires this in non-interactive shells as well.) As mentioned in Austin group bug #1045, cd shall not return an error while leaving the current directory changed. Therefore, ignore any write error.
This commit is contained in:
parent
19f49ad30f
commit
6f49cd266b
11
bin/sh/cd.c
11
bin/sh/cd.c
@ -164,8 +164,17 @@ docd(char *dest, int print, int phys)
|
||||
if ((phys || (rc = cdlogical(dest)) < 0) && (rc = cdphysical(dest)) < 0)
|
||||
return (-1);
|
||||
|
||||
if (print && iflag && curdir)
|
||||
if (print && iflag && curdir) {
|
||||
out1fmt("%s\n", curdir);
|
||||
/*
|
||||
* Ignore write errors to preserve the invariant that the
|
||||
* current directory is changed iff the exit status is 0
|
||||
* (or 1 if -e was given and the full pathname could not be
|
||||
* determined).
|
||||
*/
|
||||
flushout(out1);
|
||||
outclearerror(out1);
|
||||
}
|
||||
|
||||
return (rc);
|
||||
}
|
||||
|
@ -2018,6 +2018,11 @@ to return exit status 1 if the full pathname of the new directory
|
||||
cannot be determined reliably or at all.
|
||||
Normally this is not considered an error,
|
||||
although a warning is printed.
|
||||
.Pp
|
||||
If changing the directory fails, the exit status is greater than 1.
|
||||
If the directory is changed, the exit status is 0, or also 1 if
|
||||
.Fl e
|
||||
was given.
|
||||
.It Ic chdir
|
||||
A synonym for the
|
||||
.Ic cd
|
||||
|
@ -51,6 +51,7 @@ ${PACKAGE}FILES+= cd6.0
|
||||
${PACKAGE}FILES+= cd7.0
|
||||
${PACKAGE}FILES+= cd8.0
|
||||
${PACKAGE}FILES+= cd9.0 cd9.0.stdout
|
||||
${PACKAGE}FILES+= cd10.0
|
||||
${PACKAGE}FILES+= command1.0
|
||||
${PACKAGE}FILES+= command2.0
|
||||
${PACKAGE}FILES+= command3.0
|
||||
|
6
bin/sh/tests/builtins/cd10.0
Normal file
6
bin/sh/tests/builtins/cd10.0
Normal file
@ -0,0 +1,6 @@
|
||||
# $FreeBSD$
|
||||
|
||||
# Precondition
|
||||
(cd /bin) || exit
|
||||
# Verify write error is ignored.
|
||||
$SH +m -ic 'CDPATH=/:; cd bin 1</dev/null'
|
Loading…
Reference in New Issue
Block a user