1 | /* |
---|
2 | * Copyright (c) 1990 The Regents of the University of California. |
---|
3 | * All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms are permitted |
---|
6 | * provided that the above copyright notice and this paragraph are |
---|
7 | * duplicated in all such forms and that any documentation, |
---|
8 | * advertising materials, and other materials related to such |
---|
9 | * distribution and use acknowledge that the software was developed |
---|
10 | * by the University of California, Berkeley. The name of the |
---|
11 | * University may not be used to endorse or promote products derived |
---|
12 | * from this software without specific prior written permission. |
---|
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
---|
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
---|
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | FUNCTION |
---|
20 | <<freopen64>>---open a large file using an existing file descriptor |
---|
21 | |
---|
22 | INDEX |
---|
23 | freopen64 |
---|
24 | INDEX |
---|
25 | _freopen64_r |
---|
26 | |
---|
27 | SYNOPSIS |
---|
28 | #include <stdio.h> |
---|
29 | FILE *freopen64(const char *<[file]>, const char *<[mode]>, |
---|
30 | FILE *<[fp]>); |
---|
31 | FILE *_freopen64_r(struct _reent *<[ptr]>, const char *<[file]>, |
---|
32 | const char *<[mode]>, FILE *<[fp]>); |
---|
33 | |
---|
34 | DESCRIPTION |
---|
35 | Use this variant of <<fopen64>> if you wish to specify a particular file |
---|
36 | descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for |
---|
37 | the file. |
---|
38 | |
---|
39 | If <[fp]> was associated with another file or stream, <<freopen64>> |
---|
40 | closes that other file or stream (but ignores any errors while closing |
---|
41 | it). |
---|
42 | |
---|
43 | <[file]> and <[mode]> are used just as in <<fopen>>. |
---|
44 | |
---|
45 | If <[file]> is <<NULL>>, the underlying stream is modified rather than |
---|
46 | closed. The file cannot be given a more permissive access mode (for |
---|
47 | example, a <[mode]> of "w" will fail on a read-only file descriptor), |
---|
48 | but can change status such as append or binary mode. If modification |
---|
49 | is not possible, failure occurs. |
---|
50 | |
---|
51 | RETURNS |
---|
52 | If successful, the result is the same as the argument <[fp]>. If the |
---|
53 | file cannot be opened as specified, the result is <<NULL>>. |
---|
54 | |
---|
55 | PORTABILITY |
---|
56 | <<freopen>> is a glibc extension. |
---|
57 | |
---|
58 | Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>, |
---|
59 | <<lseek64>>, <<open64>>, <<read>>, <<sbrk>>, <<write>>. |
---|
60 | */ |
---|
61 | |
---|
62 | #include <time.h> |
---|
63 | #include <stdio.h> |
---|
64 | #include <string.h> |
---|
65 | #include <errno.h> |
---|
66 | #include <fcntl.h> |
---|
67 | #include <stdlib.h> |
---|
68 | #include <sys/lock.h> |
---|
69 | #include "local.h" |
---|
70 | |
---|
71 | /* |
---|
72 | * Re-direct an existing, open (probably) file to some other file. |
---|
73 | */ |
---|
74 | |
---|
75 | #ifdef __LARGE64_FILES |
---|
76 | |
---|
77 | FILE * |
---|
78 | _freopen64_r (struct _reent *ptr, |
---|
79 | const char *file, |
---|
80 | const char *mode, |
---|
81 | register FILE *fp) |
---|
82 | { |
---|
83 | register int f; |
---|
84 | int flags, oflags, oflags2; |
---|
85 | int e = 0; |
---|
86 | |
---|
87 | |
---|
88 | CHECK_INIT (ptr, fp); |
---|
89 | |
---|
90 | /* We can't use the _newlib_flockfile_XXX macros here due to the |
---|
91 | interlocked locking with the sfp_lock. */ |
---|
92 | #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT |
---|
93 | int __oldcancel; |
---|
94 | pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldcancel); |
---|
95 | #endif |
---|
96 | oflags2 = fp->_flags2; |
---|
97 | if (!(oflags2 & __SNLK)) |
---|
98 | _flockfile (fp); |
---|
99 | |
---|
100 | if ((flags = __sflags (ptr, mode, &oflags)) == 0) |
---|
101 | { |
---|
102 | if (!(oflags2 & __SNLK)) |
---|
103 | _funlockfile (fp); |
---|
104 | #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT |
---|
105 | pthread_setcancelstate (__oldcancel, &__oldcancel); |
---|
106 | #endif |
---|
107 | _fclose_r (ptr, fp); |
---|
108 | return NULL; |
---|
109 | } |
---|
110 | |
---|
111 | /* |
---|
112 | * Remember whether the stream was open to begin with, and |
---|
113 | * which file descriptor (if any) was associated with it. |
---|
114 | * If it was attached to a descriptor, defer closing it, |
---|
115 | * so that, e.g., freopen("/dev/stdin", "r", stdin) works. |
---|
116 | * This is unnecessary if it was not a Unix file. |
---|
117 | */ |
---|
118 | |
---|
119 | if (fp->_flags == 0) |
---|
120 | fp->_flags = __SEOF; /* hold on to it */ |
---|
121 | else |
---|
122 | { |
---|
123 | if (fp->_flags & __SWR) |
---|
124 | _fflush_r (ptr, fp); |
---|
125 | /* |
---|
126 | * If close is NULL, closing is a no-op, hence pointless. |
---|
127 | * If file is NULL, the file should not be closed. |
---|
128 | */ |
---|
129 | if (fp->_close != NULL && file != NULL) |
---|
130 | fp->_close (ptr, fp->_cookie); |
---|
131 | } |
---|
132 | |
---|
133 | /* |
---|
134 | * Now get a new descriptor to refer to the new file, or reuse the |
---|
135 | * existing file descriptor if file is NULL. |
---|
136 | */ |
---|
137 | |
---|
138 | if (file != NULL) |
---|
139 | { |
---|
140 | f = _open64_r (ptr, (char *) file, oflags, 0666); |
---|
141 | e = ptr->_errno; |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | #ifdef HAVE_FCNTL |
---|
146 | int oldflags; |
---|
147 | /* |
---|
148 | * Reuse the file descriptor, but only if the new access mode is |
---|
149 | * equal or less permissive than the old. F_SETFL correctly |
---|
150 | * ignores creation flags. |
---|
151 | */ |
---|
152 | f = fp->_file; |
---|
153 | if ((oldflags = _fcntl_r (ptr, f, F_GETFL, 0)) == -1 |
---|
154 | || ! ((oldflags & O_ACCMODE) == O_RDWR |
---|
155 | || ((oldflags ^ oflags) & O_ACCMODE) == 0) |
---|
156 | || _fcntl_r (ptr, f, F_SETFL, oflags) == -1) |
---|
157 | f = -1; |
---|
158 | #else |
---|
159 | /* We cannot modify without fcntl support. */ |
---|
160 | f = -1; |
---|
161 | #endif |
---|
162 | |
---|
163 | #ifdef __SCLE |
---|
164 | /* |
---|
165 | * F_SETFL doesn't change textmode. Don't mess with modes of ttys. |
---|
166 | */ |
---|
167 | if (0 <= f && ! isatty (f) |
---|
168 | && setmode (f, oflags & (O_BINARY | O_TEXT)) == -1) |
---|
169 | f = -1; |
---|
170 | #endif |
---|
171 | |
---|
172 | if (f < 0) |
---|
173 | { |
---|
174 | e = EBADF; |
---|
175 | if (fp->_close != NULL) |
---|
176 | fp->_close (ptr, fp->_cookie); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | /* |
---|
181 | * Finish closing fp. Even if the open succeeded above, |
---|
182 | * we cannot keep fp->_base: it may be the wrong size. |
---|
183 | * This loses the effect of any setbuffer calls, |
---|
184 | * but stdio has always done this before. |
---|
185 | */ |
---|
186 | |
---|
187 | if (fp->_flags & __SMBF) |
---|
188 | _free_r (ptr, (char *) fp->_bf._base); |
---|
189 | fp->_w = 0; |
---|
190 | fp->_r = 0; |
---|
191 | fp->_p = NULL; |
---|
192 | fp->_bf._base = NULL; |
---|
193 | fp->_bf._size = 0; |
---|
194 | fp->_lbfsize = 0; |
---|
195 | if (HASUB (fp)) |
---|
196 | FREEUB (ptr, fp); |
---|
197 | fp->_ub._size = 0; |
---|
198 | if (HASLB (fp)) |
---|
199 | FREELB (ptr, fp); |
---|
200 | fp->_lb._size = 0; |
---|
201 | fp->_flags &= ~__SORD; |
---|
202 | fp->_flags2 &= ~__SWID; |
---|
203 | memset (&fp->_mbstate, 0, sizeof (_mbstate_t)); |
---|
204 | |
---|
205 | if (f < 0) |
---|
206 | { /* did not get it after all */ |
---|
207 | __sfp_lock_acquire (); |
---|
208 | fp->_flags = 0; /* set it free */ |
---|
209 | ptr->_errno = e; /* restore in case _close clobbered */ |
---|
210 | if (!(oflags2 & __SNLK)) |
---|
211 | _funlockfile (fp); |
---|
212 | #ifndef __SINGLE_THREAD__ |
---|
213 | __lock_close_recursive (fp->_lock); |
---|
214 | #endif |
---|
215 | __sfp_lock_release (); |
---|
216 | #if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS) |
---|
217 | pthread_setcancelstate (__oldcancel, &__oldcancel); |
---|
218 | #endif |
---|
219 | return NULL; |
---|
220 | } |
---|
221 | |
---|
222 | fp->_flags = flags; |
---|
223 | fp->_file = f; |
---|
224 | fp->_cookie = (void *) fp; |
---|
225 | fp->_read = __sread; |
---|
226 | fp->_write = __swrite64; |
---|
227 | fp->_seek = __sseek; |
---|
228 | fp->_seek64 = __sseek64; |
---|
229 | fp->_close = __sclose; |
---|
230 | |
---|
231 | #ifdef __SCLE |
---|
232 | if (__stextmode(fp->_file)) |
---|
233 | fp->_flags |= __SCLE; |
---|
234 | #endif |
---|
235 | |
---|
236 | fp->_flags |= __SL64; |
---|
237 | |
---|
238 | if (!(oflags2 & __SNLK)) |
---|
239 | _funlockfile (fp); |
---|
240 | #if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS) |
---|
241 | pthread_setcancelstate (__oldcancel, &__oldcancel); |
---|
242 | #endif |
---|
243 | return fp; |
---|
244 | } |
---|
245 | |
---|
246 | #ifndef _REENT_ONLY |
---|
247 | |
---|
248 | FILE * |
---|
249 | freopen64 (const char *file, |
---|
250 | const char *mode, |
---|
251 | register FILE *fp) |
---|
252 | { |
---|
253 | return _freopen64_r (_REENT, file, mode, fp); |
---|
254 | } |
---|
255 | |
---|
256 | #endif /* !_REENT_ONLY */ |
---|
257 | |
---|
258 | #endif /* __LARGE64_FILES */ |
---|