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