Loading...
stdio/FreeBSD/freopen.c Libc-1725.40.4 Libc-763.13
--- Libc/Libc-1725.40.4/stdio/FreeBSD/freopen.c
+++ Libc/Libc-763.13/stdio/FreeBSD/freopen.c
@@ -48,7 +48,6 @@
 #include "un-namespace.h"
 #include "libc_private.h"
 #include "local.h"
-#include "libc_hooks_impl.h"
 
 /*
  * Re-direct an existing, open (probably) file to some other file.
@@ -56,14 +55,13 @@
  * all possible, no matter what.
  */
 FILE *
-freopen(const char * __restrict file, const char * __restrict mode, FILE *fp)
+freopen(file, mode, fp)
+	const char * __restrict file;
+	const char * __restrict mode;
+	FILE *fp;
 {
 	int f;
 	int dflags, flags, isopen, oflags, sverrno, wantfd;
-
-	libc_hooks_will_read_cstring(file);
-	libc_hooks_will_read_cstring(mode);
-	libc_hooks_will_write(fp, sizeof(*fp));
 
 	if ((flags = __sflags(mode, &oflags)) == 0) {
 		sverrno = errno;
@@ -72,9 +70,10 @@
 		return (NULL);
 	}
 
-	pthread_once(&__sdidinit, __sinit);
-	
 	FLOCKFILE(fp);
+
+	if (!__sdidinit)
+		__sinit();
 
 	/*
 	 * If the filename is a NULL pointer, the caller is asking us to
@@ -99,7 +98,7 @@
 		    (oflags & O_ACCMODE)) {
 			fclose(fp);
 			FUNLOCKFILE(fp);
-			errno = EBADF;
+			errno = EINVAL;
 			return (NULL);
 		}
 		if (fp->_flags & __SWR)
@@ -132,8 +131,6 @@
 	 * descriptor (if any) was associated with it.  If it was attached to
 	 * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
 	 * should work.  This is unnecessary if it was not a Unix file.
-	 *
-	 * For UNIX03, we always close if it was open.
 	 */
 	if (fp->_flags == 0) {
 		fp->_flags = __SEOF;	/* hold on to it */
@@ -144,18 +141,11 @@
 		if (fp->_flags & __SWR)
 			(void) __sflush(fp);
 		/* if close is NULL, closing is a no-op, hence pointless */
-#if __DARWIN_UNIX03
-		if (fp->_close)
-			(void) (*fp->_close)(fp->_cookie);
-		isopen = 0;
-		wantfd = -1;
-#else /* !__DARWIN_UNIX03 */
 		isopen = fp->_close != NULL;
 		if ((wantfd = fp->_file) < 0 && isopen) {
 			(void) (*fp->_close)(fp->_cookie);
 			isopen = 0;
 		}
-#endif /* __DARWIN_UNIX03 */
 	}
 
 	/* Get a new descriptor to refer to the new file. */
@@ -196,8 +186,8 @@
 	memset(&fp->_mbstate, 0, sizeof(mbstate_t));
 
 	if (f < 0) {			/* did not get it after all */
+		fp->_flags = 0;		/* set it free */
 		FUNLOCKFILE(fp);
-		__sfprelease(fp);	/* set it free */
 		errno = sverrno;	/* restore in case _close clobbered */
 		return (NULL);
 	}
@@ -222,8 +212,8 @@
 	 * open.
 	 */
 	if (f > SHRT_MAX) {
+		fp->_flags = 0;		/* set it free */
 		FUNLOCKFILE(fp);
-		__sfprelease(fp);	/* set it free */
 		errno = EMFILE;
 		return (NULL);
 	}