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

Subversion Repositories openrisc_me

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
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
<<fopen>>---open a file
21
 
22
INDEX
23
        fopen
24
INDEX
25
        _fopen_r
26
 
27
ANSI_SYNOPSIS
28
        #include <stdio.h>
29
        FILE *fopen(const char *<[file]>, const char *<[mode]>);
30
 
31
        FILE *_fopen_r(struct _reent *<[reent]>,
32
                       const char *<[file]>, const char *<[mode]>);
33
 
34
TRAD_SYNOPSIS
35
        #include <stdio.h>
36
        FILE *fopen(<[file]>, <[mode]>)
37
        char *<[file]>;
38
        char *<[mode]>;
39
 
40
        FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
41
        struct _reent *<[reent]>;
42
        char *<[file]>;
43
        char *<[mode]>;
44
 
45
DESCRIPTION
46
<<fopen>> initializes the data structures needed to read or write a
47
file.  Specify the file's name as the string at <[file]>, and the kind
48
of access you need to the file with the string at <[mode]>.
49
 
50
The alternate function <<_fopen_r>> is a reentrant version.
51
The extra argument <[reent]> is a pointer to a reentrancy structure.
52
 
53
Three fundamental kinds of access are available: read, write, and append.
54
<<*<[mode]>>> must begin with one of the three characters `<<r>>',
55
`<<w>>', or `<<a>>', to select one of these:
56
 
57
o+
58
o r
59
Open the file for reading; the operation will fail if the file does
60
not exist, or if the host system does not permit you to read it.
61
 
62
o w
63
Open the file for writing @emph{from the beginning} of the file:
64
effectively, this always creates a new file.  If the file whose name you
65
specified already existed, its old contents are discarded.
66
 
67
o a
68
Open the file for appending data, that is writing from the end of
69
file.  When you open a file this way, all data always goes to the
70
current end of file; you cannot change this using <<fseek>>.
71
o-
72
 
73
Some host systems distinguish between ``binary'' and ``text'' files.
74
Such systems may perform data transformations on data written to, or
75
read from, files opened as ``text''.
76
If your system is one of these, then you can append a `<<b>>' to any
77
of the three modes above, to specify that you are opening the file as
78
a binary file (the default is to open the file as a text file).
79
 
80
`<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
81
`<<ab>>', ``append binary''.
82
 
83
To make C programs more portable, the `<<b>>' is accepted on all
84
systems, whether or not it makes a difference.
85
 
86
Finally, you might need to both read and write from the same file.
87
You can also append a `<<+>>' to any of the three modes, to permit
88
this.  (If you want to append both `<<b>>' and `<<+>>', you can do it
89
in either order: for example, <<"rb+">> means the same thing as
90
<<"r+b">> when used as a mode string.)
91
 
92
Use <<"r+">> (or <<"rb+">>) to permit reading and writing anywhere in
93
an existing file, without discarding any data; <<"w+">> (or <<"wb+">>)
94
to create a new file (or begin by discarding all data from an old one)
95
that permits reading and writing anywhere in it; and <<"a+">> (or
96
<<"ab+">>) to permit reading anywhere in an existing file, but writing
97
only at the end.
98
 
99
RETURNS
100
<<fopen>> returns a file pointer which you can use for other file
101
operations, unless the file you requested could not be opened; in that
102
situation, the result is <<NULL>>.  If the reason for failure was an
103
invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
104
 
105
PORTABILITY
106
<<fopen>> is required by ANSI C.
107
 
108
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
109
<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
110
*/
111
 
112
#if defined(LIBC_SCCS) && !defined(lint)
113
static char sccsid[] = "%W% (Berkeley) %G%";
114
#endif /* LIBC_SCCS and not lint */
115
 
116
#include <_ansi.h>
117
#include <reent.h>
118
#include <stdio.h>
119
#include <errno.h>
120
#include <sys/lock.h>
121
#ifdef __CYGWIN__
122
#include <fcntl.h>
123
#endif
124
#include "local.h"
125
 
126
FILE *
127
_DEFUN(_fopen_r, (ptr, file, mode),
128
       struct _reent *ptr _AND
129
       _CONST char *file _AND
130
       _CONST char *mode)
131
{
132
  register FILE *fp;
133
  register int f;
134
  int flags, oflags;
135
 
136
  if ((flags = __sflags (ptr, mode, &oflags)) == 0)
137
    return NULL;
138
  if ((fp = __sfp (ptr)) == NULL)
139
    return NULL;
140
 
141
  if ((f = _open_r (ptr, file, oflags, 0666)) < 0)
142
    {
143
      __sfp_lock_acquire ();
144
      fp->_flags = 0;            /* release */
145
#ifndef __SINGLE_THREAD__
146
      __lock_close_recursive (fp->_lock);
147
#endif
148
      __sfp_lock_release ();
149
      return NULL;
150
    }
151
 
152
  _flockfile (fp);
153
 
154
  fp->_file = f;
155
  fp->_flags = flags;
156
  fp->_cookie = (_PTR) fp;
157
  fp->_read = __sread;
158
  fp->_write = __swrite;
159
  fp->_seek = __sseek;
160
  fp->_close = __sclose;
161
 
162
  if (fp->_flags & __SAPP)
163
    _fseek_r (ptr, fp, 0, SEEK_END);
164
 
165
#ifdef __SCLE
166
  if (__stextmode (fp->_file))
167
    fp->_flags |= __SCLE;
168
#endif
169
 
170
  _funlockfile (fp);
171
  return fp;
172
}
173
 
174
#ifndef _REENT_ONLY
175
 
176
FILE *
177
_DEFUN(fopen, (file, mode),
178
       _CONST char *file _AND
179
       _CONST char *mode)
180
{
181
  return _fopen_r (_REENT, file, mode);
182
}
183
 
184
#endif

powered by: WebSVN 2.1.0

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