os/ossrv/genericopenlibs/cstdlib/LSTDIO/FOPEN.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* FOPEN.C
     2  * 
     3  * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
     4  * All rights reserved.
     5  */
     6 
     7 /*
     8  * Copyright (c) 1990 The Regents of the University of California.
     9  * All rights reserved.
    10  *
    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.
    22  */
    23 
    24 /*
    25 FUNCTION
    26 <<fopen>>---open a file
    27 
    28 INDEX
    29 	fopen
    30 INDEX
    31 	_fopen_r
    32 
    33 ANSI_SYNOPSIS
    34 	#include <stdio.h>
    35 	FILE *fopen(const char *<[file]>, const char *<[mode]>);
    36 
    37 	FILE *_fopen_r(void *<[reent]>, 
    38                        const char *<[file]>, const char *<[mode]>);
    39 
    40 TRAD_SYNOPSIS
    41 	#include <stdio.h>
    42 	FILE *fopen(<[file]>, <[mode]>)
    43 	char *<[file]>;
    44 	char *<[mode]>;
    45 
    46 	FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
    47 	char *<[reent]>;
    48 	char *<[file]>;
    49 	char *<[mode]>;
    50 
    51 DESCRIPTION
    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]>.
    55 
    56 The alternate function <<_fopen_r>> is a reentrant version.
    57 The extra argument <[reent]> is a pointer to a reentrancy structure.
    58 
    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:
    62 
    63 o+
    64 o r
    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.
    67 
    68 o w
    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.
    72 
    73 o a
    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>>.
    77 o-
    78 
    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).
    85 
    86 `<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
    87 `<<ab>>', ``append binary''.
    88 
    89 To make C programs more portable, the `<<b>>' is accepted on all
    90 systems, whether or not it makes a difference.
    91 
    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.)
    97 
    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
   103 only at the end.
   104 
   105 RETURNS
   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>>.
   110 
   111 PORTABILITY
   112 <<fopen>> is required by ANSI C.
   113 
   114 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
   115 <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
   116 */
   117 
   118 #include <stdio_r.h>
   119 #include <errno.h>
   120 #include "LOCAL.H"
   121 #include <stdlib_r.h>
   122 
   123 
   124 #define MaxFullName 255
   125 
   126 
   127 /**
   128 A reentrant version of fopen().
   129 */
   130 EXPORT_C FILE * _fopen_r(struct _reent *ptr, const char *file, const char *mode)
   131 	{
   132 	wchar_t _wfile[MaxFullName+1];	
   133 	wchar_t _wmode[MaxFullName+1];
   134 
   135 	if ((-1 != mbstowcs(_wfile, file, MaxFullName)) && 
   136 		(-1 != mbstowcs(_wmode, mode, MaxFullName)))
   137 	{
   138 		return _wfopen_r(ptr, _wfile, _wmode);
   139 	}
   140 
   141 	ptr->_errno = EILSEQ;
   142 	return NULL;
   143 	}
   144 
   145 /**
   146 A reentrant version of wfopen().
   147 */
   148 EXPORT_C FILE * _wfopen_r(struct _reent *ptr, const wchar_t *file, const wchar_t *mode)
   149 {
   150   register FILE *fp;
   151   register int f;
   152   int flags, oflags;
   153 
   154   if ((flags = __sflags (ptr, mode, &oflags)) == 0)
   155     return NULL;
   156   if ((fp = __sfp (ptr)) == NULL)
   157     return NULL;
   158 
   159   if ((f = _wopen_r (fp->_data, file, oflags, 0666)) < 0)
   160     {
   161       fp->_flags = 0;		/* release */
   162       return NULL;
   163     }
   164 
   165   fp->_file = (short)f;
   166   fp->_flags = (short)flags;
   167   fp->_cookie = (void*) fp;
   168   fp->_read = __sread;
   169   fp->_write = __swrite;
   170   fp->_seek = __sseek;
   171   fp->_close = __sclose;
   172 
   173   if (fp->_flags & __SAPP)
   174     fseek (fp, 0, SEEK_END);
   175 
   176   return fp;
   177 }
   178 
   179 
   180 #ifndef _REENT_ONLY
   181 
   182 /**
   183 Open a file.
   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
   194 */
   195 EXPORT_C FILE * fopen(const char *file, const char *mode)
   196 {
   197   return _fopen_r (_REENT, file, mode);
   198 }
   199 
   200 EXPORT_C FILE * wfopen(const wchar_t *file, const wchar_t *mode)
   201 {
   202   return _wfopen_r (_REENT, file, mode);
   203 }
   204 
   205 #endif