1 --- freopen.c.bsdnew 2009-11-11 13:33:09.000000000 -0800
2 +++ freopen.c 2009-11-11 14:45:57.000000000 -0800
3 @@ -98,7 +98,7 @@ freopen(file, mode, fp)
4 (oflags & O_ACCMODE)) {
11 if (fp->_flags & __SWR)
12 @@ -131,6 +131,8 @@ freopen(file, mode, fp)
13 * descriptor (if any) was associated with it. If it was attached to
14 * a descriptor, defer closing it; freopen("/dev/stdin", "r", stdin)
15 * should work. This is unnecessary if it was not a Unix file.
17 + * For UNIX03, we always close if it was open.
19 if (fp->_flags == 0) {
20 fp->_flags = __SEOF; /* hold on to it */
21 @@ -141,11 +143,18 @@ freopen(file, mode, fp)
22 if (fp->_flags & __SWR)
24 /* if close is NULL, closing is a no-op, hence pointless */
27 + (void) (*fp->_close)(fp->_cookie);
30 +#else /* !__DARWIN_UNIX03 */
31 isopen = fp->_close != NULL;
32 if ((wantfd = fp->_file) < 0 && isopen) {
33 (void) (*fp->_close)(fp->_cookie);
36 +#endif /* __DARWIN_UNIX03 */
39 /* Get a new descriptor to refer to the new file. */
40 @@ -186,7 +195,7 @@ finish:
41 memset(&fp->_mbstate, 0, sizeof(mbstate_t));
43 if (f < 0) { /* did not get it after all */
44 - fp->_flags = 0; /* set it free */
45 + __sfprelease(fp); /* set it free */
47 errno = sverrno; /* restore in case _close clobbered */
49 @@ -212,7 +221,7 @@ finish:
53 - fp->_flags = 0; /* set it free */
54 + __sfprelease(fp); /* set it free */