1 /* Copyright (c) 2003 Christopher Faylor <me@cgf.cx> */
2 /*
3 FUNCTION
4 <<fdopen64>>---turn open large file into a stream
5 
6 INDEX
7 	fdopen64
8 INDEX
9 	_fdopen64_r
10 
11 SYNOPSIS
12 	#include <stdio.h>
13 	FILE *fdopen64(int <[fd]>, const char *<[mode]>);
14 	FILE *_fdopen64_r(void *<[reent]>,
15                      int <[fd]>, const char *<[mode]>);
16 
17 DESCRIPTION
18 <<fdopen64>> produces a file descriptor of type <<FILE *>>, from a
19 descriptor for an already-open file (returned, for example, by the
20 system subroutine <<open>> rather than by <<fopen>>).
21 The <[mode]> argument has the same meanings as in <<fopen>>.
22 
23 RETURNS
24 File pointer or <<NULL>>, as for <<fopen>>.
25 */
26 
27 #define _DEFAULT_SOURCE
28 #include <sys/types.h>
29 #include <sys/fcntl.h>
30 
31 #include <stdio.h>
32 #include <errno.h>
33 #include <_syslist.h>
34 #include <sys/lock.h>
35 #include "../stdio/local.h"
36 
37 extern int __sflags ();
38 
39 FILE *
fdopen64(int fd,const char * mode)40 fdopen64 (
41 	int fd,
42 	const char *mode)
43 {
44   register FILE *fp;
45   int flags, oflags;
46 #ifdef _HAVE_FCNTL
47   int fdflags, fdmode;
48 #endif
49 
50   if ((flags = __sflags (mode, &oflags)) == 0)
51     return 0;
52 
53   /* make sure the mode the user wants is a subset of the actual mode */
54 #ifdef _HAVE_FCNTL
55   if ((fdflags = fcntl (fd, F_GETFL, 0)) < 0)
56     return 0;
57   fdmode = fdflags & O_ACCMODE;
58   if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
59     {
60       _REENT_ERRNO(ptr) = EBADF;
61       return 0;
62     }
63 #endif
64 
65   if ((fp = __sfp ()) == 0)
66     return 0;
67 
68   _newlib_flockfile_start(fp);
69 
70   fp->_flags = flags;
71   /* POSIX recommends setting the O_APPEND bit on fd to match append
72      streams.  Someone may later clear O_APPEND on fileno(fp), but the
73      stream must still remain in append mode.  Rely on __sflags
74      setting __SAPP properly.  */
75 #ifdef _HAVE_FCNTL
76   if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
77     fcntl (fd, F_SETFL, fdflags | O_APPEND);
78 #endif
79   fp->_file = fd;
80   fp->_cookie = (void *) fp;
81 
82 #undef _read
83 #undef _write
84 #undef _seek
85 #undef _close
86 
87   fp->_read = __sread;
88   fp->_write = __swrite64;
89   fp->_seek = __sseek;
90   fp->_seek64 = __sseek64;
91   fp->_close = __sclose;
92 
93 #ifdef __SCLE
94   /* Explicit given mode results in explicit setting mode on fd */
95   if (oflags & O_BINARY)
96     setmode(fp->_file, O_BINARY);
97   else if (oflags & O_TEXT)
98     setmode(fp->_file, O_TEXT);
99   if (__stextmode(fp->_file))
100     fp->_flags |= __SCLE;
101 #endif
102 
103   fp->_flags |= __SL64;
104 
105   _newlib_flockfile_end(fp);
106   return fp;
107 }
108