Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
16 * <<fdopen>>---turn open file into a stream
23 * FILE *fdopen(int <[fd]>, const char *<[mode]>);
24 * FILE *_fdopen_r(void *<[reent]>,
25 * int <[fd]>, const char *<[mode]>);
28 * FILE *fdopen(<[fd]>, <[mode]>)
31 * FILE *_fdopen_r(<[reent]>, <[fd]>, <[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>>.
40 * File pointer or <<NULL>>, as for <<fopen>>.
49 #include <sys/types.h>
57 #define MaxFullName 255
59 A reentrant version of fdopen().
61 EXPORT_C FILE * _fdopen_r (struct _reent *ptr, int fd, const char *mode)
63 wchar_t _wmode[MaxFullName+1];
65 if (-1 != mbstowcs(_wmode, mode, MaxFullName))
66 return _wfdopen_r(ptr, fd, _wmode);
73 A reentrant version of wfdopen().
75 EXPORT_C FILE * _wfdopen_r (struct _reent *ptr, int fd, const wchar_t *mode)
80 if ((flags = __sflags (ptr, mode, &oflags)) == 0)
83 /* make sure the mode the user wants is a subset of the actual mode */
85 if ((fdflags = _fcntl (fd, F_GETFL, 0)) < 0)
87 fdmode = fdflags & O_ACCMODE;
88 if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
95 if ((fp = __sfp (ptr)) == 0)
97 fp->_flags = (short)flags;
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.
104 if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
106 fp->_flags |= __SAPP;
107 fp->_file = (short)fd;
108 fp->_cookie = (void*) fp;
116 fp->_write = __swrite;
118 fp->_close = __sclose;
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.
131 EXPORT_C FILE * fdopen (int fd, const char *mode)
133 return _fdopen_r (_REENT, fd, mode);
137 A wide-character version of fdopen()
139 EXPORT_C FILE * wfdopen (int fd, const wchar_t *mode)
141 return _wfdopen_r (_REENT, fd, mode);