os/ossrv/genericopenlibs/cstdlib/LSTDIO/FDOPEN.C
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/LSTDIO/FDOPEN.C	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,144 @@
     1.4 +/*
     1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description:
    1.18 +* FUNCTION
    1.19 +* <<fdopen>>---turn open file into a stream
    1.20 +* INDEX
    1.21 +* fdopen
    1.22 +* INDEX
    1.23 +* _fdopen_r
    1.24 +* ANSI_SYNOPSIS
    1.25 +* #include <stdio.h>
    1.26 +* FILE *fdopen(int <[fd]>, const char *<[mode]>);
    1.27 +* FILE *_fdopen_r(void *<[reent]>,
    1.28 +* int <[fd]>, const char *<[mode]>);
    1.29 +* TRAD_SYNOPSIS
    1.30 +* #include <stdio.h>
    1.31 +* FILE *fdopen(<[fd]>, <[mode]>)
    1.32 +* int <[fd]>;
    1.33 +* char *<[mode]>;
    1.34 +* FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
    1.35 +* char *<[reent]>;
    1.36 +* int <[fd]>;
    1.37 +* char *<[mode]>);
    1.38 +* <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
    1.39 +* descriptor for an already-open file (returned, for example, by the
    1.40 +* system subroutine <<open>> rather than by <<fopen>>).
    1.41 +* The <[mode]> argument has the same meanings as in <<fopen>>.
    1.42 +* RETURNS
    1.43 +* File pointer or <<NULL>>, as for <<fopen>>.
    1.44 +* PORTABILITY
    1.45 +* <<fdopen>> is ANSI.
    1.46 +* 
    1.47 +*
    1.48 +*/
    1.49 +
    1.50 +
    1.51 +
    1.52 +#include <sys/types.h>
    1.53 +
    1.54 +#include <stdlib_r.h>
    1.55 +#include <stdio_r.h>
    1.56 +#include <errno.h>
    1.57 +#include "LOCAL.H"
    1.58 +
    1.59 +
    1.60 +#define MaxFullName 255
    1.61 +/**
    1.62 +A reentrant version of fdopen().
    1.63 +*/
    1.64 +EXPORT_C FILE * _fdopen_r (struct _reent *ptr, int fd, const char *mode)
    1.65 +	{
    1.66 +	wchar_t _wmode[MaxFullName+1];
    1.67 +
    1.68 +	if (-1 != mbstowcs(_wmode, mode, MaxFullName))
    1.69 +		return _wfdopen_r(ptr, fd, _wmode);
    1.70 +
    1.71 +	ptr->_errno = EILSEQ;
    1.72 +	return NULL;
    1.73 +
    1.74 +	}
    1.75 +/**
    1.76 +A reentrant version of wfdopen().
    1.77 +*/
    1.78 +EXPORT_C FILE * _wfdopen_r (struct _reent *ptr, int fd, const wchar_t *mode)
    1.79 +{
    1.80 +  register FILE *fp;
    1.81 +  int flags, oflags;
    1.82 +
    1.83 +  if ((flags = __sflags (ptr, mode, &oflags)) == 0)
    1.84 +    return 0;
    1.85 +
    1.86 +  /* make sure the mode the user wants is a subset of the actual mode */
    1.87 +#ifdef F_GETFL
    1.88 +  if ((fdflags = _fcntl (fd, F_GETFL, 0)) < 0)
    1.89 +    return 0;
    1.90 +  fdmode = fdflags & O_ACCMODE;
    1.91 +  if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
    1.92 +    {
    1.93 +      ptr->_errno = EBADF;
    1.94 +      return 0;
    1.95 +    }
    1.96 +#endif
    1.97 +
    1.98 +  if ((fp = __sfp (ptr)) == 0)
    1.99 +    return 0;
   1.100 +  fp->_flags = (short)flags;
   1.101 +  /*
   1.102 +   * If opened for appending, but underlying descriptor
   1.103 +   * does not have O_APPEND bit set, assert __SAPP so that
   1.104 +   * __swrite() will lseek to end before each write.
   1.105 +   */
   1.106 +#ifdef F_GETFL
   1.107 +  if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
   1.108 +#endif
   1.109 +    fp->_flags |= __SAPP;
   1.110 +  fp->_file = (short)fd;
   1.111 +  fp->_cookie = (void*) fp;
   1.112 +
   1.113 +#undef _read
   1.114 +#undef _write
   1.115 +#undef _seek
   1.116 +#undef _close
   1.117 +
   1.118 +  fp->_read = __sread;
   1.119 +  fp->_write = __swrite;
   1.120 +  fp->_seek = __sseek;
   1.121 +  fp->_close = __sclose;
   1.122 +  return fp;
   1.123 +}
   1.124 +
   1.125 +#ifndef _REENT_ONLY
   1.126 +/**
   1.127 +This function associates a stream with an open file descriptor. 
   1.128 +A stream is a pointer to a FILE structure that contains information about a file.
   1.129 +A stream permits user-controlled buffering and formatted input and output.
   1.130 +@return a FILE pointer to the control block for the new stream.
   1.131 +@param fd The open file descriptor on which to open a stream.
   1.132 +@param mode The access mode for the stream. 
   1.133 +*/
   1.134 +EXPORT_C FILE * fdopen (int fd, const char *mode)
   1.135 +{
   1.136 +  return _fdopen_r (_REENT, fd, mode);
   1.137 +}
   1.138 +
   1.139 +/**
   1.140 +A wide-character version of fdopen()
   1.141 +*/
   1.142 +EXPORT_C FILE * wfdopen (int fd, const wchar_t *mode)
   1.143 +{
   1.144 +  return _wfdopen_r (_REENT, fd, mode);
   1.145 +}
   1.146 +
   1.147 +#endif