sl@0
|
1 |
/* FOPEN.C
|
sl@0
|
2 |
*
|
sl@0
|
3 |
* Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
4 |
* All rights reserved.
|
sl@0
|
5 |
*/
|
sl@0
|
6 |
|
sl@0
|
7 |
/*
|
sl@0
|
8 |
* Copyright (c) 1990 The Regents of the University of California.
|
sl@0
|
9 |
* All rights reserved.
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* Redistribution and use in source and binary forms are permitted
|
sl@0
|
12 |
* provided that the above copyright notice and this paragraph are
|
sl@0
|
13 |
* duplicated in all such forms and that any documentation,
|
sl@0
|
14 |
* advertising materials, and other materials related to such
|
sl@0
|
15 |
* distribution and use acknowledge that the software was developed
|
sl@0
|
16 |
* by the University of California, Berkeley. The name of the
|
sl@0
|
17 |
* University may not be used to endorse or promote products derived
|
sl@0
|
18 |
* from this software without specific prior written permission.
|
sl@0
|
19 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
sl@0
|
20 |
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
sl@0
|
21 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
sl@0
|
22 |
*/
|
sl@0
|
23 |
|
sl@0
|
24 |
/*
|
sl@0
|
25 |
FUNCTION
|
sl@0
|
26 |
<<fopen>>---open a file
|
sl@0
|
27 |
|
sl@0
|
28 |
INDEX
|
sl@0
|
29 |
fopen
|
sl@0
|
30 |
INDEX
|
sl@0
|
31 |
_fopen_r
|
sl@0
|
32 |
|
sl@0
|
33 |
ANSI_SYNOPSIS
|
sl@0
|
34 |
#include <stdio.h>
|
sl@0
|
35 |
FILE *fopen(const char *<[file]>, const char *<[mode]>);
|
sl@0
|
36 |
|
sl@0
|
37 |
FILE *_fopen_r(void *<[reent]>,
|
sl@0
|
38 |
const char *<[file]>, const char *<[mode]>);
|
sl@0
|
39 |
|
sl@0
|
40 |
TRAD_SYNOPSIS
|
sl@0
|
41 |
#include <stdio.h>
|
sl@0
|
42 |
FILE *fopen(<[file]>, <[mode]>)
|
sl@0
|
43 |
char *<[file]>;
|
sl@0
|
44 |
char *<[mode]>;
|
sl@0
|
45 |
|
sl@0
|
46 |
FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
|
sl@0
|
47 |
char *<[reent]>;
|
sl@0
|
48 |
char *<[file]>;
|
sl@0
|
49 |
char *<[mode]>;
|
sl@0
|
50 |
|
sl@0
|
51 |
DESCRIPTION
|
sl@0
|
52 |
<<fopen>> initializes the data structures needed to read or write a
|
sl@0
|
53 |
file. Specify the file's name as the string at <[file]>, and the kind
|
sl@0
|
54 |
of access you need to the file with the string at <[mode]>.
|
sl@0
|
55 |
|
sl@0
|
56 |
The alternate function <<_fopen_r>> is a reentrant version.
|
sl@0
|
57 |
The extra argument <[reent]> is a pointer to a reentrancy structure.
|
sl@0
|
58 |
|
sl@0
|
59 |
Three fundamental kinds of access are available: read, write, and append.
|
sl@0
|
60 |
<<*<[mode]>>> must begin with one of the three characters `<<r>>',
|
sl@0
|
61 |
`<<w>>', or `<<a>>', to select one of these:
|
sl@0
|
62 |
|
sl@0
|
63 |
o+
|
sl@0
|
64 |
o r
|
sl@0
|
65 |
Open the file for reading; the operation will fail if the file does
|
sl@0
|
66 |
not exist, or if the host system does not permit you to read it.
|
sl@0
|
67 |
|
sl@0
|
68 |
o w
|
sl@0
|
69 |
Open the file for writing @emph{from the beginning} of the file:
|
sl@0
|
70 |
effectively, this always creates a new file. If the file whose name you
|
sl@0
|
71 |
specified already existed, its old contents are discarded.
|
sl@0
|
72 |
|
sl@0
|
73 |
o a
|
sl@0
|
74 |
Open the file for appending data, that is writing from the end of
|
sl@0
|
75 |
file. When you open a file this way, all data always goes to the
|
sl@0
|
76 |
current end of file; you cannot change this using <<fseek>>.
|
sl@0
|
77 |
o-
|
sl@0
|
78 |
|
sl@0
|
79 |
Some host systems distinguish between ``binary'' and ``text'' files.
|
sl@0
|
80 |
Such systems may perform data transformations on data written to, or
|
sl@0
|
81 |
read from, files opened as ``text''.
|
sl@0
|
82 |
If your system is one of these, then you can append a `<<b>>' to any
|
sl@0
|
83 |
of the three modes above, to specify that you are opening the file as
|
sl@0
|
84 |
a binary file (the default is to open the file as a text file).
|
sl@0
|
85 |
|
sl@0
|
86 |
`<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
|
sl@0
|
87 |
`<<ab>>', ``append binary''.
|
sl@0
|
88 |
|
sl@0
|
89 |
To make C programs more portable, the `<<b>>' is accepted on all
|
sl@0
|
90 |
systems, whether or not it makes a difference.
|
sl@0
|
91 |
|
sl@0
|
92 |
Finally, you might need to both read and write from the same file.
|
sl@0
|
93 |
You can also append a `<<+>>' to any of the three modes, to permit
|
sl@0
|
94 |
this. (If you want to append both `<<b>>' and `<<+>>', you can do it
|
sl@0
|
95 |
in either order: for example, <<"rb+">> means the same thing as
|
sl@0
|
96 |
<<"r+b">> when used as a mode string.)
|
sl@0
|
97 |
|
sl@0
|
98 |
Use <<"r+">> (or <<"rb+">>) to permit reading and writing anywhere in
|
sl@0
|
99 |
an existing file, without discarding any data; <<"w+">> (or <<"wb+">>)
|
sl@0
|
100 |
to create a new file (or begin by discarding all data from an old one)
|
sl@0
|
101 |
that permits reading and writing anywhere in it; and <<"a+">> (or
|
sl@0
|
102 |
<<"ab+">>) to permit reading anywhere in an existing file, but writing
|
sl@0
|
103 |
only at the end.
|
sl@0
|
104 |
|
sl@0
|
105 |
RETURNS
|
sl@0
|
106 |
<<fopen>> returns a file pointer which you can use for other file
|
sl@0
|
107 |
operations, unless the file you requested could not be opened; in that
|
sl@0
|
108 |
situation, the result is <<NULL>>. If the reason for failure was an
|
sl@0
|
109 |
invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
|
sl@0
|
110 |
|
sl@0
|
111 |
PORTABILITY
|
sl@0
|
112 |
<<fopen>> is required by ANSI C.
|
sl@0
|
113 |
|
sl@0
|
114 |
Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
|
sl@0
|
115 |
<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
sl@0
|
116 |
*/
|
sl@0
|
117 |
|
sl@0
|
118 |
#include <stdio_r.h>
|
sl@0
|
119 |
#include <errno.h>
|
sl@0
|
120 |
#include "LOCAL.H"
|
sl@0
|
121 |
#include <stdlib_r.h>
|
sl@0
|
122 |
|
sl@0
|
123 |
|
sl@0
|
124 |
#define MaxFullName 255
|
sl@0
|
125 |
|
sl@0
|
126 |
|
sl@0
|
127 |
/**
|
sl@0
|
128 |
A reentrant version of fopen().
|
sl@0
|
129 |
*/
|
sl@0
|
130 |
EXPORT_C FILE * _fopen_r(struct _reent *ptr, const char *file, const char *mode)
|
sl@0
|
131 |
{
|
sl@0
|
132 |
wchar_t _wfile[MaxFullName+1];
|
sl@0
|
133 |
wchar_t _wmode[MaxFullName+1];
|
sl@0
|
134 |
|
sl@0
|
135 |
if ((-1 != mbstowcs(_wfile, file, MaxFullName)) &&
|
sl@0
|
136 |
(-1 != mbstowcs(_wmode, mode, MaxFullName)))
|
sl@0
|
137 |
{
|
sl@0
|
138 |
return _wfopen_r(ptr, _wfile, _wmode);
|
sl@0
|
139 |
}
|
sl@0
|
140 |
|
sl@0
|
141 |
ptr->_errno = EILSEQ;
|
sl@0
|
142 |
return NULL;
|
sl@0
|
143 |
}
|
sl@0
|
144 |
|
sl@0
|
145 |
/**
|
sl@0
|
146 |
A reentrant version of wfopen().
|
sl@0
|
147 |
*/
|
sl@0
|
148 |
EXPORT_C FILE * _wfopen_r(struct _reent *ptr, const wchar_t *file, const wchar_t *mode)
|
sl@0
|
149 |
{
|
sl@0
|
150 |
register FILE *fp;
|
sl@0
|
151 |
register int f;
|
sl@0
|
152 |
int flags, oflags;
|
sl@0
|
153 |
|
sl@0
|
154 |
if ((flags = __sflags (ptr, mode, &oflags)) == 0)
|
sl@0
|
155 |
return NULL;
|
sl@0
|
156 |
if ((fp = __sfp (ptr)) == NULL)
|
sl@0
|
157 |
return NULL;
|
sl@0
|
158 |
|
sl@0
|
159 |
if ((f = _wopen_r (fp->_data, file, oflags, 0666)) < 0)
|
sl@0
|
160 |
{
|
sl@0
|
161 |
fp->_flags = 0; /* release */
|
sl@0
|
162 |
return NULL;
|
sl@0
|
163 |
}
|
sl@0
|
164 |
|
sl@0
|
165 |
fp->_file = (short)f;
|
sl@0
|
166 |
fp->_flags = (short)flags;
|
sl@0
|
167 |
fp->_cookie = (void*) fp;
|
sl@0
|
168 |
fp->_read = __sread;
|
sl@0
|
169 |
fp->_write = __swrite;
|
sl@0
|
170 |
fp->_seek = __sseek;
|
sl@0
|
171 |
fp->_close = __sclose;
|
sl@0
|
172 |
|
sl@0
|
173 |
if (fp->_flags & __SAPP)
|
sl@0
|
174 |
fseek (fp, 0, SEEK_END);
|
sl@0
|
175 |
|
sl@0
|
176 |
return fp;
|
sl@0
|
177 |
}
|
sl@0
|
178 |
|
sl@0
|
179 |
|
sl@0
|
180 |
#ifndef _REENT_ONLY
|
sl@0
|
181 |
|
sl@0
|
182 |
/**
|
sl@0
|
183 |
Open a file.
|
sl@0
|
184 |
Opens the file which name is stored in the filename string
|
sl@0
|
185 |
and returns a pointer to the file (stream).
|
sl@0
|
186 |
Operations allowed to the file returned are defined by the mode parameter.
|
sl@0
|
187 |
@return If the file has been succesfully opened
|
sl@0
|
188 |
the function will return a pointer to the file.
|
sl@0
|
189 |
Otherwise a NULL pointer is returned.
|
sl@0
|
190 |
@param file name of the file to be opened.
|
sl@0
|
191 |
This paramenter must follow operating system's specifications
|
sl@0
|
192 |
and can include a path if the system supports it.
|
sl@0
|
193 |
@param mode type of access requested
|
sl@0
|
194 |
*/
|
sl@0
|
195 |
EXPORT_C FILE * fopen(const char *file, const char *mode)
|
sl@0
|
196 |
{
|
sl@0
|
197 |
return _fopen_r (_REENT, file, mode);
|
sl@0
|
198 |
}
|
sl@0
|
199 |
|
sl@0
|
200 |
EXPORT_C FILE * wfopen(const wchar_t *file, const wchar_t *mode)
|
sl@0
|
201 |
{
|
sl@0
|
202 |
return _wfopen_r (_REENT, file, mode);
|
sl@0
|
203 |
}
|
sl@0
|
204 |
|
sl@0
|
205 |
#endif
|