1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FOPEN.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,205 @@
1.4 +/* FOPEN.C
1.5 + *
1.6 + * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
1.7 + * All rights reserved.
1.8 + */
1.9 +
1.10 +/*
1.11 + * Copyright (c) 1990 The Regents of the University of California.
1.12 + * All rights reserved.
1.13 + *
1.14 + * Redistribution and use in source and binary forms are permitted
1.15 + * provided that the above copyright notice and this paragraph are
1.16 + * duplicated in all such forms and that any documentation,
1.17 + * advertising materials, and other materials related to such
1.18 + * distribution and use acknowledge that the software was developed
1.19 + * by the University of California, Berkeley. The name of the
1.20 + * University may not be used to endorse or promote products derived
1.21 + * from this software without specific prior written permission.
1.22 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1.23 + * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1.24 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1.25 + */
1.26 +
1.27 +/*
1.28 +FUNCTION
1.29 +<<fopen>>---open a file
1.30 +
1.31 +INDEX
1.32 + fopen
1.33 +INDEX
1.34 + _fopen_r
1.35 +
1.36 +ANSI_SYNOPSIS
1.37 + #include <stdio.h>
1.38 + FILE *fopen(const char *<[file]>, const char *<[mode]>);
1.39 +
1.40 + FILE *_fopen_r(void *<[reent]>,
1.41 + const char *<[file]>, const char *<[mode]>);
1.42 +
1.43 +TRAD_SYNOPSIS
1.44 + #include <stdio.h>
1.45 + FILE *fopen(<[file]>, <[mode]>)
1.46 + char *<[file]>;
1.47 + char *<[mode]>;
1.48 +
1.49 + FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
1.50 + char *<[reent]>;
1.51 + char *<[file]>;
1.52 + char *<[mode]>;
1.53 +
1.54 +DESCRIPTION
1.55 +<<fopen>> initializes the data structures needed to read or write a
1.56 +file. Specify the file's name as the string at <[file]>, and the kind
1.57 +of access you need to the file with the string at <[mode]>.
1.58 +
1.59 +The alternate function <<_fopen_r>> is a reentrant version.
1.60 +The extra argument <[reent]> is a pointer to a reentrancy structure.
1.61 +
1.62 +Three fundamental kinds of access are available: read, write, and append.
1.63 +<<*<[mode]>>> must begin with one of the three characters `<<r>>',
1.64 +`<<w>>', or `<<a>>', to select one of these:
1.65 +
1.66 +o+
1.67 +o r
1.68 +Open the file for reading; the operation will fail if the file does
1.69 +not exist, or if the host system does not permit you to read it.
1.70 +
1.71 +o w
1.72 +Open the file for writing @emph{from the beginning} of the file:
1.73 +effectively, this always creates a new file. If the file whose name you
1.74 +specified already existed, its old contents are discarded.
1.75 +
1.76 +o a
1.77 +Open the file for appending data, that is writing from the end of
1.78 +file. When you open a file this way, all data always goes to the
1.79 +current end of file; you cannot change this using <<fseek>>.
1.80 +o-
1.81 +
1.82 +Some host systems distinguish between ``binary'' and ``text'' files.
1.83 +Such systems may perform data transformations on data written to, or
1.84 +read from, files opened as ``text''.
1.85 +If your system is one of these, then you can append a `<<b>>' to any
1.86 +of the three modes above, to specify that you are opening the file as
1.87 +a binary file (the default is to open the file as a text file).
1.88 +
1.89 +`<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
1.90 +`<<ab>>', ``append binary''.
1.91 +
1.92 +To make C programs more portable, the `<<b>>' is accepted on all
1.93 +systems, whether or not it makes a difference.
1.94 +
1.95 +Finally, you might need to both read and write from the same file.
1.96 +You can also append a `<<+>>' to any of the three modes, to permit
1.97 +this. (If you want to append both `<<b>>' and `<<+>>', you can do it
1.98 +in either order: for example, <<"rb+">> means the same thing as
1.99 +<<"r+b">> when used as a mode string.)
1.100 +
1.101 +Use <<"r+">> (or <<"rb+">>) to permit reading and writing anywhere in
1.102 +an existing file, without discarding any data; <<"w+">> (or <<"wb+">>)
1.103 +to create a new file (or begin by discarding all data from an old one)
1.104 +that permits reading and writing anywhere in it; and <<"a+">> (or
1.105 +<<"ab+">>) to permit reading anywhere in an existing file, but writing
1.106 +only at the end.
1.107 +
1.108 +RETURNS
1.109 +<<fopen>> returns a file pointer which you can use for other file
1.110 +operations, unless the file you requested could not be opened; in that
1.111 +situation, the result is <<NULL>>. If the reason for failure was an
1.112 +invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
1.113 +
1.114 +PORTABILITY
1.115 +<<fopen>> is required by ANSI C.
1.116 +
1.117 +Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
1.118 +<<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
1.119 +*/
1.120 +
1.121 +#include <stdio_r.h>
1.122 +#include <errno.h>
1.123 +#include "LOCAL.H"
1.124 +#include <stdlib_r.h>
1.125 +
1.126 +
1.127 +#define MaxFullName 255
1.128 +
1.129 +
1.130 +/**
1.131 +A reentrant version of fopen().
1.132 +*/
1.133 +EXPORT_C FILE * _fopen_r(struct _reent *ptr, const char *file, const char *mode)
1.134 + {
1.135 + wchar_t _wfile[MaxFullName+1];
1.136 + wchar_t _wmode[MaxFullName+1];
1.137 +
1.138 + if ((-1 != mbstowcs(_wfile, file, MaxFullName)) &&
1.139 + (-1 != mbstowcs(_wmode, mode, MaxFullName)))
1.140 + {
1.141 + return _wfopen_r(ptr, _wfile, _wmode);
1.142 + }
1.143 +
1.144 + ptr->_errno = EILSEQ;
1.145 + return NULL;
1.146 + }
1.147 +
1.148 +/**
1.149 +A reentrant version of wfopen().
1.150 +*/
1.151 +EXPORT_C FILE * _wfopen_r(struct _reent *ptr, const wchar_t *file, const wchar_t *mode)
1.152 +{
1.153 + register FILE *fp;
1.154 + register int f;
1.155 + int flags, oflags;
1.156 +
1.157 + if ((flags = __sflags (ptr, mode, &oflags)) == 0)
1.158 + return NULL;
1.159 + if ((fp = __sfp (ptr)) == NULL)
1.160 + return NULL;
1.161 +
1.162 + if ((f = _wopen_r (fp->_data, file, oflags, 0666)) < 0)
1.163 + {
1.164 + fp->_flags = 0; /* release */
1.165 + return NULL;
1.166 + }
1.167 +
1.168 + fp->_file = (short)f;
1.169 + fp->_flags = (short)flags;
1.170 + fp->_cookie = (void*) fp;
1.171 + fp->_read = __sread;
1.172 + fp->_write = __swrite;
1.173 + fp->_seek = __sseek;
1.174 + fp->_close = __sclose;
1.175 +
1.176 + if (fp->_flags & __SAPP)
1.177 + fseek (fp, 0, SEEK_END);
1.178 +
1.179 + return fp;
1.180 +}
1.181 +
1.182 +
1.183 +#ifndef _REENT_ONLY
1.184 +
1.185 +/**
1.186 +Open a file.
1.187 +Opens the file which name is stored in the filename string
1.188 +and returns a pointer to the file (stream).
1.189 +Operations allowed to the file returned are defined by the mode parameter.
1.190 +@return If the file has been succesfully opened
1.191 +the function will return a pointer to the file.
1.192 +Otherwise a NULL pointer is returned.
1.193 +@param file name of the file to be opened.
1.194 +This paramenter must follow operating system's specifications
1.195 +and can include a path if the system supports it.
1.196 +@param mode type of access requested
1.197 +*/
1.198 +EXPORT_C FILE * fopen(const char *file, const char *mode)
1.199 +{
1.200 + return _fopen_r (_REENT, file, mode);
1.201 +}
1.202 +
1.203 +EXPORT_C FILE * wfopen(const wchar_t *file, const wchar_t *mode)
1.204 +{
1.205 + return _wfopen_r (_REENT, file, mode);
1.206 +}
1.207 +
1.208 +#endif