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  * and/or 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 #define _DEFAULT_SOURCE
63 #include <time.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <errno.h>
67 #include <fcntl.h>
68 #include <stdlib.h>
69 #include <sys/lock.h>
70 #include "../stdio/local.h"
71 
72 /*
73  * Re-direct an existing, open (probably) file to some other file.
74  */
75 
76 #ifdef __LARGE64_FILES
77 
78 FILE *
freopen64(const char * file,const char * mode,register FILE * fp)79 freopen64 (
80 	const char *file,
81 	const char *mode,
82 	register FILE *fp)
83 {
84   register int f;
85   int flags, oflags, oflags2;
86   int e = 0;
87 
88 
89   CHECK_INIT (ptr, fp);
90 
91   /* We can't use the _newlib_flockfile_XXX macros here due to the
92      interlocked locking with the sfp_lock. */
93 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
94   int __oldcancel;
95   pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &__oldcancel);
96 #endif
97   oflags2 = fp->_flags2;
98   if (!(oflags2 & __SNLK))
99     _flockfile (fp);
100 
101   if ((flags = __sflags (mode, &oflags)) == 0)
102     {
103       if (!(oflags2 & __SNLK))
104 	_funlockfile (fp);
105 #ifdef _STDIO_WITH_THREAD_CANCELLATION_SUPPORT
106       pthread_setcancelstate (__oldcancel, &__oldcancel);
107 #endif
108       fclose (fp);
109       return NULL;
110     }
111 
112   /*
113    * Remember whether the stream was open to begin with, and
114    * which file descriptor (if any) was associated with it.
115    * If it was attached to a descriptor, defer closing it,
116    * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
117    * This is unnecessary if it was not a Unix file.
118    */
119 
120   if (fp->_flags == 0)
121     fp->_flags = __SEOF;	/* hold on to it */
122   else
123     {
124       if (fp->_flags & __SWR)
125 	fflush (fp);
126       /*
127        * If close is NULL, closing is a no-op, hence pointless.
128        * If file is NULL, the file should not be closed.
129        */
130       if (fp->_close != NULL && file != NULL)
131 	fp->_close (fp->_cookie);
132     }
133 
134   /*
135    * Now get a new descriptor to refer to the new file, or reuse the
136    * existing file descriptor if file is NULL.
137    */
138 
139   if (file != NULL)
140     {
141       f = open64 ((char *) file, oflags, 0666);
142       e = _REENT_ERRNO(ptr);
143     }
144   else
145     {
146 #ifdef _HAVE_FCNTL
147       int oldflags;
148       /*
149        * Reuse the file descriptor, but only if the new access mode is
150        * equal or less permissive than the old.  F_SETFL correctly
151        * ignores creation flags.
152        */
153       f = fp->_file;
154       if ((oldflags = fcntl (f, F_GETFL, 0)) == -1
155 	  || ! ((oldflags & O_ACCMODE) == O_RDWR
156 		|| ((oldflags ^ oflags) & O_ACCMODE) == 0)
157 	  || fcntl (f, F_SETFL, oflags) == -1)
158 	f = -1;
159 #else
160       /* We cannot modify without fcntl support.  */
161       f = -1;
162 #endif
163 
164 #ifdef __SCLE
165       /*
166        * F_SETFL doesn't change textmode.  Don't mess with modes of ttys.
167        */
168       if (0 <= f && ! isatty (f)
169 	  && setmode (f, oflags & (O_BINARY | O_TEXT)) == -1)
170 	f = -1;
171 #endif
172 
173       if (f < 0)
174 	{
175 	  e = EBADF;
176 	  if (fp->_close != NULL)
177 	    fp->_close (fp->_cookie);
178 	}
179     }
180 
181   /*
182    * Finish closing fp.  Even if the open succeeded above,
183    * we cannot keep fp->_base: it may be the wrong size.
184    * This loses the effect of any setbuffer calls,
185    * but stdio has always done this before.
186    */
187 
188   if (fp->_flags & __SMBF)
189     free ((char *) fp->_bf._base);
190   fp->_w = 0;
191   fp->_r = 0;
192   fp->_p = NULL;
193   fp->_bf._base = NULL;
194   fp->_bf._size = 0;
195   fp->_lbfsize = 0;
196   if (HASUB (fp))
197     FREEUB (ptr, fp);
198   fp->_ub._size = 0;
199   if (HASLB (fp))
200     FREELB (ptr, fp);
201   fp->_lb._size = 0;
202   fp->_flags &= ~__SORD;
203   fp->_flags2 &= ~__SWID;
204   memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
205 
206   if (f < 0)
207     {				/* did not get it after all */
208       __sfp_lock_acquire ();
209       fp->_flags = 0;		/* set it free */
210       _REENT_ERRNO(ptr) = e;	/* restore in case _close clobbered */
211       if (!(oflags2 & __SNLK))
212 	_funlockfile (fp);
213 #ifndef __SINGLE_THREAD__
214       __lock_close_recursive (fp->_lock);
215 #endif
216       __sfp_lock_release ();
217 #if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS)
218       pthread_setcancelstate (__oldcancel, &__oldcancel);
219 #endif
220       return NULL;
221     }
222 
223   fp->_flags = flags;
224   fp->_file = f;
225   fp->_cookie = (void *) fp;
226   fp->_read = __sread;
227   fp->_write = __swrite64;
228   fp->_seek = __sseek;
229   fp->_seek64 = __sseek64;
230   fp->_close = __sclose;
231 
232 #ifdef __SCLE
233   if (__stextmode(fp->_file))
234     fp->_flags |= __SCLE;
235 #endif
236 
237   fp->_flags |= __SL64;
238 
239   if (!(oflags2 & __SNLK))
240     _funlockfile (fp);
241 #if !defined (__SINGLE_THREAD__) && defined (_POSIX_THREADS)
242   pthread_setcancelstate (__oldcancel, &__oldcancel);
243 #endif
244   return fp;
245 }
246 
247 #endif /* __LARGE64_FILES */
248