OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-newlib/] [newlib-1.17.0/] [newlib/] [libc/] [stdio64/] [freopen64.c] - Blame information for rev 9

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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