sl@0: /*
sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description:
sl@0: * FUNCTION
sl@0: * <<fdopen>>---turn open file into a stream
sl@0: * INDEX
sl@0: * fdopen
sl@0: * INDEX
sl@0: * _fdopen_r
sl@0: * ANSI_SYNOPSIS
sl@0: * #include <stdio.h>
sl@0: * FILE *fdopen(int <[fd]>, const char *<[mode]>);
sl@0: * FILE *_fdopen_r(void *<[reent]>,
sl@0: * int <[fd]>, const char *<[mode]>);
sl@0: * TRAD_SYNOPSIS
sl@0: * #include <stdio.h>
sl@0: * FILE *fdopen(<[fd]>, <[mode]>)
sl@0: * int <[fd]>;
sl@0: * char *<[mode]>;
sl@0: * FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
sl@0: * char *<[reent]>;
sl@0: * int <[fd]>;
sl@0: * char *<[mode]>);
sl@0: * <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
sl@0: * descriptor for an already-open file (returned, for example, by the
sl@0: * system subroutine <<open>> rather than by <<fopen>>).
sl@0: * The <[mode]> argument has the same meanings as in <<fopen>>.
sl@0: * RETURNS
sl@0: * File pointer or <<NULL>>, as for <<fopen>>.
sl@0: * PORTABILITY
sl@0: * <<fdopen>> is ANSI.
sl@0: * 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: 
sl@0: #include <sys/types.h>
sl@0: 
sl@0: #include <stdlib_r.h>
sl@0: #include <stdio_r.h>
sl@0: #include <errno.h>
sl@0: #include "LOCAL.H"
sl@0: 
sl@0: 
sl@0: #define MaxFullName 255
sl@0: /**
sl@0: A reentrant version of fdopen().
sl@0: */
sl@0: EXPORT_C FILE * _fdopen_r (struct _reent *ptr, int fd, const char *mode)
sl@0: 	{
sl@0: 	wchar_t _wmode[MaxFullName+1];
sl@0: 
sl@0: 	if (-1 != mbstowcs(_wmode, mode, MaxFullName))
sl@0: 		return _wfdopen_r(ptr, fd, _wmode);
sl@0: 
sl@0: 	ptr->_errno = EILSEQ;
sl@0: 	return NULL;
sl@0: 
sl@0: 	}
sl@0: /**
sl@0: A reentrant version of wfdopen().
sl@0: */
sl@0: EXPORT_C FILE * _wfdopen_r (struct _reent *ptr, int fd, const wchar_t *mode)
sl@0: {
sl@0:   register FILE *fp;
sl@0:   int flags, oflags;
sl@0: 
sl@0:   if ((flags = __sflags (ptr, mode, &oflags)) == 0)
sl@0:     return 0;
sl@0: 
sl@0:   /* make sure the mode the user wants is a subset of the actual mode */
sl@0: #ifdef F_GETFL
sl@0:   if ((fdflags = _fcntl (fd, F_GETFL, 0)) < 0)
sl@0:     return 0;
sl@0:   fdmode = fdflags & O_ACCMODE;
sl@0:   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
sl@0:     {
sl@0:       ptr->_errno = EBADF;
sl@0:       return 0;
sl@0:     }
sl@0: #endif
sl@0: 
sl@0:   if ((fp = __sfp (ptr)) == 0)
sl@0:     return 0;
sl@0:   fp->_flags = (short)flags;
sl@0:   /*
sl@0:    * If opened for appending, but underlying descriptor
sl@0:    * does not have O_APPEND bit set, assert __SAPP so that
sl@0:    * __swrite() will lseek to end before each write.
sl@0:    */
sl@0: #ifdef F_GETFL
sl@0:   if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
sl@0: #endif
sl@0:     fp->_flags |= __SAPP;
sl@0:   fp->_file = (short)fd;
sl@0:   fp->_cookie = (void*) fp;
sl@0: 
sl@0: #undef _read
sl@0: #undef _write
sl@0: #undef _seek
sl@0: #undef _close
sl@0: 
sl@0:   fp->_read = __sread;
sl@0:   fp->_write = __swrite;
sl@0:   fp->_seek = __sseek;
sl@0:   fp->_close = __sclose;
sl@0:   return fp;
sl@0: }
sl@0: 
sl@0: #ifndef _REENT_ONLY
sl@0: /**
sl@0: This function associates a stream with an open file descriptor. 
sl@0: A stream is a pointer to a FILE structure that contains information about a file.
sl@0: A stream permits user-controlled buffering and formatted input and output.
sl@0: @return a FILE pointer to the control block for the new stream.
sl@0: @param fd The open file descriptor on which to open a stream.
sl@0: @param mode The access mode for the stream. 
sl@0: */
sl@0: EXPORT_C FILE * fdopen (int fd, const char *mode)
sl@0: {
sl@0:   return _fdopen_r (_REENT, fd, mode);
sl@0: }
sl@0: 
sl@0: /**
sl@0: A wide-character version of fdopen()
sl@0: */
sl@0: EXPORT_C FILE * wfdopen (int fd, const wchar_t *mode)
sl@0: {
sl@0:   return _wfdopen_r (_REENT, fd, mode);
sl@0: }
sl@0: 
sl@0: #endif