]>
Commit | Line | Data |
---|---|---|
1f2f436a A |
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) | |
224c7076 A |
4 | (oflags & O_ACCMODE)) { |
5 | fclose(fp); | |
6 | FUNLOCKFILE(fp); | |
7 | - errno = EINVAL; | |
8 | + errno = EBADF; | |
9 | return (NULL); | |
10 | } | |
1f2f436a A |
11 | if (fp->_flags & __SWR) |
12 | @@ -131,6 +131,8 @@ freopen(file, mode, fp) | |
3d9156a7 A |
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 */ | |
1f2f436a | 21 | @@ -141,11 +143,18 @@ freopen(file, mode, fp) |
3d9156a7 A |
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. */ | |
1f2f436a A |
40 | @@ -186,7 +195,7 @@ finish: |
41 | memset(&fp->_mbstate, 0, sizeof(mbstate_t)); | |
34e8f829 A |
42 | |
43 | if (f < 0) { /* did not get it after all */ | |
44 | - fp->_flags = 0; /* set it free */ | |
45 | + __sfprelease(fp); /* set it free */ | |
1f2f436a | 46 | FUNLOCKFILE(fp); |
34e8f829 | 47 | errno = sverrno; /* restore in case _close clobbered */ |
1f2f436a A |
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 */ | |
34e8f829 | 55 | FUNLOCKFILE(fp); |
1f2f436a | 56 | errno = EMFILE; |
34e8f829 | 57 | return (NULL); |