os/ossrv/genericopenlibs/cstdlib/LSTDIO/FDOPEN.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description:
    15 * FUNCTION
    16 * <<fdopen>>---turn open file into a stream
    17 * INDEX
    18 * fdopen
    19 * INDEX
    20 * _fdopen_r
    21 * ANSI_SYNOPSIS
    22 * #include <stdio.h>
    23 * FILE *fdopen(int <[fd]>, const char *<[mode]>);
    24 * FILE *_fdopen_r(void *<[reent]>,
    25 * int <[fd]>, const char *<[mode]>);
    26 * TRAD_SYNOPSIS
    27 * #include <stdio.h>
    28 * FILE *fdopen(<[fd]>, <[mode]>)
    29 * int <[fd]>;
    30 * char *<[mode]>;
    31 * FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
    32 * char *<[reent]>;
    33 * int <[fd]>;
    34 * char *<[mode]>);
    35 * <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
    36 * descriptor for an already-open file (returned, for example, by the
    37 * system subroutine <<open>> rather than by <<fopen>>).
    38 * The <[mode]> argument has the same meanings as in <<fopen>>.
    39 * RETURNS
    40 * File pointer or <<NULL>>, as for <<fopen>>.
    41 * PORTABILITY
    42 * <<fdopen>> is ANSI.
    43 * 
    44 *
    45 */
    46 
    47 
    48 
    49 #include <sys/types.h>
    50 
    51 #include <stdlib_r.h>
    52 #include <stdio_r.h>
    53 #include <errno.h>
    54 #include "LOCAL.H"
    55 
    56 
    57 #define MaxFullName 255
    58 /**
    59 A reentrant version of fdopen().
    60 */
    61 EXPORT_C FILE * _fdopen_r (struct _reent *ptr, int fd, const char *mode)
    62 	{
    63 	wchar_t _wmode[MaxFullName+1];
    64 
    65 	if (-1 != mbstowcs(_wmode, mode, MaxFullName))
    66 		return _wfdopen_r(ptr, fd, _wmode);
    67 
    68 	ptr->_errno = EILSEQ;
    69 	return NULL;
    70 
    71 	}
    72 /**
    73 A reentrant version of wfdopen().
    74 */
    75 EXPORT_C FILE * _wfdopen_r (struct _reent *ptr, int fd, const wchar_t *mode)
    76 {
    77   register FILE *fp;
    78   int flags, oflags;
    79 
    80   if ((flags = __sflags (ptr, mode, &oflags)) == 0)
    81     return 0;
    82 
    83   /* make sure the mode the user wants is a subset of the actual mode */
    84 #ifdef F_GETFL
    85   if ((fdflags = _fcntl (fd, F_GETFL, 0)) < 0)
    86     return 0;
    87   fdmode = fdflags & O_ACCMODE;
    88   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
    89     {
    90       ptr->_errno = EBADF;
    91       return 0;
    92     }
    93 #endif
    94 
    95   if ((fp = __sfp (ptr)) == 0)
    96     return 0;
    97   fp->_flags = (short)flags;
    98   /*
    99    * If opened for appending, but underlying descriptor
   100    * does not have O_APPEND bit set, assert __SAPP so that
   101    * __swrite() will lseek to end before each write.
   102    */
   103 #ifdef F_GETFL
   104   if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
   105 #endif
   106     fp->_flags |= __SAPP;
   107   fp->_file = (short)fd;
   108   fp->_cookie = (void*) fp;
   109 
   110 #undef _read
   111 #undef _write
   112 #undef _seek
   113 #undef _close
   114 
   115   fp->_read = __sread;
   116   fp->_write = __swrite;
   117   fp->_seek = __sseek;
   118   fp->_close = __sclose;
   119   return fp;
   120 }
   121 
   122 #ifndef _REENT_ONLY
   123 /**
   124 This function associates a stream with an open file descriptor. 
   125 A stream is a pointer to a FILE structure that contains information about a file.
   126 A stream permits user-controlled buffering and formatted input and output.
   127 @return a FILE pointer to the control block for the new stream.
   128 @param fd The open file descriptor on which to open a stream.
   129 @param mode The access mode for the stream. 
   130 */
   131 EXPORT_C FILE * fdopen (int fd, const char *mode)
   132 {
   133   return _fdopen_r (_REENT, fd, mode);
   134 }
   135 
   136 /**
   137 A wide-character version of fdopen()
   138 */
   139 EXPORT_C FILE * wfdopen (int fd, const wchar_t *mode)
   140 {
   141   return _wfdopen_r (_REENT, fd, mode);
   142 }
   143 
   144 #endif