2 * Copyright (c) 1990 The Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * and/or other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 <<fdopen>>---turn open file into a stream
29 FILE *fdopen(int <[fd]>, const char *<[mode]>);
30 FILE *_fdopen_r(struct _reent *<[reent]>,
31 int <[fd]>, const char *<[mode]>);
34 <<fdopen>> produces a file descriptor of type <<FILE *>>, from a
35 descriptor for an already-open file (returned, for example, by the
36 system subroutine <<open>> rather than by <<fopen>>).
37 The <[mode]> argument has the same meanings as in <<fopen>>.
40 File pointer or <<NULL>>, as for <<fopen>>.
48 #include <sys/types.h>
49 #include <sys/fcntl.h>
56 _fdopen_r (struct _reent
*ptr
,
66 if ((flags
= __sflags (ptr
, mode
, &oflags
)) == 0)
69 /* make sure the mode the user wants is a subset of the actual mode */
71 if ((fdflags
= _fcntl_r (ptr
, fd
, F_GETFL
, 0)) < 0)
73 fdmode
= fdflags
& O_ACCMODE
;
74 if (fdmode
!= O_RDWR
&& (fdmode
!= (oflags
& O_ACCMODE
)))
76 _REENT_ERRNO(ptr
) = EBADF
;
81 if ((fp
= __sfp (ptr
)) == 0)
84 _newlib_flockfile_start (fp
);
87 /* POSIX recommends setting the O_APPEND bit on fd to match append
88 streams. Someone may later clear O_APPEND on fileno(fp), but the
89 stream must still remain in append mode. Rely on __sflags
90 setting __SAPP properly. */
92 if ((oflags
& O_APPEND
) && !(fdflags
& O_APPEND
))
93 _fcntl_r (ptr
, fd
, F_SETFL
, fdflags
| O_APPEND
);
96 fp
->_cookie
= (void *) fp
;
104 fp
->_write
= __swrite
;
106 fp
->_close
= __sclose
;
109 /* Explicit given mode results in explicit setting mode on fd */
110 if (oflags
& O_BINARY
)
111 setmode (fp
->_file
, O_BINARY
);
112 else if (oflags
& O_TEXT
)
113 setmode (fp
->_file
, O_TEXT
);
114 if (__stextmode (fp
->_file
))
115 fp
->_flags
|= __SCLE
;
118 _newlib_flockfile_end (fp
);
128 return _fdopen_r (_REENT
, fd
, mode
);