]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/freopen.c.patch
ba84faaf5770388f5161929728ab9230c792a6d7
[apple/libc.git] / stdio / FreeBSD / freopen.c.patch
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)) {
5 fclose(fp);
6 FUNLOCKFILE(fp);
7 - errno = EINVAL;
8 + errno = EBADF;
9 return (NULL);
10 }
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.
16 + *
17 + * For UNIX03, we always close if it was open.
18 */
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)
23 (void) __sflush(fp);
24 /* if close is NULL, closing is a no-op, hence pointless */
25 +#if __DARWIN_UNIX03
26 + if (fp->_close)
27 + (void) (*fp->_close)(fp->_cookie);
28 + isopen = 0;
29 + wantfd = -1;
30 +#else /* !__DARWIN_UNIX03 */
31 isopen = fp->_close != NULL;
32 if ((wantfd = fp->_file) < 0 && isopen) {
33 (void) (*fp->_close)(fp->_cookie);
34 isopen = 0;
35 }
36 +#endif /* __DARWIN_UNIX03 */
37 }
38
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));
42
43 if (f < 0) { /* did not get it after all */
44 - fp->_flags = 0; /* set it free */
45 + __sfprelease(fp); /* set it free */
46 FUNLOCKFILE(fp);
47 errno = sverrno; /* restore in case _close clobbered */
48 return (NULL);
49 @@ -212,7 +221,7 @@ finish:
50 * open.
51 */
52 if (f > SHRT_MAX) {
53 - fp->_flags = 0; /* set it free */
54 + __sfprelease(fp); /* set it free */
55 FUNLOCKFILE(fp);
56 errno = EMFILE;
57 return (NULL);