sl@0: /* FOPEN.C
sl@0:  * 
sl@0:  * Portions Copyright (c) 1990-2005 Nokia Corporation and/or its subsidiary(-ies).
sl@0:  * All rights reserved.
sl@0:  */
sl@0: 
sl@0: /*
sl@0:  * Copyright (c) 1990 The Regents of the University of California.
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms are permitted
sl@0:  * provided that the above copyright notice and this paragraph are
sl@0:  * duplicated in all such forms and that any documentation,
sl@0:  * advertising materials, and other materials related to such
sl@0:  * distribution and use acknowledge that the software was developed
sl@0:  * by the University of California, Berkeley.  The name of the
sl@0:  * University may not be used to endorse or promote products derived
sl@0:  * from this software without specific prior written permission.
sl@0:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0:  */
sl@0: 
sl@0: /*
sl@0: FUNCTION
sl@0: <<fopen>>---open a file
sl@0: 
sl@0: INDEX
sl@0: 	fopen
sl@0: INDEX
sl@0: 	_fopen_r
sl@0: 
sl@0: ANSI_SYNOPSIS
sl@0: 	#include <stdio.h>
sl@0: 	FILE *fopen(const char *<[file]>, const char *<[mode]>);
sl@0: 
sl@0: 	FILE *_fopen_r(void *<[reent]>, 
sl@0:                        const char *<[file]>, const char *<[mode]>);
sl@0: 
sl@0: TRAD_SYNOPSIS
sl@0: 	#include <stdio.h>
sl@0: 	FILE *fopen(<[file]>, <[mode]>)
sl@0: 	char *<[file]>;
sl@0: 	char *<[mode]>;
sl@0: 
sl@0: 	FILE *_fopen_r(<[reent]>, <[file]>, <[mode]>)
sl@0: 	char *<[reent]>;
sl@0: 	char *<[file]>;
sl@0: 	char *<[mode]>;
sl@0: 
sl@0: DESCRIPTION
sl@0: <<fopen>> initializes the data structures needed to read or write a
sl@0: file.  Specify the file's name as the string at <[file]>, and the kind
sl@0: of access you need to the file with the string at <[mode]>.
sl@0: 
sl@0: The alternate function <<_fopen_r>> is a reentrant version.
sl@0: The extra argument <[reent]> is a pointer to a reentrancy structure.
sl@0: 
sl@0: Three fundamental kinds of access are available: read, write, and append.
sl@0: <<*<[mode]>>> must begin with one of the three characters `<<r>>',
sl@0: `<<w>>', or `<<a>>', to select one of these:
sl@0: 
sl@0: o+
sl@0: o r
sl@0: Open the file for reading; the operation will fail if the file does
sl@0: not exist, or if the host system does not permit you to read it.
sl@0: 
sl@0: o w
sl@0: Open the file for writing @emph{from the beginning} of the file:
sl@0: effectively, this always creates a new file.  If the file whose name you
sl@0: specified already existed, its old contents are discarded.
sl@0: 
sl@0: o a
sl@0: Open the file for appending data, that is writing from the end of
sl@0: file.  When you open a file this way, all data always goes to the
sl@0: current end of file; you cannot change this using <<fseek>>.
sl@0: o-
sl@0: 
sl@0: Some host systems distinguish between ``binary'' and ``text'' files.
sl@0: Such systems may perform data transformations on data written to, or
sl@0: read from, files opened as ``text''.
sl@0: If your system is one of these, then you can append a `<<b>>' to any
sl@0: of the three modes above, to specify that you are opening the file as
sl@0: a binary file (the default is to open the file as a text file).
sl@0: 
sl@0: `<<rb>>', then, means ``read binary''; `<<wb>>', ``write binary''; and
sl@0: `<<ab>>', ``append binary''.
sl@0: 
sl@0: To make C programs more portable, the `<<b>>' is accepted on all
sl@0: systems, whether or not it makes a difference.
sl@0: 
sl@0: Finally, you might need to both read and write from the same file.
sl@0: You can also append a `<<+>>' to any of the three modes, to permit
sl@0: this.  (If you want to append both `<<b>>' and `<<+>>', you can do it
sl@0: in either order: for example, <<"rb+">> means the same thing as
sl@0: <<"r+b">> when used as a mode string.)
sl@0: 
sl@0: Use <<"r+">> (or <<"rb+">>) to permit reading and writing anywhere in
sl@0: an existing file, without discarding any data; <<"w+">> (or <<"wb+">>)
sl@0: to create a new file (or begin by discarding all data from an old one)
sl@0: that permits reading and writing anywhere in it; and <<"a+">> (or
sl@0: <<"ab+">>) to permit reading anywhere in an existing file, but writing
sl@0: only at the end.
sl@0: 
sl@0: RETURNS
sl@0: <<fopen>> returns a file pointer which you can use for other file
sl@0: operations, unless the file you requested could not be opened; in that
sl@0: situation, the result is <<NULL>>.  If the reason for failure was an
sl@0: invalid string at <[mode]>, <<errno>> is set to <<EINVAL>>.
sl@0: 
sl@0: PORTABILITY
sl@0: <<fopen>> is required by ANSI C.
sl@0: 
sl@0: Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
sl@0: <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
sl@0: */
sl@0: 
sl@0: #include <stdio_r.h>
sl@0: #include <errno.h>
sl@0: #include "LOCAL.H"
sl@0: #include <stdlib_r.h>
sl@0: 
sl@0: 
sl@0: #define MaxFullName 255
sl@0: 
sl@0: 
sl@0: /**
sl@0: A reentrant version of fopen().
sl@0: */
sl@0: EXPORT_C FILE * _fopen_r(struct _reent *ptr, const char *file, const char *mode)
sl@0: 	{
sl@0: 	wchar_t _wfile[MaxFullName+1];	
sl@0: 	wchar_t _wmode[MaxFullName+1];
sl@0: 
sl@0: 	if ((-1 != mbstowcs(_wfile, file, MaxFullName)) && 
sl@0: 		(-1 != mbstowcs(_wmode, mode, MaxFullName)))
sl@0: 	{
sl@0: 		return _wfopen_r(ptr, _wfile, _wmode);
sl@0: 	}
sl@0: 
sl@0: 	ptr->_errno = EILSEQ;
sl@0: 	return NULL;
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: A reentrant version of wfopen().
sl@0: */
sl@0: EXPORT_C FILE * _wfopen_r(struct _reent *ptr, const wchar_t *file, const wchar_t *mode)
sl@0: {
sl@0:   register FILE *fp;
sl@0:   register int f;
sl@0:   int flags, oflags;
sl@0: 
sl@0:   if ((flags = __sflags (ptr, mode, &oflags)) == 0)
sl@0:     return NULL;
sl@0:   if ((fp = __sfp (ptr)) == NULL)
sl@0:     return NULL;
sl@0: 
sl@0:   if ((f = _wopen_r (fp->_data, file, oflags, 0666)) < 0)
sl@0:     {
sl@0:       fp->_flags = 0;		/* release */
sl@0:       return NULL;
sl@0:     }
sl@0: 
sl@0:   fp->_file = (short)f;
sl@0:   fp->_flags = (short)flags;
sl@0:   fp->_cookie = (void*) fp;
sl@0:   fp->_read = __sread;
sl@0:   fp->_write = __swrite;
sl@0:   fp->_seek = __sseek;
sl@0:   fp->_close = __sclose;
sl@0: 
sl@0:   if (fp->_flags & __SAPP)
sl@0:     fseek (fp, 0, SEEK_END);
sl@0: 
sl@0:   return fp;
sl@0: }
sl@0: 
sl@0: 
sl@0: #ifndef _REENT_ONLY
sl@0: 
sl@0: /**
sl@0: Open a file.
sl@0: Opens the file which name is stored in the filename string
sl@0: and returns a pointer to the file (stream).
sl@0: Operations allowed to the file returned are defined by the mode parameter.
sl@0: @return If the file has been succesfully opened 
sl@0: the function will return a pointer to the file. 
sl@0: Otherwise a NULL pointer is returned.
sl@0: @param file name of the file to be opened. 
sl@0: This paramenter must follow operating system's specifications
sl@0: and can include a path if the system supports it. 
sl@0: @param mode type of access requested
sl@0: */
sl@0: EXPORT_C FILE * fopen(const char *file, const char *mode)
sl@0: {
sl@0:   return _fopen_r (_REENT, file, mode);
sl@0: }
sl@0: 
sl@0: EXPORT_C FILE * wfopen(const wchar_t *file, const wchar_t *mode)
sl@0: {
sl@0:   return _wfopen_r (_REENT, file, mode);
sl@0: }
sl@0: 
sl@0: #endif