1 --- freopen.c.orig 2009-02-15 03:11:22.000000000 -0800
2 +++ freopen.c 2009-02-15 14:26:16.000000000 -0800
3 @@ -99,7 +99,7 @@ freopen(file, mode, fp)
4 (oflags & O_ACCMODE)) {
11 if ((oflags ^ dflags) & O_APPEND) {
12 @@ -136,6 +136,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 @@ -146,11 +148,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 @@ -191,7 +200,7 @@ finish:
41 memset(&fp->_extra->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 */
46 errno = sverrno; /* restore in case _close clobbered */