Update contrib.
3 * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
8 * Copyright (c) 1990 The Regents of the University of California.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that the above copyright notice and this paragraph are
13 * duplicated in all such forms and that any documentation,
14 * advertising materials, and other materials related to such
15 * distribution and use acknowledge that the software was developed
16 * by the University of California, Berkeley. The name of the
17 * University may not be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 <<freopen>>---open a file using an existing file descriptor
33 FILE *freopen(const char *<[file]>, const char *<[mode]>,
38 FILE *freopen(<[file]>, <[mode]>, <[fp]>)
44 Use this variant of <<fopen>> if you wish to specify a particular file
45 descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for
48 If <[fp]> was associated with another file or stream, <<freopen>>
49 closes that other file or stream (but ignores any errors while closing
52 <[file]> and <[mode]> are used just as in <<fopen>>.
55 If successful, the result is the same as the argument <[fp]>. If the
56 file cannot be opened as specified, the result is <<NULL>>.
59 ANSI C requires <<freopen>>.
61 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
62 <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
73 * Re-direct an existing, open (probably) file to some other file.
76 #define MaxFullName 255
79 Reopen a stream with a different file and mode.
80 @return If the file has been succesfully reopened the function returns a pointer to the file.
81 Otherwise a NULL pointer is returned.
82 @param file name of the file to be opened.
83 This paramenter must follow operating system's specifications and can include a path
84 if the system supports it.
85 @param mode type of access requested.
86 @param fp pointer to open file that has to be reopened.
88 EXPORT_C FILE * freopen (const char *file, const char *mode, FILE *fp)
90 wchar_t _wfile[MaxFullName+1];
91 wchar_t _wmode[MaxFullName+1];
94 if ((-1 != mbstowcs(_wfile, file, MaxFullName)) &&
95 (-1 != mbstowcs(_wmode, mode, MaxFullName)))
97 return wfreopen(_wfile, _wmode, fp);
102 ptr->_errno = EILSEQ;
106 EXPORT_C FILE * wfreopen (const wchar_t *file, const wchar_t *mode, FILE *fp)
109 int isopen, flags, oflags, e;
115 if ((flags = __sflags (ptr, mode, &oflags)) == 0)
122 * Remember whether the stream was open to begin with, and
123 * which file descriptor (if any) was associated with it.
124 * If it was attached to a descriptor, defer closing it,
125 * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
126 * This is unnecessary if it was not a Unix file.
131 fp->_flags = __SEOF; /* hold on to it */
136 if (fp->_flags & __SWR)
138 /* if close is NULL, closing is a no-op, hence pointless */
139 isopen = fp->_close != NULL;
140 if (fp->_file < 0 && isopen)
142 (void) (*fp->_close) (fp->_cookie);
148 * Now get a new descriptor to refer to the new file.
151 f = _wopen_r (ptr, (wchar_t *)file, oflags, 0666);
155 * May have used up all descriptors, so close the old
158 (void) (*fp->_close) (fp->_cookie);
160 f = _wopen_r (ptr, (wchar_t *) file, oflags, 0666);
165 * Finish closing fp. Even if the open succeeded above,
166 * we cannot keep fp->_base: it may be the wrong size.
167 * This loses the effect of any setbuffer calls,
168 * but stdio has always done this before.
172 (void) (*fp->_close) (fp->_cookie);
173 if (fp->_flags & __SMBF)
174 _free_r (ptr, (char *) fp->_bf._base);
178 fp->_bf._base = NULL;
189 { /* did not get it after all */
190 fp->_flags = 0; /* set it free */
191 ptr->_errno = e; /* restore in case _close clobbered */
195 fp->_flags = (short)flags;
196 fp->_file = (short)f;
197 fp->_cookie = (void*) fp;
199 fp->_write = __swrite;
201 fp->_close = __sclose;