OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [stdio/] [freopen.c] - Blame information for rev 277

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
2
 * Copyright (c) 1990, 2007 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
<<freopen>>---open a file using an existing file descriptor
21
 
22
INDEX
23
        freopen
24
INDEX
25
        _freopen_r
26
 
27
ANSI_SYNOPSIS
28
        #include <stdio.h>
29
        FILE *freopen(const char *<[file]>, const char *<[mode]>,
30
                      FILE *<[fp]>);
31
        FILE *_freopen_r(struct _reent *<[ptr]>, const char *<[file]>,
32
                      const char *<[mode]>, FILE *<[fp]>);
33
 
34
TRAD_SYNOPSIS
35
        #include <stdio.h>
36
        FILE *freopen(<[file]>, <[mode]>, <[fp]>)
37
        char *<[file]>;
38
        char *<[mode]>;
39
        FILE *<[fp]>;
40
 
41
        FILE *_freopen_r(<[ptr]>, <[file]>, <[mode]>, <[fp]>)
42
        struct _reent *<[ptr]>;
43
        char *<[file]>;
44
        char *<[mode]>;
45
        FILE *<[fp]>;
46
 
47
DESCRIPTION
48
Use this variant of <<fopen>> if you wish to specify a particular file
49
descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for
50
the file.
51
 
52
If <[fp]> was associated with another file or stream, <<freopen>>
53
closes that other file or stream (but ignores any errors while closing
54
it).
55
 
56
<[file]> and <[mode]> are used just as in <<fopen>>.
57
 
58
If <[file]> is <<NULL>>, the underlying stream is modified rather than
59
closed.  The file cannot be given a more permissive access mode (for
60
example, a <[mode]> of "w" will fail on a read-only file descriptor),
61
but can change status such as append or binary mode.  If modification
62
is not possible, failure occurs.
63
 
64
RETURNS
65
If successful, the result is the same as the argument <[fp]>.  If the
66
file cannot be opened as specified, the result is <<NULL>>.
67
 
68
PORTABILITY
69
ANSI C requires <<freopen>>.
70
 
71
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
72
<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
73
*/
74
 
75
#include <_ansi.h>
76
#include <reent.h>
77
#include <time.h>
78
#include <stdio.h>
79
#include <string.h>
80
#include <errno.h>
81
#include <fcntl.h>
82
#include <stdlib.h>
83
#include <sys/lock.h>
84
#include "local.h"
85
 
86
/*
87
 * Re-direct an existing, open (probably) file to some other file.
88
 */
89
 
90
FILE *
91
_DEFUN(_freopen_r, (ptr, file, mode, fp),
92
       struct _reent *ptr _AND
93
       const char *file _AND
94
       const char *mode _AND
95
       register FILE *fp)
96
{
97
  register int f;
98
  int flags, oflags;
99
  int e = 0;
100
 
101
  __sfp_lock_acquire ();
102
 
103
  CHECK_INIT (ptr, fp);
104
 
105
  _flockfile (fp);
106
 
107
  if ((flags = __sflags (ptr, mode, &oflags)) == 0)
108
    {
109
      _funlockfile (fp);
110
      _fclose_r (ptr, fp);
111
      __sfp_lock_release ();
112
      return NULL;
113
    }
114
 
115
  /*
116
   * Remember whether the stream was open to begin with, and
117
   * which file descriptor (if any) was associated with it.
118
   * If it was attached to a descriptor, defer closing it,
119
   * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
120
   * This is unnecessary if it was not a Unix file.
121
   */
122
 
123
  if (fp->_flags == 0)
124
    fp->_flags = __SEOF;        /* hold on to it */
125
  else
126
    {
127
      if (fp->_flags & __SWR)
128
        _fflush_r (ptr, fp);
129
      /*
130
       * If close is NULL, closing is a no-op, hence pointless.
131
       * If file is NULL, the file should not be closed.
132
       */
133
      if (fp->_close != NULL && file != NULL)
134
        fp->_close (ptr, fp->_cookie);
135
    }
136
 
137
  /*
138
   * Now get a new descriptor to refer to the new file, or reuse the
139
   * existing file descriptor if file is NULL.
140
   */
141
 
142
  if (file != NULL)
143
    {
144
      f = _open_r (ptr, (char *) file, oflags, 0666);
145
      e = ptr->_errno;
146
    }
147
  else
148
    {
149
#ifdef HAVE_FCNTL
150
      int oldflags;
151
      /*
152
       * Reuse the file descriptor, but only if the new access mode is
153
       * equal or less permissive than the old.  F_SETFL correctly
154
       * ignores creation flags.
155
       */
156
      f = fp->_file;
157
      if ((oldflags = _fcntl_r (ptr, f, F_GETFL, 0)) == -1
158
          || ! ((oldflags & O_ACCMODE) == O_RDWR
159
                || ((oldflags ^ oflags) & O_ACCMODE) == 0)
160
          || _fcntl_r (ptr, f, F_SETFL, oflags) == -1)
161
        f = -1;
162
#else
163
      /* We cannot modify without fcntl support.  */
164
      f = -1;
165
#endif
166
 
167
#ifdef __SCLE
168
      /*
169
       * F_SETFL doesn't change textmode.  Don't mess with modes of ttys.
170
       */
171
      if (0 <= f && ! _isatty_r (ptr, f)
172
          && setmode (f, oflags & (O_BINARY | O_TEXT)) == -1)
173
        f = -1;
174
#endif
175
 
176
      if (f < 0)
177
        {
178
          e = EBADF;
179
          if (fp->_close != NULL)
180
            fp->_close (ptr, fp->_cookie);
181
        }
182
    }
183
 
184
  /*
185
   * Finish closing fp.  Even if the open succeeded above,
186
   * we cannot keep fp->_base: it may be the wrong size.
187
   * This loses the effect of any setbuffer calls,
188
   * but stdio has always done this before.
189
   */
190
 
191
  if (fp->_flags & __SMBF)
192
    _free_r (ptr, (char *) fp->_bf._base);
193
  fp->_w = 0;
194
  fp->_r = 0;
195
  fp->_p = NULL;
196
  fp->_bf._base = NULL;
197
  fp->_bf._size = 0;
198
  fp->_lbfsize = 0;
199
  if (HASUB (fp))
200
    FREEUB (ptr, fp);
201
  fp->_ub._size = 0;
202
  if (HASLB (fp))
203
    FREELB (ptr, fp);
204
  fp->_lb._size = 0;
205
  fp->_flags & ~__SORD;
206
  fp->_flags2 = 0;
207
  memset (&fp->_mbstate, 0, sizeof (_mbstate_t));
208
 
209
  if (f < 0)
210
    {                           /* did not get it after all */
211
      fp->_flags = 0;            /* set it free */
212
      ptr->_errno = e;          /* restore in case _close clobbered */
213
      _funlockfile (fp);
214
#ifndef __SINGLE_THREAD__
215
      __lock_close_recursive (fp->_lock);
216
#endif
217
      __sfp_lock_release ();
218
      return NULL;
219
    }
220
 
221
  fp->_flags = flags;
222
  fp->_file = f;
223
  fp->_cookie = (_PTR) fp;
224
  fp->_read = __sread;
225
  fp->_write = __swrite;
226
  fp->_seek = __sseek;
227
  fp->_close = __sclose;
228
 
229
#ifdef __SCLE
230
  if (__stextmode (fp->_file))
231
    fp->_flags |= __SCLE;
232
#endif
233
 
234
  _funlockfile (fp);
235
  __sfp_lock_release ();
236
  return fp;
237
}
238
 
239
#ifndef _REENT_ONLY
240
 
241
FILE *
242
_DEFUN(freopen, (file, mode, fp),
243
       _CONST char *file _AND
244
       _CONST char *mode _AND
245
       register FILE *fp)
246
{
247
  return _freopen_r (_REENT, file, mode, fp);
248
}
249
 
250
#endif /*!_REENT_ONLY */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.