1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
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.
16 */
17
18 /*
19 FUNCTION
20 <<fdopen>>---turn open file into a stream
21
22 INDEX
23 fdopen
24 INDEX
25 _fdopen_r
26
27 SYNOPSIS
28 #include <stdio.h>
29 FILE *fdopen(int <[fd]>, const char *<[mode]>);
30 FILE *fdopen(
31 int <[fd]>, const char *<[mode]>);
32
33 DESCRIPTION
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>>.
38
39 RETURNS
40 File pointer or <<NULL>>, as for <<fopen>>.
41
42 PORTABILITY
43 <<fdopen>> is ANSI.
44 */
45
46 #define _DEFAULT_SOURCE
47 #include <sys/types.h>
48 #include <sys/fcntl.h>
49 #include <stdio.h>
50 #include <errno.h>
51 #include "local.h"
52 #include <_syslist.h>
53
54 FILE *
fdopen(int fd,const char * mode)55 fdopen (
56 int fd,
57 const char *mode)
58 {
59 register FILE *fp;
60 int flags, oflags;
61 #ifdef _HAVE_FCNTL
62 int fdflags, fdmode;
63 #endif
64
65 if ((flags = __sflags (mode, &oflags)) == 0)
66 return 0;
67
68 /* make sure the mode the user wants is a subset of the actual mode */
69 #ifdef _HAVE_FCNTL
70 if ((fdflags = fcntl (fd, F_GETFL, 0)) < 0)
71 return 0;
72 fdmode = fdflags & O_ACCMODE;
73 if (fdmode != O_RDWR && (fdmode != (oflags & O_ACCMODE)))
74 {
75 _REENT_ERRNO(ptr) = EBADF;
76 return 0;
77 }
78 #endif
79
80 if ((fp = __sfp ()) == 0)
81 return 0;
82
83 _newlib_flockfile_start (fp);
84
85 fp->_flags = flags;
86 /* POSIX recommends setting the O_APPEND bit on fd to match append
87 streams. Someone may later clear O_APPEND on fileno(fp), but the
88 stream must still remain in append mode. Rely on __sflags
89 setting __SAPP properly. */
90 #ifdef _HAVE_FCNTL
91 if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
92 fcntl (fd, F_SETFL, fdflags | O_APPEND);
93 #endif
94 fp->_file = fd;
95 fp->_cookie = (void *) fp;
96
97 #undef _read
98 #undef _write
99 #undef _seek
100 #undef _close
101
102 fp->_read = __sread;
103 fp->_write = __swrite;
104 fp->_seek = __sseek;
105 fp->_close = __sclose;
106
107 #ifdef __SCLE
108 /* Explicit given mode results in explicit setting mode on fd */
109 if (oflags & O_BINARY)
110 setmode (fp->_file, O_BINARY);
111 else if (oflags & O_TEXT)
112 setmode (fp->_file, O_TEXT);
113 if (__stextmode (fp->_file))
114 fp->_flags |= __SCLE;
115 #endif
116
117 _newlib_flockfile_end (fp);
118 return fp;
119 }
120