Update contrib.
3 * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
8 * Copyright (c) 1990 The Regents of the University of California.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that the above copyright notice and this paragraph are
13 * duplicated in all such forms and that any documentation,
14 * advertising materials, and other materials related to such
15 * distribution and use acknowledge that the software was developed
16 * by the University of California, Berkeley. The name of the
17 * University may not be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 <<fopen>>---open a file
35 FILE *fopen(const char *<[file]>, const char *<[mode]>);
37 FILE *_fopen_r(void *<[reent]>,
38 const char *<[file]>, const char *<[mode]>);
42 FILE *fopen(<[file]>, <[mode]>)
46 FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
52 <<fopen>> initializes the data structures needed to read or write a
53 file. Specify the file's name as the string at <[file]>, and the kind
54 of access you need to the file with the string at <[mode]>.
56 The alternate function <<_fopen_r>> is a reentrant version.
57 The extra argument <[reent]> is a pointer to a reentrancy structure.
59 Three fundamental kinds of access are available: read, write, and append.
60 <<*<[mode]>>> must begin with one of the three characters `<<r>>',
61 `<<w>>', or `<<a>>', to select one of these:
65 Open the file for reading; the operation will fail if the file does
66 not exist, or if the host system does not permit you to read it.
69 Open the file for writing @emph{from the beginning} of the file:
70 effectively, this always creates a new file. If the file whose name you
71 specified already existed, its old contents are discarded.
74 Open the file for appending data, that is writing from the end of
75 file. When you open a file this way, all data always goes to the
76 current end of file; you cannot change this using <<fseek>>.
79 Some host systems distinguish between ``binary'' and ``text'' files.
80 Such systems may perform data transformations on data written to, or
81 read from, files opened as ``text''.
82 If your system is one of these, then you can append a `<<b>>' to any
83 of the three modes above, to specify that you are opening the file as
84 a binary file (the default is to open the file as a text file).
86 `<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
87 `<<ab>>', ``append binary''.
89 To make C programs more portable, the `<<b>>' is accepted on all
90 systems, whether or not it makes a difference.
92 Finally, you might need to both read and write from the same file.
93 You can also append a `<<+>>' to any of the three modes, to permit
94 this. (If you want to append both `<<b>>' and `<<+>>', you can do it
95 in either order: for example, <<"rb+">> means the same thing as
96 <<"r+b">> when used as a mode string.)
98 Use <<"r+">> (or <<"rb+">>) to permit reading and writing anywhere in
99 an existing file, without discarding any data; <<"w+">> (or <<"wb+">>)
100 to create a new file (or begin by discarding all data from an old one)
101 that permits reading and writing anywhere in it; and <<"a+">> (or
102 <<"ab+">>) to permit reading anywhere in an existing file, but writing
106 <<fopen>> returns a file pointer which you can use for other file
107 operations, unless the file you requested could not be opened; in that
108 situation, the result is <<NULL>>. If the reason for failure was an
109 invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
112 <<fopen>> is required by ANSI C.
114 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
115 <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
121 #include <stdlib_r.h>
124 #define MaxFullName 255
128 A reentrant version of fopen().
130 EXPORT_C FILE * _fopen_r(struct _reent *ptr, const char *file, const char *mode)
132 wchar_t _wfile[MaxFullName+1];
133 wchar_t _wmode[MaxFullName+1];
135 if ((-1 != mbstowcs(_wfile, file, MaxFullName)) &&
136 (-1 != mbstowcs(_wmode, mode, MaxFullName)))
138 return _wfopen_r(ptr, _wfile, _wmode);
141 ptr->_errno = EILSEQ;
146 A reentrant version of wfopen().
148 EXPORT_C FILE * _wfopen_r(struct _reent *ptr, const wchar_t *file, const wchar_t *mode)
154 if ((flags = __sflags (ptr, mode, &oflags)) == 0)
156 if ((fp = __sfp (ptr)) == NULL)
159 if ((f = _wopen_r (fp->_data, file, oflags, 0666)) < 0)
161 fp->_flags = 0; /* release */
165 fp->_file = (short)f;
166 fp->_flags = (short)flags;
167 fp->_cookie = (void*) fp;
169 fp->_write = __swrite;
171 fp->_close = __sclose;
173 if (fp->_flags & __SAPP)
174 fseek (fp, 0, SEEK_END);
184 Opens the file which name is stored in the filename string
185 and returns a pointer to the file (stream).
186 Operations allowed to the file returned are defined by the mode parameter.
187 @return If the file has been succesfully opened
188 the function will return a pointer to the file.
189 Otherwise a NULL pointer is returned.
190 @param file name of the file to be opened.
191 This paramenter must follow operating system's specifications
192 and can include a path if the system supports it.
193 @param mode type of access requested
195 EXPORT_C FILE * fopen(const char *file, const char *mode)
197 return _fopen_r (_REENT, file, mode);
200 EXPORT_C FILE * wfopen(const wchar_t *file, const wchar_t *mode)
202 return _wfopen_r (_REENT, file, mode);