3 <<fdopen>>---turn open file into a stream
12 FILE *fdopen(int <[fd]>, const char *<[mode]>);
13 FILE *_fdopen_r(void *<[reent]>,
14 int <[fd]>, const char *<[mode]>);
18 FILE *fdopen(<[fd]>, <[mode]>)
22 FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
28 <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
29 descriptor for an already-open file (returned, for example, by the
30 system subroutine <<open>> rather than by <<fopen>>).
31 The <[mode]> argument has the same meanings as in <<fopen>>.
34 File pointer or <<NULL>>, as for <<fopen>>.
40 #include <sys/types.h>
41 #include <sys/fcntl.h>
48 extern int __sflags ();
51 _DEFUN (_fdopen_r
, (ptr
, fd
, mode
),
52 struct _reent
*ptr _AND
62 if ((flags
= __sflags (ptr
, mode
, &oflags
)) == 0)
65 /* make sure the mode the user wants is a subset of the actual mode */
67 if ((fdflags
= _fcntl_r (ptr
, fd
, F_GETFL
, 0)) < 0)
69 fdmode
= fdflags
& O_ACCMODE
;
70 if (fdmode
!= O_RDWR
&& (fdmode
!= (oflags
& O_ACCMODE
)))
77 if ((fp
= __sfp (ptr
)) == 0)
81 * If opened for appending, but underlying descriptor
82 * does not have O_APPEND bit set, assert __SAPP so that
83 * __swrite() will lseek to end before each write.
85 if ((oflags
& O_APPEND
)
87 && !(fdflags
& O_APPEND
)
92 fp
->_cookie
= (_PTR
) fp
;
100 fp
->_write
= __swrite
;
102 fp
->_close
= __sclose
;
105 /* Explicit given mode results in explicit setting mode on fd */
106 if (oflags
& O_BINARY
)
107 setmode(fp
->_file
, O_BINARY
);
108 else if (oflags
& O_TEXT
)
109 setmode(fp
->_file
, O_TEXT
);
110 if (__stextmode(fp
->_file
))
111 fp
->_flags
|= __SCLE
;
120 _DEFUN (fdopen
, (fd
, mode
),
124 return _fdopen_r (_REENT
, fd
, mode
);