diff options
author | jilles <jilles@FreeBSD.org> | 2015-08-09 15:53:02 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2015-08-09 15:53:02 +0000 |
commit | de78631dfc5ad851e1352a3820d1276e4f0178c2 (patch) | |
tree | a0e938a21819092f9fb8ab7e06c66b3dfbaad1e1 /lib | |
parent | 83028e54ab8f8b1bab3e88aed286cc829c23f28d (diff) | |
download | FreeBSD-src-de78631dfc5ad851e1352a3820d1276e4f0178c2.zip FreeBSD-src-de78631dfc5ad851e1352a3820d1276e4f0178c2.tar.gz |
MFC r284649: fts_children: preserve errno after running close/fchdir
PR: 200942
Submitted by: Conrad Meyer
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libc/gen/fts.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c index 1ca8334..8083a88 100644 --- a/lib/libc/gen/fts.c +++ b/lib/libc/gen/fts.c @@ -515,7 +515,7 @@ FTSENT * fts_children(FTS *sp, int instr) { FTSENT *p; - int fd; + int fd, rc, serrno; if (instr != 0 && instr != FTS_NAMEONLY) { errno = EINVAL; @@ -571,11 +571,14 @@ fts_children(FTS *sp, int instr) if ((fd = _open(".", O_RDONLY | O_CLOEXEC, 0)) < 0) return (NULL); sp->fts_child = fts_build(sp, instr); - if (fchdir(fd)) { - (void)_close(fd); - return (NULL); - } + serrno = (sp->fts_child == NULL) ? errno : 0; + rc = fchdir(fd); + if (rc < 0 && serrno == 0) + serrno = errno; (void)_close(fd); + errno = serrno; + if (rc < 0) + return (NULL); return (sp->fts_child); } |