X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/ad3c9f2af814c84582fdd1649e49ec4f68572c5a..refs/heads/master:/stdio/FreeBSD/fclose.c diff --git a/stdio/FreeBSD/fclose.c b/stdio/FreeBSD/fclose.c index 91df68a..25a6cbb 100644 --- a/stdio/FreeBSD/fclose.c +++ b/stdio/FreeBSD/fclose.c @@ -48,9 +48,9 @@ int fclose(FILE *fp) { int r; + int error = 0; - if (!__sdidinit) - __sinit(); + pthread_once(&__sdidinit, __sinit); if (fp == NULL) { errno = EFAULT; @@ -61,9 +61,14 @@ fclose(FILE *fp) return (EOF); } FLOCKFILE(fp); - r = fp->_flags & __SWR ? __sflush(fp) : 0; - if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) + r = __sflush(fp); + if (r < 0) { + error = errno; + } + if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0) { r = EOF; + error = errno; + } if (fp->_flags & __SMBF) free((char *)fp->_bf._base); if (HASUB(fp)) @@ -72,7 +77,11 @@ fclose(FILE *fp) FREELB(fp); fp->_file = -1; fp->_r = fp->_w = 0; /* Mess up if reaccessed. */ - __sfprelease(fp); /* Release this FILE for reuse. */ FUNLOCKFILE(fp); + __sfprelease(fp); /* Release this FILE for reuse. */ + /* Don't clobber errno unnecessarily. */ + if (error) { + errno = error; + } return (r); }