3 <<fdopen64>>---turn open large file into a stream
12 FILE *fdopen64(int <[fd]>, const char *<[mode]>);
13 FILE *_fdopen64_r(void *<[reent]>,
14 int <[fd]>, const char *<[mode]>);
17 <<fdopen64>> produces a file descriptor of type <<FILE *>>, from a
18 descriptor for an already-open file (returned, for example, by the
19 system subroutine <<open>> rather than by <<fopen>>).
20 The <[mode]> argument has the same meanings as in <<fopen>>.
23 File pointer or <<NULL>>, as for <<fopen>>.
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
35 extern int __sflags ();
38 _fdopen64_r (struct _reent
*ptr
,
48 if ((flags
= __sflags (ptr
, mode
, &oflags
)) == 0)
51 /* make sure the mode the user wants is a subset of the actual mode */
53 if ((fdflags
= _fcntl_r (ptr
, fd
, F_GETFL
, 0)) < 0)
55 fdmode
= fdflags
& O_ACCMODE
;
56 if (fdmode
!= O_RDWR
&& (fdmode
!= (oflags
& O_ACCMODE
)))
58 _REENT_ERRNO(ptr
) = EBADF
;
63 if ((fp
= __sfp (ptr
)) == 0)
66 _newlib_flockfile_start(fp
);
69 /* POSIX recommends setting the O_APPEND bit on fd to match append
70 streams. Someone may later clear O_APPEND on fileno(fp), but the
71 stream must still remain in append mode. Rely on __sflags
72 setting __SAPP properly. */
74 if ((oflags
& O_APPEND
) && !(fdflags
& O_APPEND
))
75 _fcntl_r (ptr
, fd
, F_SETFL
, fdflags
| O_APPEND
);
78 fp
->_cookie
= (void *) fp
;
86 fp
->_write
= __swrite64
;
88 fp
->_seek64
= __sseek64
;
89 fp
->_close
= __sclose
;
92 /* Explicit given mode results in explicit setting mode on fd */
93 if (oflags
& O_BINARY
)
94 setmode(fp
->_file
, O_BINARY
);
95 else if (oflags
& O_TEXT
)
96 setmode(fp
->_file
, O_TEXT
);
97 if (__stextmode(fp
->_file
))
101 fp
->_flags
|= __SL64
;
103 _newlib_flockfile_end(fp
);
113 return _fdopen64_r (_REENT
, fd
, mode
);