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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [freopen.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/*
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
<<freopen>>---open a file using an existing file descriptor
21
 
22
INDEX
23
        freopen
24
 
25
ANSI_SYNOPSIS
26
        #include <stdio.h>
27
        FILE *freopen(const char *<[file]>, const char *<[mode]>,
28
                      FILE *<[fp]>);
29
 
30
TRAD_SYNOPSIS
31
        #include <stdio.h>
32
        FILE *freopen(<[file]>, <[mode]>, <[fp]>)
33
        char *<[file]>;
34
        char *<[mode]>;
35
        FILE *<[fp]>;
36
 
37
DESCRIPTION
38
Use this variant of <<fopen>> if you wish to specify a particular file
39
descriptor <[fp]> (notably <<stdin>>, <<stdout>>, or <<stderr>>) for
40
the file.
41
 
42
If <[fp]> was associated with another file or stream, <<freopen>>
43
closes that other file or stream (but ignores any errors while closing
44
it).
45
 
46
<[file]> and <[mode]> are used just as in <<fopen>>.
47
 
48
RETURNS
49
If successful, the result is the same as the argument <[fp]>.  If the
50
file cannot be opened as specified, the result is <<NULL>>.
51
 
52
PORTABILITY
53
ANSI C requires <<freopen>>.
54
 
55
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
56
<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
57
*/
58
 
59
#include <time.h>
60
#include <stdio.h>
61
#include <fcntl.h>
62
#include <stdlib.h>
63
#include "local.h"
64
 
65
/*
66
 * Re-direct an existing, open (probably) file to some other file.
67
 */
68
 
69
FILE *
70
_DEFUN (freopen, (file, mode, fp),
71
        _CONST char *file _AND
72
        _CONST char *mode _AND
73
        register FILE *fp)
74
{
75
  register int f;
76
  int flags, oflags, e;
77
  struct _reent *ptr;
78
 
79
  CHECK_INIT (fp);
80
  ptr = fp->_data;
81
 
82
  if ((flags = __sflags (ptr, mode, &oflags)) == 0)
83
    {
84
      (void) fclose (fp);
85
      return NULL;
86
    }
87
 
88
  /*
89
   * Remember whether the stream was open to begin with, and
90
   * which file descriptor (if any) was associated with it.
91
   * If it was attached to a descriptor, defer closing it,
92
   * so that, e.g., freopen("/dev/stdin", "r", stdin) works.
93
   * This is unnecessary if it was not a Unix file.
94
   */
95
 
96
  if (fp->_flags == 0)
97
    fp->_flags = __SEOF;        /* hold on to it */
98
  else
99
    {
100
      if (fp->_flags & __SWR)
101
        (void) fflush (fp);
102
      /* if close is NULL, closing is a no-op, hence pointless */
103
      if (fp->_close != NULL)
104
        (void) (*fp->_close) (fp->_cookie);
105
    }
106
 
107
  /*
108
   * Now get a new descriptor to refer to the new file.
109
   */
110
 
111
  f = _open_r (ptr, (char *) file, oflags, 0666);
112
  e = ptr->_errno;
113
 
114
  /*
115
   * Finish closing fp.  Even if the open succeeded above,
116
   * we cannot keep fp->_base: it may be the wrong size.
117
   * This loses the effect of any setbuffer calls,
118
   * but stdio has always done this before.
119
   */
120
 
121
  if (fp->_flags & __SMBF)
122
    _free_r (ptr, (char *) fp->_bf._base);
123
  fp->_w = 0;
124
  fp->_r = 0;
125
  fp->_p = NULL;
126
  fp->_bf._base = NULL;
127
  fp->_bf._size = 0;
128
  fp->_lbfsize = 0;
129
  if (HASUB (fp))
130
    FREEUB (fp);
131
  fp->_ub._size = 0;
132
  if (HASLB (fp))
133
    FREELB (fp);
134
  fp->_lb._size = 0;
135
 
136
  if (f < 0)
137
    {                           /* did not get it after all */
138
      fp->_flags = 0;            /* set it free */
139
      ptr->_errno = e;          /* restore in case _close clobbered */
140
      return NULL;
141
    }
142
 
143
  fp->_flags = flags;
144
  fp->_file = f;
145
  fp->_cookie = (_PTR) fp;
146
  fp->_read = __sread;
147
  fp->_write = __swrite;
148
  fp->_seek = __sseek;
149
  fp->_close = __sclose;
150
 
151
#ifdef __SCLE
152
  if (__stextmode(fp->_file))
153
    fp->_flags |= __SCLE;
154
#endif
155
 
156
  return fp;
157
}

powered by: WebSVN 2.1.0

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