fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / stdio / fdopen.c
blob0db6062dd51ad0b5a6c6a72e22d8bdc9dc10c849
1 /*
2 FUNCTION
3 <<fdopen>>---turn open file into a stream
5 INDEX
6 fdopen
7 INDEX
8 _fdopen_r
10 ANSI_SYNOPSIS
11 #include <stdio.h>
12 FILE *fdopen(int <[fd]>, const char *<[mode]>);
13 FILE *_fdopen_r(void *<[reent]>,
14 int <[fd]>, const char *<[mode]>);
16 TRAD_SYNOPSIS
17 #include <stdio.h>
18 FILE *fdopen(<[fd]>, <[mode]>)
19 int <[fd]>;
20 char *<[mode]>;
22 FILE *_fdopen_r(<[reent]>, <[fd]>, <[mode]>)
23 char *<[reent]>;
24 int <[fd]>;
25 char *<[mode]>);
27 DESCRIPTION
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>>.
33 RETURNS
34 File pointer or <<NULL>>, as for <<fopen>>.
36 PORTABILITY
37 <<fdopen>> is ANSI.
40 #include <sys/types.h>
41 #include <sys/fcntl.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include "local.h"
46 #include <_syslist.h>
48 extern int __sflags ();
50 FILE *
51 _DEFUN (_fdopen_r, (ptr, fd, mode),
52 struct _reent *ptr _AND
53 int fd _AND
54 _CONST char *mode)
56 register FILE *fp;
57 int flags, oflags;
58 #ifdef HAVE_FCNTL
59 int fdflags, fdmode;
60 #endif
62 if ((flags = __sflags (ptr, mode, &oflags)) == 0)
63 return 0;
65 /* make sure the mode the user wants is a subset of the actual mode */
66 #ifdef HAVE_FCNTL
67 if ((fdflags = _fcntl_r (ptr, fd, F_GETFL, 0)) < 0)
68 return 0;
69 fdmode = fdflags & O_ACCMODE;
70 if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
72 ptr->_errno = EBADF;
73 return 0;
75 #endif
77 if ((fp = __sfp (ptr)) == 0)
78 return 0;
79 fp->_flags = flags;
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)
86 #ifdef HAVE_FCNTL
87 && !(fdflags & O_APPEND)
88 #endif
90 fp->_flags |= __SAPP;
91 fp->_file = fd;
92 fp->_cookie = (_PTR) fp;
94 #undef _read
95 #undef _write
96 #undef _seek
97 #undef _close
99 fp->_read = __sread;
100 fp->_write = __swrite;
101 fp->_seek = __sseek;
102 fp->_close = __sclose;
104 #ifdef __SCLE
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;
112 #endif
114 return fp;
117 #ifndef _REENT_ONLY
119 FILE *
120 _DEFUN (fdopen, (fd, mode),
121 int fd _AND
122 _CONST char *mode)
124 return _fdopen_r (_REENT, fd, mode);
127 #endif